Skip to content

Commit

Permalink
Fix several mojo issues when pom project is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
charphi committed Apr 18, 2024
1 parent fb10f3f commit 4b13db4
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Fix file ordering in MultiFileInput [#343](https://github.com/nbbrd/java-console-properties/issues/343)
- Fix forge URL in summary [#248](https://github.com/nbbrd/heylogs/issues/248)
- Fix missing output file in scan, check and list mojos [#246](https://github.com/nbbrd/heylogs/issues/246)
- Fix several mojo issues when pom project is not available [#249](https://github.com/nbbrd/heylogs/issues/249)

## [0.8.0] - 2024-04-10

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package internal.heylogs.maven.plugin;

import internal.heylogs.StylishFormat;

import java.io.File;

public final class HeylogsParameters {
Expand All @@ -12,15 +10,13 @@ private HeylogsParameters() {

public static final String INPUT_FILE_PROPERTY = "heylogs.input.file";

public static final String DEFAULT_INPUT_FILE = "${project.basedir}/CHANGELOG.md";
public static final String WORKING_DIR_CHANGELOG = "CHANGELOG.md";

public static final String FORMAT_ID_PROPERTY = "heylogs.format.id";

public static final String DEFAULT_FORMAT_ID = StylishFormat.ID;

public static final String OUTPUT_FILE_PROPERTY = "heylogs.output.file";

public static final String DEFAULT_OUTPUT_FILE = "";
public static final String MOJO_LOG_FILE = "";

public static boolean isMojoLogFile(File outputFile) {
return outputFile == null || outputFile.toString().isEmpty();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nbbrd.heylogs.maven.plugin;

import internal.heylogs.StylishFormat;
import internal.heylogs.semver.SemVer;
import nbbrd.heylogs.Check;
import nbbrd.heylogs.Heylogs;
Expand All @@ -17,19 +18,19 @@
import static java.util.Collections.singletonList;
import static java.util.Locale.ROOT;

@Mojo(name = "check", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true)
@Mojo(name = "check", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true, requiresProject = false)
public final class CheckMojo extends HeylogsMojo {

@Parameter(defaultValue = DEFAULT_INPUT_FILE, property = INPUT_FILE_PROPERTY)
@Parameter(defaultValue = WORKING_DIR_CHANGELOG, property = INPUT_FILE_PROPERTY)
private File inputFile;

@Parameter(defaultValue = DEFAULT_OUTPUT_FILE, property = OUTPUT_FILE_PROPERTY)
@Parameter(defaultValue = MOJO_LOG_FILE, property = OUTPUT_FILE_PROPERTY)
private File outputFile;

@Parameter(defaultValue = "false", property = "heylogs.semver")
private boolean semver;

@Parameter(defaultValue = DEFAULT_FORMAT_ID, property = FORMAT_ID_PROPERTY)
@Parameter(defaultValue = StylishFormat.ID, property = FORMAT_ID_PROPERTY)
private String formatId;

@Override
Expand All @@ -40,23 +41,25 @@ public void execute() throws MojoExecutionException {
}

if (semver) {
checkVersioning(new SemVer());
checkVersioning(new SemVer(), getProjectVersionOrNull());
}

if (inputFile.exists()) {
check();
} else {
if (isRootProject(projectBaseDir)) {
if (isRootProject()) {
raiseErrorMissingChangelog();
} else {
notifyMissingChangelog();
}
}
}

private void checkVersioning(Versioning versioning) throws MojoExecutionException {
private void checkVersioning(Versioning versioning, String projectVersion) throws MojoExecutionException {
getLog().info("Using " + versioning.getVersioningName() + " (" + versioning.getVersioningId() + ")");
if (versioning.isValidVersion(projectVersion)) {
if (projectVersion == null) {
getLog().warn("Cannot find project version");
} else if (versioning.isValidVersion(projectVersion)) {
getLog().info("Valid project version");
} else {
getLog().error(String.format(ROOT, "Invalid project version: '%s' must follow %s", projectVersion, versioning.getVersioningName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
import static internal.heylogs.maven.plugin.MojoFunction.onLocalDate;
import static internal.heylogs.maven.plugin.MojoFunction.onPattern;

@Mojo(name = "extract", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
@Mojo(name = "extract", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true, requiresProject = false)
public final class ExtractMojo extends HeylogsMojo {

@Parameter(defaultValue = DEFAULT_INPUT_FILE, property = INPUT_FILE_PROPERTY)
@Parameter(defaultValue = WORKING_DIR_CHANGELOG, property = INPUT_FILE_PROPERTY)
private File inputFile;

@Parameter(defaultValue = "${project.build.directory}/CHANGELOG.md", property = OUTPUT_FILE_PROPERTY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ abstract class HeylogsMojo extends AbstractMojo {
protected boolean skip;

@Parameter(defaultValue = "${project.version}", readonly = true)
protected String projectVersion;
private String projectVersion;

@Parameter(defaultValue = "${project.basedir}", readonly = true)
protected File projectBaseDir;
private File projectBaseDir;

protected String getProjectVersionOrNull() {
return projectVersion;
}

protected void raiseErrorMissingChangelog() throws MojoExecutionException {
getLog().error("Missing changelog");
Expand Down Expand Up @@ -60,7 +64,10 @@ protected void writeChangelog(Document document, File outputFile) throws MojoExe
}
}

protected static boolean isRootProject(File projectBaseDir) {
protected boolean isRootProject() {
if (projectBaseDir == null) {
return true;
}
File parentDir = projectBaseDir.getParentFile();
if (parentDir != null) {
File parentPom = new File(parentDir, "pom.xml");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nbbrd.heylogs.maven.plugin;

import internal.heylogs.StylishFormat;
import nbbrd.heylogs.Heylogs;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand All @@ -12,16 +13,16 @@

import static internal.heylogs.maven.plugin.HeylogsParameters.*;

@Mojo(name = "list", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true)
@Mojo(name = "list", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true, requiresProject = false)
public final class ListMojo extends HeylogsMojo {

@Parameter(defaultValue = DEFAULT_OUTPUT_FILE, property = OUTPUT_FILE_PROPERTY)
@Parameter(defaultValue = MOJO_LOG_FILE, property = OUTPUT_FILE_PROPERTY)
private File outputFile;

@Parameter(defaultValue = "false", property = "heylogs.semver")
private boolean semver;

@Parameter(defaultValue = DEFAULT_FORMAT_ID, property = FORMAT_ID_PROPERTY)
@Parameter(defaultValue = StylishFormat.ID, property = FORMAT_ID_PROPERTY)
private String formatId;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nbbrd.heylogs.maven.plugin;

import internal.heylogs.StylishFormat;
import nbbrd.heylogs.Heylogs;
import nbbrd.heylogs.Scan;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -14,16 +15,16 @@
import static internal.heylogs.maven.plugin.HeylogsParameters.*;
import static java.util.Collections.singletonList;

@Mojo(name = "scan", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true)
@Mojo(name = "scan", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true, requiresProject = false)
public final class ScanMojo extends HeylogsMojo {

@Parameter(defaultValue = DEFAULT_INPUT_FILE, property = INPUT_FILE_PROPERTY)
@Parameter(defaultValue = WORKING_DIR_CHANGELOG, property = INPUT_FILE_PROPERTY)
private File inputFile;

@Parameter(defaultValue = DEFAULT_OUTPUT_FILE, property = OUTPUT_FILE_PROPERTY)
@Parameter(defaultValue = MOJO_LOG_FILE, property = OUTPUT_FILE_PROPERTY)
private File outputFile;

@Parameter(defaultValue = DEFAULT_FORMAT_ID, property = FORMAT_ID_PROPERTY)
@Parameter(defaultValue = StylishFormat.ID, property = FORMAT_ID_PROPERTY)
private String formatId;

@Override
Expand All @@ -36,7 +37,7 @@ public void execute() throws MojoExecutionException {
if (inputFile.exists()) {
scan();
} else {
if (isRootProject(projectBaseDir)) {
if (isRootProject()) {
raiseErrorMissingChangelog();
} else {
notifyMissingChangelog();
Expand Down

0 comments on commit 4b13db4

Please sign in to comment.