Skip to content

Commit

Permalink
Merge pull request #61 from blzsaa/main
Browse files Browse the repository at this point in the history
skip writing CI friendly pom files when those are already up to date.
  • Loading branch information
ayoukhananov authored May 22, 2024
2 parents 5b7bec0 + 0c0e8a9 commit 9bd4b9d
Showing 1 changed file with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public class FlattenMojo extends AbstractCiFriendlyMojo {
@Parameter(property = "changelist")
private String changeList;

/**
* Should automatically rewrite CI friendly POM or only when there is a change.
*/
@Parameter(property = "autoRewriteCiFriendlyPoms", defaultValue = "true")
private boolean autoRewriteCiFriendlyPoms;

private final PomVisitorImpl pomVisitor = new PomVisitorImpl();

/**
Expand All @@ -56,13 +62,28 @@ public void execute() throws MojoExecutionException {
if (originalPom.equals(modifiedPom)) {
getLog().info("Pom does not have any CI friendly properties");
} else {
getLog().info("Replacing CI friendly properties for project " + this.project.getId() + "...");
final File ciFriendlyPomFile = writePom(modifiedPom);
final File ciFriendlyPomFile = isUpdateNeeded(modifiedPom) ? writePom(modifiedPom) : getCiFriendlyPomFile();
this.project.setPomFile(ciFriendlyPomFile);
this.project.setOriginalModel(getModel(ciFriendlyPomFile));
}
}

private boolean isUpdateNeeded(String content) throws MojoExecutionException {
if (autoRewriteCiFriendlyPoms) {
return true;
}
File flattenedPomFile = getCiFriendlyPomFile();
if (flattenedPomFile.exists()) {
String contentOfFlattenedPomFileBeforeUpdate = readFile(flattenedPomFile);
if(contentOfFlattenedPomFileBeforeUpdate.equals(content)){
getLog().info( "Skipping writing CI friendly properties for project " + this.project.getId() +
" as it is already up to date");
return false;
}
}
return true;
}

private Model getModel(final File file) throws MojoExecutionException {
MavenXpp3Reader reader = new MavenXpp3Reader();
try {
Expand All @@ -73,6 +94,7 @@ private Model getModel(final File file) throws MojoExecutionException {
}

private File writePom(final String content) throws MojoExecutionException {
getLog().info("Replacing CI friendly properties for project " + this.project.getId() + "...");
final File flattenedPomFile = getCiFriendlyPomFile();

final File parentFile = flattenedPomFile.getParentFile();
Expand All @@ -96,10 +118,14 @@ private File writePom(final String content) throws MojoExecutionException {

private String readPom() throws MojoExecutionException {
final File originalPomFile = this.project.getFile();
return readFile(originalPomFile);
}

private String readFile(File file) throws MojoExecutionException {
try {
return FileUtils.readFileToString(originalPomFile, Charset.defaultCharset());
return FileUtils.readFileToString(file, Charset.defaultCharset());
} catch (final IOException e) {
getLog().error("An error occurred while reading " + originalPomFile, e);
getLog().error("An error occurred while reading " + file, e);
final String message = e.getMessage();
throw new MojoExecutionException(message);
}
Expand Down

0 comments on commit 9bd4b9d

Please sign in to comment.