This is an automated email from the ASF dual-hosted git repository. elharo pushed a commit to branch code in repository https://gitbox.apache.org/repos/asf/maven-patch-plugin.git
commit 88de2c31264dad5587456dfef6b365b144f494f7 Author: Elliotte Rusty Harold <elh...@ibiblio.org> AuthorDate: Sat Dec 7 07:50:57 2024 -0500 Use Java 8 coding practices: try with resources, generics, etc. --- .../org/apache/maven/plugin/patch/ApplyMojo.java | 60 ++++++++-------------- 1 file changed, 22 insertions(+), 38 deletions(-) diff --git a/src/main/java/org/apache/maven/plugin/patch/ApplyMojo.java b/src/main/java/org/apache/maven/plugin/patch/ApplyMojo.java index d253a79..675023b 100644 --- a/src/main/java/org/apache/maven/plugin/patch/ApplyMojo.java +++ b/src/main/java/org/apache/maven/plugin/patch/ApplyMojo.java @@ -26,7 +26,6 @@ import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.codehaus.plexus.util.FileUtils; -import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.cli.CommandLineException; import org.codehaus.plexus.util.cli.CommandLineUtils; @@ -38,6 +37,7 @@ import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.StringWriter; +import java.io.Writer; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -54,15 +54,15 @@ public class ApplyMojo extends AbstractMojo { - public static final List PATCH_FAILURE_WATCH_PHRASES; + public static final List<String> PATCH_FAILURE_WATCH_PHRASES; - public static final List DEFAULT_IGNORED_PATCHES; + public static final List<String> DEFAULT_IGNORED_PATCHES; - public static final List DEFAULT_IGNORED_PATCH_PATTERNS; + public static final List<String> DEFAULT_IGNORED_PATCH_PATTERNS; static { - List watches = new ArrayList(); + List<String> watches = new ArrayList<>(); watches.add( "fail" ); watches.add( "skip" ); @@ -70,14 +70,14 @@ public class ApplyMojo PATCH_FAILURE_WATCH_PHRASES = watches; - List ignored = new ArrayList(); + List<String> ignored = new ArrayList<>(); ignored.add( ".svn" ); ignored.add( "CVS" ); DEFAULT_IGNORED_PATCHES = ignored; - List ignoredPatterns = new ArrayList(); + List<String> ignoredPatterns = new ArrayList<>(); ignoredPatterns.add( ".svn/**" ); ignoredPatterns.add( "CVS/**" ); @@ -97,7 +97,7 @@ public class ApplyMojo * is mutually exclusive with the <code>patchfile</code> parameter. */ @Parameter - protected List patches; + protected List<String> patches; /** * Whether to skip this goal's execution. @@ -194,7 +194,7 @@ public class ApplyMojo * <code>skip</code> and <code>reject</code> are used. */ @Parameter - private List failurePhrases = PATCH_FAILURE_WATCH_PHRASES; + private List<String> failurePhrases = PATCH_FAILURE_WATCH_PHRASES; /** * The original file which will be modified by the patch. By default, the patch tool will automatically derive the @@ -296,7 +296,7 @@ public class ApplyMojo getLog().info( "Exclude pattern: " + excludePatterns ); } - List foundPatchFiles = FileUtils.getFileNames( patchDirectory, "*", excludePatterns, false ); + List<String> foundPatchFiles = FileUtils.getFileNames( patchDirectory, "*", excludePatterns, false ); patchesToApply = findPatchesToApply( foundPatchFiles, patchDirectory ); @@ -340,10 +340,7 @@ public class ApplyMojo throw new MojoFailureException( "unable to read patch tracking file: " + ioe.getMessage() ); } - for ( Object patche : patches ) - { - String patch = (String) patche; - + for ( String patch : patches ) { if ( !alreadyAppliedPatches.contains( patch ) ) { File patchFile = new File( patchSourceDir, patch ); @@ -425,7 +422,7 @@ public class ApplyMojo } } - private String applyPatches( Map patchesApplied ) + private String applyPatches( Map<String, Commandline> patchesApplied ) throws MojoExecutionException { final StringWriter outputWriter = new StringWriter(); @@ -446,11 +443,9 @@ public class ApplyMojo // used if failFast is false List failedPatches = new ArrayList(); - for ( Object o : patchesApplied.entrySet() ) - { - Entry entry = (Entry) o; - String patchName = (String) entry.getKey(); - Commandline cli = (Commandline) entry.getValue(); + for ( Entry<String, Commandline> entry : patchesApplied.entrySet()) { + String patchName = entry.getKey(); + Commandline cli = entry.getValue(); try { @@ -481,12 +476,12 @@ public class ApplyMojo if ( !failedPatches.isEmpty() ) { getLog().error( "Failed applying one or more patches:" ); - for ( Object failedPatche : failedPatches ) + for ( Object failedPatch : failedPatches ) { - getLog().error( "* " + failedPatche ); + getLog().error( "* " + failedPatch ); } throw new MojoExecutionException( "Patch command failed for one or more patches." - + " Please see console and debug output for more information." ); + + " See console and debug output for more information." ); } return outputWriter.toString(); @@ -513,12 +508,9 @@ public class ApplyMojo private void writeTrackingFile( Map patchesApplied ) throws MojoExecutionException { - FileWriter writer = null; - try - { - boolean appending = patchTrackingFile.exists(); + boolean appending = patchTrackingFile.exists(); - writer = new FileWriter( patchTrackingFile, appending ); + try (Writer writer = new FileWriter( patchTrackingFile, appending )) { for ( Iterator it = patchesApplied.keySet().iterator(); it.hasNext(); ) { @@ -536,26 +528,18 @@ public class ApplyMojo } } - writer.close(); - writer = null; } catch ( IOException e ) { throw new MojoExecutionException( "Failed to write patch-tracking file: " + patchTrackingFile, e ); } - finally - { - IOUtil.close( writer ); - } } private void checkForWatchPhrases( String output ) throws MojoExecutionException { - for ( Object failurePhrase : failurePhrases ) + for ( String phrase : failurePhrases ) { - String phrase = (String) failurePhrase; - if ( output.contains( phrase ) ) { throw new MojoExecutionException( "Failed to apply patches (detected watch-phrase: \'" + phrase @@ -565,7 +549,7 @@ public class ApplyMojo } /** - * Add a new Patch task to the Ant calling mechanism. Give preference to originalFile/destFile, then workDir, and + * Add a new Patch task to the calling mechanism. Give preference to originalFile/destFile, then workDir, and * finally ${basedir}. */ private Commandline createPatchCommand( File patchFile )