diff --git a/.github/README.md b/.github/README.md index 247d434..0213041 100644 --- a/.github/README.md +++ b/.github/README.md @@ -33,7 +33,7 @@ This plugin is only available for Maven and can be installed with the following io.github.lukasmansour patch-maven-plugin - 1.0-SNAPSHOT + 1.1.0-SNAPSHOT ${project.basedir} ${project.basedir}\src\main\patches diff --git a/build.gradle.kts b/build.gradle.kts index 25ab206..de2efc3 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { } group = "io.github.lukasmansour" -version = "1.0" +version = "1.1.0-SNAPSHOT" description = "A Maven patch plugin with no dependency on GNU Patch." java { diff --git a/src/main/java/io/github/lukasmansour/patch/PatchMojo.java b/src/main/java/io/github/lukasmansour/patch/PatchMojo.java index f9b6e3e..51fffa2 100644 --- a/src/main/java/io/github/lukasmansour/patch/PatchMojo.java +++ b/src/main/java/io/github/lukasmansour/patch/PatchMojo.java @@ -26,7 +26,10 @@ import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.nio.file.Path; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.LifecyclePhase; @@ -45,6 +48,9 @@ public class PatchMojo extends AbstractMojo { @Parameter(property = "patch.patchDirectory", defaultValue = "src/main/patches") private File patchDirectory; + @Parameter(property = "patch.failOnFailedPatch", defaultValue = "false") + private boolean failOnFailedPatch; + public void execute() throws MojoExecutionException { getLog().info(targetDirectory.toString()); getLog().info("Applying patches..."); @@ -66,6 +72,7 @@ public void execute() throws MojoExecutionException { UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff( new FileInputStream(patchFile)); + Map> writeResults = new HashMap<>(); for (UnifiedDiffFile file : diff.getFiles()) { Path targetFile = targetDirectory.toPath().resolve(file.getToFile()); String targetFileName = targetFile.getFileName().toString(); @@ -75,17 +82,27 @@ public void execute() throws MojoExecutionException { Files.readAllLines(targetFile) ); - Files.write(targetFile, results); + writeResults.put(targetFile, results); + getLog().info( String.format("Applied diff to '%s' successfully.", targetFileName)); } catch (PatchFailedException pfe1) { - getLog().warn(String.format( + String failureMessage = String.format( "Failed to apply patch file '%s' to file '%s'. (It may already have been applied!)", - patchFileName, targetFileName)); + patchFileName, targetFileName); + + if (failOnFailedPatch) { + throw new MojoExecutionException(failureMessage); + } else { + getLog().warn(failureMessage); + } break; } getLog().info(String.format("Finished applying diff to '%s'.", targetFileName)); } + for (Entry> entry : writeResults.entrySet()) { + Files.write(entry.getKey(), entry.getValue()); + } getLog().info(String.format("Finished applying patch '%s'.", patchFileName)); } diff --git a/test-project/pom.xml b/test-project/pom.xml index f9b31e1..9c76d4a 100644 --- a/test-project/pom.xml +++ b/test-project/pom.xml @@ -35,11 +35,21 @@ io.github.lukasmansour patch-maven-plugin - 1.0-SNAPSHOT + 1.1.0 ${project.basedir}\src\main\patches + true + + + + + + apply + + +