diff --git a/src/main/java/org/apache/maven/plugins/shade/DefaultShader.java b/src/main/java/org/apache/maven/plugins/shade/DefaultShader.java index e990440f..c4d677a9 100644 --- a/src/main/java/org/apache/maven/plugins/shade/DefaultShader.java +++ b/src/main/java/org/apache/maven/plugins/shade/DefaultShader.java @@ -27,12 +27,12 @@ import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Writer; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Collection; import java.util.Enumeration; import java.util.HashSet; import java.util.Iterator; -import java.util.LinkedList; import java.util.List; import java.util.Set; import java.util.jar.JarEntry; @@ -159,9 +159,7 @@ private void shadeJars( ShadeRequest shadeRequest, Set resources, List jarFilters = getFilters( jar, shadeRequest.getFilters() ); - JarFile jarFile = newJarFile( jar ); - - try + try ( JarFile jarFile = newJarFile( jar ) ) { for ( Enumeration j = jarFile.entries(); j.hasMoreElements(); ) @@ -183,7 +181,7 @@ private void shadeJars( ShadeRequest shadeRequest, Set resources, List resources, List resources, JarEntry entry, String name ) throws IOException, MojoExecutionException { - InputStream in = null; - try + try ( InputStream in = jarFile.getInputStream( entry ) ) { - in = jarFile.getInputStream( entry ); String mappedName = remapper.map( name ); int idx = mappedName.lastIndexOf( '/' ); @@ -262,13 +254,6 @@ else if ( shadeRequest.isShadeSourcesContent() && name.endsWith( ".java" ) ) addResource( resources, jos, mappedName, entry.getTime(), in ); } } - - in.close(); - in = null; - } - finally - { - IOUtil.close( in ); } } @@ -281,8 +266,7 @@ private void goThroughAllJarEntriesForManifestTransformer( ShadeRequest shadeReq { for ( File jar : shadeRequest.getJars() ) { - JarFile jarFile = newJarFile( jar ); - try + try ( JarFile jarFile = newJarFile( jar ) ) { for ( Enumeration en = jarFile.entries(); en.hasMoreElements(); ) { @@ -291,24 +275,15 @@ private void goThroughAllJarEntriesForManifestTransformer( ShadeRequest shadeReq if ( manifestTransformer.canTransformResource( resource ) ) { resources.add( resource ); - InputStream inputStream = jarFile.getInputStream( entry ); - try + try ( InputStream inputStream = jarFile.getInputStream( entry ) ) { manifestTransformer.processResource( resource, inputStream, - shadeRequest.getRelocators() ); - } - finally - { - inputStream.close(); + shadeRequest.getRelocators() ); } break; } } } - finally - { - jarFile.close(); - } } if ( manifestTransformer.hasTransformedResource() ) { @@ -332,14 +307,14 @@ private void logSummaryOfDuplicates( Multimap, String> overlapp { for ( Collection jarz : overlapping.keySet() ) { - List jarzS = new LinkedList<>(); + List jarzS = new ArrayList<>(); for ( File jjar : jarz ) { jarzS.add( jjar.getName() ); } - List classes = new LinkedList<>(); + List classes = new ArrayList<>(); for ( String clazz : overlapping.get( jarz ) ) { @@ -483,7 +458,7 @@ public void visitSource( final String source, final String debug ) // Now we put it back on so the class file is written out with the right extension. jos.putNextEntry( new JarEntry( mappedName + ".class" ) ); - IOUtil.copy( renamedClass, jos ); + jos.write( renamedClass ); } catch ( ZipException e ) { @@ -532,15 +507,15 @@ private void addJavaSource( Set resources, JarOutputStream jos, String n { jos.putNextEntry( new JarEntry( name ) ); - String sourceContent = IOUtil.toString( new InputStreamReader( is, "UTF-8" ) ); + String sourceContent = IOUtil.toString( new InputStreamReader( is, StandardCharsets.UTF_8 ) ); for ( Relocator relocator : relocators ) { sourceContent = relocator.applyToSourceContent( sourceContent ); } - final Writer writer = new OutputStreamWriter( jos, "UTF-8" ); - IOUtil.copy( sourceContent, writer ); + final Writer writer = new OutputStreamWriter( jos, StandardCharsets.UTF_8 ); + writer.write( sourceContent ); writer.flush(); resources.add( name ); diff --git a/src/main/java/org/apache/maven/plugins/shade/filter/MinijarFilter.java b/src/main/java/org/apache/maven/plugins/shade/filter/MinijarFilter.java index 031f92b8..bccf1b45 100644 --- a/src/main/java/org/apache/maven/plugins/shade/filter/MinijarFilter.java +++ b/src/main/java/org/apache/maven/plugins/shade/filter/MinijarFilter.java @@ -117,22 +117,17 @@ public MinijarFilter( MavenProject project, Log log, List simpleFi private ClazzpathUnit addDependencyToClasspath( Clazzpath cp, Artifact dependency ) throws IOException { - InputStream is = null; ClazzpathUnit clazzpathUnit = null; - try + try ( InputStream is = new FileInputStream( dependency.getFile() ) ) { - is = new FileInputStream( dependency.getFile() ); clazzpathUnit = cp.addClazzpathUnit( is, dependency.toString() ); - is.close(); - is = null; } catch ( ZipException e ) { log.warn( dependency.getFile() + " could not be unpacked/read for minimization; dependency is probably malformed." ); IOException ioe = new IOException( "Dependency " + dependency.toString() + " in file " - + dependency.getFile() + " could not be unpacked. File is probably corrupt" ); - ioe.initCause( e ); + + dependency.getFile() + " could not be unpacked. File is probably corrupt", e ); throw ioe; } catch ( ArrayIndexOutOfBoundsException | IllegalArgumentException e ) @@ -141,10 +136,6 @@ private ClazzpathUnit addDependencyToClasspath( Clazzpath cp, Artifact dependenc log.warn( dependency.toString() + " could not be analyzed for minimization; dependency is probably malformed." ); } - finally - { - IOUtil.close( is ); - } return clazzpathUnit; } diff --git a/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java b/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java index 4a5c021a..c99ed420 100644 --- a/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java +++ b/src/main/java/org/apache/maven/plugins/shade/mojo/ShadeMojo.java @@ -695,22 +695,12 @@ private void replaceFile( File oldFile, File newFile ) private void copyFiles( File source, File target ) throws IOException { - InputStream in = null; - OutputStream out = null; - try - { - in = new FileInputStream( source ); - out = new FileOutputStream( target ); - IOUtil.copy( in, out ); - out.close(); - out = null; - in.close(); - in = null; - } - finally + try ( InputStream in = new FileInputStream( source ) ) { - IOUtil.close( in ); - IOUtil.close( out ); + try ( OutputStream out = new FileOutputStream( target ) ) + { + IOUtil.copy( in, out ); + } } } diff --git a/src/main/java/org/apache/maven/plugins/shade/pom/MavenJDOMWriter.java b/src/main/java/org/apache/maven/plugins/shade/pom/MavenJDOMWriter.java index 80772e44..412527b4 100644 --- a/src/main/java/org/apache/maven/plugins/shade/pom/MavenJDOMWriter.java +++ b/src/main/java/org/apache/maven/plugins/shade/pom/MavenJDOMWriter.java @@ -19,6 +19,7 @@ * under the License. */ +import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; @@ -113,22 +114,22 @@ public MavenJDOMWriter() */ protected Element findAndReplaceProperties( Counter counter, Element parent, String name, Map props ) { - boolean shouldExist = props != null && !props.isEmpty(); + Map properties = props; + boolean shouldExist = properties != null && !properties.isEmpty(); Element element = updateElement( counter, parent, name, shouldExist ); if ( shouldExist ) { Counter innerCounter = new Counter( counter.getDepth() + 1 ); - // while ( it.hasNext() ) - for ( Map.Entry entry : ( (Map) props ).entrySet() ) + for ( Map.Entry entry : properties.entrySet() ) { String key = entry.getKey(); findAndReplaceSimpleElement( innerCounter, element, key, entry.getValue(), null ); } - List lst = new ArrayList( props.keySet() ); - Iterator it = element.getChildren().iterator(); + List lst = new ArrayList<>( properties.keySet() ); + Iterator it = element.getChildren().iterator(); while ( it.hasNext() ) { - Element elem = (Element) it.next(); + Element elem = it.next(); String key = elem.getName(); if ( !lst.contains( key ) ) { @@ -155,7 +156,7 @@ protected Element findAndReplaceSimpleElement( Counter counter, Element parent, { Element element = parent.getChild( name, parent.getNamespace() ); // if exist and is default value or if doesn't exist.. just keep the way it is.. - if ( ( element != null && defaultValue.equals( element.getText() ) ) || element == null ) + if ( element == null || defaultValue.equals( element.getText() ) ) { return element; } @@ -178,14 +179,14 @@ protected Element findAndReplaceSimpleElement( Counter counter, Element parent, * @param list * @param parent */ - protected Element findAndReplaceSimpleLists( Counter counter, Element parent, java.util.Collection list, + protected Element findAndReplaceSimpleLists( Counter counter, Element parent, Collection list, String parentName, String childName ) { boolean shouldExist = list != null && list.size() > 0; Element element = updateElement( counter, parent, parentName, shouldExist ); if ( shouldExist ) { - Iterator elIt = element.getChildren( childName, element.getNamespace() ).iterator(); + Iterator elIt = element.getChildren( childName, element.getNamespace() ).iterator(); if ( !elIt.hasNext() ) { elIt = null; @@ -196,7 +197,7 @@ protected Element findAndReplaceSimpleLists( Counter counter, Element parent, ja Element el; if ( elIt != null && elIt.hasNext() ) { - el = (Element) elIt.next(); + el = elIt.next(); if ( !elIt.hasNext() ) { elIt = null; @@ -252,7 +253,7 @@ protected void insertAtPreferredLocation( Element parent, Element child, Counter { int contentIndex = 0; int elementCounter = 0; - Iterator it = parent.getContent().iterator(); + Iterator it = parent.getContent().iterator(); Text lastText = null; int offset = 0; while ( it.hasNext() && elementCounter <= counter.getCurrentIndex() ) @@ -272,20 +273,20 @@ protected void insertAtPreferredLocation( Element parent, Element child, Counter } if ( lastText != null && lastText.getTextTrim().length() == 0 ) { - lastText = (Text) lastText.clone(); + lastText = lastText.clone(); } else { - String starter = lineSeparator; + StringBuilder starter = new StringBuilder( lineSeparator ); for ( int i = 0; i < counter.getDepth(); i++ ) { - starter = starter + " "; // TODO make settable? + starter.append( " " ); // TODO make settable? } - lastText = factory.text( starter ); + lastText = factory.text( starter.toString() ); } if ( parent.getContentSize() == 0 ) { - Text finalText = (Text) lastText.clone(); + Text finalText = lastText.clone(); finalText.setText( finalText.getText().substring( 0, finalText.getText().length() - " ".length() ) ); parent.addContent( contentIndex, finalText ); } @@ -303,13 +304,13 @@ protected void insertAtPreferredLocation( Element parent, Element child, Counter * @param parent */ protected void iterateContributor( Counter counter, Element parent, Collection list, - java.lang.String parentTag, java.lang.String childTag ) + String parentTag, String childTag ) { boolean shouldExist = list != null && list.size() > 0; Element element = updateElement( counter, parent, parentTag, shouldExist ); if ( shouldExist ) { - Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); + Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); if ( !elIt.hasNext() ) { elIt = null; @@ -320,7 +321,7 @@ protected void iterateContributor( Counter counter, Element parent, Collection list, - java.lang.String parentTag, java.lang.String childTag ) + String parentTag, String childTag ) { boolean shouldExist = list != null && list.size() > 0; Element element = updateElement( counter, parent, parentTag, shouldExist ); if ( shouldExist ) { - Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); + Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); if ( !elIt.hasNext() ) { elIt = null; @@ -372,7 +373,7 @@ protected void iterateDependency( Counter counter, Element parent, Collection list, - java.lang.String parentTag, java.lang.String childTag ) + String parentTag, String childTag ) { boolean shouldExist = list != null && list.size() > 0; Element element = updateElement( counter, parent, parentTag, shouldExist ); if ( shouldExist ) { - Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); + Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); if ( !elIt.hasNext() ) { elIt = null; @@ -424,7 +425,7 @@ protected void iterateDeveloper( Counter counter, Element parent, Collection list, - java.lang.String parentTag, java.lang.String childTag ) + String parentTag, String childTag ) { boolean shouldExist = list != null && list.size() > 0; Element element = updateElement( counter, parent, parentTag, shouldExist ); if ( shouldExist ) { - Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); + Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); if ( !elIt.hasNext() ) { elIt = null; @@ -476,7 +477,7 @@ protected void iterateExclusion( Counter counter, Element parent, Collection list, - java.lang.String parentTag, java.lang.String childTag ) + String parentTag, String childTag ) { boolean shouldExist = list != null && list.size() > 0; Element element = updateElement( counter, parent, parentTag, shouldExist ); if ( shouldExist ) { - Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); + Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); if ( !elIt.hasNext() ) { elIt = null; @@ -528,7 +529,7 @@ protected void iterateExtension( Counter counter, Element parent, Collection list, - java.lang.String parentTag, java.lang.String childTag ) + String parentTag, String childTag ) { boolean shouldExist = list != null && list.size() > 0; Element element = updateElement( counter, parent, parentTag, shouldExist ); if ( shouldExist ) { - Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); + Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); if ( !elIt.hasNext() ) { elIt = null; @@ -580,7 +581,7 @@ protected void iterateLicense( Counter counter, Element parent, Collection list, - java.lang.String parentTag, java.lang.String childTag ) + String parentTag, String childTag ) { boolean shouldExist = list != null && list.size() > 0; Element element = updateElement( counter, parent, parentTag, shouldExist ); if ( shouldExist ) { - Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); + Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); if ( !elIt.hasNext() ) { elIt = null; @@ -632,7 +633,7 @@ protected void iterateMailingList( Counter counter, Element parent, Collection list, - java.lang.String parentTag, java.lang.String childTag ) + protected void iterateNotifier( Counter counter, Element parent, Collection list, + String parentTag, String childTag ) { boolean shouldExist = list != null && list.size() > 0; Element element = updateElement( counter, parent, parentTag, shouldExist ); if ( shouldExist ) { - Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); + Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); if ( !elIt.hasNext() ) { elIt = null; @@ -684,7 +685,7 @@ protected void iterateNotifier( Counter counter, Element parent, java.util.Colle Element el; if ( elIt != null && elIt.hasNext() ) { - el = (Element) elIt.next(); + el = elIt.next(); if ( !elIt.hasNext() ) { elIt = null; @@ -719,13 +720,13 @@ protected void iterateNotifier( Counter counter, Element parent, java.util.Colle * @param parent */ protected void iteratePlugin( Counter counter, Element parent, Collection list, - java.lang.String parentTag, java.lang.String childTag ) + String parentTag, String childTag ) { boolean shouldExist = list != null && list.size() > 0; Element element = updateElement( counter, parent, parentTag, shouldExist ); if ( shouldExist ) { - Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); + Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); if ( !elIt.hasNext() ) { elIt = null; @@ -736,7 +737,7 @@ protected void iteratePlugin( Counter counter, Element parent, Collection list, - java.lang.String parentTag, java.lang.String childTag ) + String parentTag, String childTag ) { boolean shouldExist = list != null && list.size() > 0; Element element = updateElement( counter, parent, parentTag, shouldExist ); if ( shouldExist ) { - Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); + Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); if ( !elIt.hasNext() ) { elIt = null; @@ -788,7 +789,7 @@ protected void iteratePluginExecution( Counter counter, Element parent, Collecti Element el; if ( elIt != null && elIt.hasNext() ) { - el = (Element) elIt.next(); + el = elIt.next(); if ( !elIt.hasNext() ) { elIt = null; @@ -823,13 +824,13 @@ protected void iteratePluginExecution( Counter counter, Element parent, Collecti * @param parent */ protected void iterateProfile( Counter counter, Element parent, Collection list, - java.lang.String parentTag, java.lang.String childTag ) + String parentTag, String childTag ) { boolean shouldExist = list != null && list.size() > 0; Element element = updateElement( counter, parent, parentTag, shouldExist ); if ( shouldExist ) { - Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); + Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); if ( !elIt.hasNext() ) { elIt = null; @@ -840,7 +841,7 @@ protected void iterateProfile( Counter counter, Element parent, Collection list, - java.lang.String parentTag, java.lang.String childTag ) + String parentTag, String childTag ) { boolean shouldExist = list != null && list.size() > 0; Element element = updateElement( counter, parent, parentTag, shouldExist ); if ( shouldExist ) { - Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); + Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); if ( !elIt.hasNext() ) { elIt = null; @@ -892,7 +893,7 @@ protected void iterateReportPlugin( Counter counter, Element parent, Collection< Element el; if ( elIt != null && elIt.hasNext() ) { - el = (Element) elIt.next(); + el = elIt.next(); if ( !elIt.hasNext() ) { elIt = null; @@ -927,13 +928,13 @@ protected void iterateReportPlugin( Counter counter, Element parent, Collection< * @param parent */ protected void iterateReportSet( Counter counter, Element parent, Collection list, - java.lang.String parentTag, java.lang.String childTag ) + String parentTag, String childTag ) { boolean shouldExist = list != null && list.size() > 0; Element element = updateElement( counter, parent, parentTag, shouldExist ); if ( shouldExist ) { - Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); + Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); if ( !elIt.hasNext() ) { elIt = null; @@ -944,7 +945,7 @@ protected void iterateReportSet( Counter counter, Element parent, Collection list, - java.lang.String parentTag, java.lang.String childTag ) + String parentTag, String childTag ) { boolean shouldExist = list != null && list.size() > 0; Element element = updateElement( counter, parent, parentTag, shouldExist ); if ( shouldExist ) { - Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); + Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); if ( !elIt.hasNext() ) { elIt = null; @@ -996,7 +997,7 @@ protected void iterateRepository( Counter counter, Element parent, Collection list, - java.lang.String parentTag, java.lang.String childTag ) + String parentTag, String childTag ) { boolean shouldExist = list != null && list.size() > 0; Element element = updateElement( counter, parent, parentTag, shouldExist ); if ( shouldExist ) { - Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); + Iterator elIt = element.getChildren( childTag, element.getNamespace() ).iterator(); if ( !elIt.hasNext() ) { elIt = null; @@ -1048,7 +1049,7 @@ protected void iterateResource( Counter counter, Element parent, Collection re public boolean hasTransformedResource() { - return file != null ? file.exists() : false; + return file != null && file.exists(); } public void modifyOutputStream( JarOutputStream jos ) throws IOException { - InputStream in = null; - try + try ( InputStream in = new FileInputStream( file ) ) { jos.putNextEntry( new JarEntry( resource ) ); - - in = new FileInputStream( file ); IOUtil.copy( in, jos ); - in.close(); - in = null; - } - finally - { - IOUtil.close( in ); } } diff --git a/src/main/java/org/apache/maven/plugins/shade/resource/PluginXmlResourceTransformer.java b/src/main/java/org/apache/maven/plugins/shade/resource/PluginXmlResourceTransformer.java index 98e18e6b..4ae690d3 100644 --- a/src/main/java/org/apache/maven/plugins/shade/resource/PluginXmlResourceTransformer.java +++ b/src/main/java/org/apache/maven/plugins/shade/resource/PluginXmlResourceTransformer.java @@ -25,13 +25,12 @@ import java.io.InputStream; import java.io.Reader; import java.io.Writer; -import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; import org.apache.maven.plugins.shade.relocation.Relocator; -import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.ReaderFactory; import org.codehaus.plexus.util.WriterFactory; import org.codehaus.plexus.util.xml.Xpp3Dom; @@ -47,7 +46,7 @@ public class PluginXmlResourceTransformer implements ResourceTransformer { - private List mojos = new LinkedList<>(); + private List mojos = new ArrayList<>(); public static final String PLUGIN_XML_PATH = "META-INF/maven/plugin.xml"; @@ -78,7 +77,7 @@ public void close() } catch ( Exception e ) { - throw (IOException) new IOException( "Error parsing plugin.xml in " + is ).initCause( e ); + throw new IOException( "Error parsing plugin.xml in " + is, e ); } // Only try to merge in mojos if there are some elements in the plugin @@ -137,7 +136,7 @@ public void modifyOutputStream( JarOutputStream jos ) jos.putNextEntry( new JarEntry( PLUGIN_XML_PATH ) ); - IOUtil.copy( data, jos ); + jos.write( data ); mojos.clear(); } @@ -152,8 +151,7 @@ byte[] getTransformedResource() { ByteArrayOutputStream baos = new ByteArrayOutputStream( 1024 * 4 ); - Writer writer = WriterFactory.newXmlWriter( baos ); - try + try ( Writer writer = WriterFactory.newXmlWriter( baos ) ) { Xpp3Dom dom = new Xpp3Dom( "plugin" ); @@ -167,13 +165,6 @@ byte[] getTransformedResource() } Xpp3DomWriter.write( writer, dom ); - - writer.close(); - writer = null; - } - finally - { - IOUtil.close( writer ); } return baos.toByteArray(); diff --git a/src/main/java/org/apache/maven/plugins/shade/resource/ResourceBundleAppendingTransformer.java b/src/main/java/org/apache/maven/plugins/shade/resource/ResourceBundleAppendingTransformer.java index 7d73ed87..39bb9087 100644 --- a/src/main/java/org/apache/maven/plugins/shade/resource/ResourceBundleAppendingTransformer.java +++ b/src/main/java/org/apache/maven/plugins/shade/resource/ResourceBundleAppendingTransformer.java @@ -19,7 +19,6 @@ * under the License. */ -import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; @@ -89,7 +88,7 @@ public void modifyOutputStream( JarOutputStream jos ) { jos.putNextEntry( new JarEntry( dataEntry.getKey() ) ); - IOUtil.copy( new ByteArrayInputStream( dataEntry.getValue().toByteArray() ), jos ); + jos.write( dataEntry.getValue().toByteArray() ); dataEntry.getValue().reset(); } } diff --git a/src/main/java/org/apache/maven/plugins/shade/resource/ResourceTransformer.java b/src/main/java/org/apache/maven/plugins/shade/resource/ResourceTransformer.java index 9152b589..877f80b1 100644 --- a/src/main/java/org/apache/maven/plugins/shade/resource/ResourceTransformer.java +++ b/src/main/java/org/apache/maven/plugins/shade/resource/ResourceTransformer.java @@ -33,7 +33,7 @@ public interface ResourceTransformer /** * Transform an individual resource - * @param resource The resoure name + * @param resource The resource name * @param is An input stream for the resource, the implementation should *not* close this stream * @param relocators A list of relocators * @throws IOException When the IO blows up diff --git a/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java b/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java index ea5b784d..b8d624f8 100644 --- a/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java +++ b/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java @@ -27,6 +27,7 @@ import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringReader; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -178,7 +179,7 @@ public void append( String content ) write( '\n' ); } - byte[] contentBytes = content.getBytes( "UTF-8" ); + byte[] contentBytes = content.getBytes( StandardCharsets.UTF_8 ); this.write( contentBytes ); } diff --git a/src/test/java/org/apache/maven/plugins/shade/filter/MinijarFilterTest.java b/src/test/java/org/apache/maven/plugins/shade/filter/MinijarFilterTest.java index bd32ef2f..b9ff154a 100644 --- a/src/test/java/org/apache/maven/plugins/shade/filter/MinijarFilterTest.java +++ b/src/test/java/org/apache/maven/plugins/shade/filter/MinijarFilterTest.java @@ -153,7 +153,7 @@ public void finsishedShouldProduceMessageForClassesTotalNonZero() } @Test - public void finsishedShouldProduceMessageForClassesTotalZero() + public void finishedShouldProduceMessageForClassesTotalZero() { ArgumentCaptor logCaptor = ArgumentCaptor.forClass( CharSequence.class ); diff --git a/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java b/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java index 5bc8ee69..208bac5f 100644 --- a/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java @@ -105,8 +105,8 @@ public void testShaderWithExclusions() Field field = c.getDeclaredField( "CLASS_REALM_PACKAGE_IMPORT" ); assertEquals( "org.codehaus.plexus.util.xml.pull", field.get( null ) ); - Method method = c.getDeclaredMethod( "getClassRealmPackageImport", new Class[0] ); - assertEquals( "org.codehaus.plexus.util.xml.pull", method.invoke( null, new Object[0] ) ); + Method method = c.getDeclaredMethod( "getClassRealmPackageImport" ); + assertEquals( "org.codehaus.plexus.util.xml.pull", method.invoke( null ) ); } /** @@ -202,7 +202,7 @@ public Artifact getArtifact() sessionField.set( mojo, mock( MavenSession.class ) ); // invoke getFilters() - Method getFilters = ShadeMojo.class.getDeclaredMethod( "getFilters", new Class[0] ); + Method getFilters = ShadeMojo.class.getDeclaredMethod( "getFilters" ); getFilters.setAccessible( true ); List filters = (List) getFilters.invoke( mojo); diff --git a/src/test/java/org/apache/maven/plugins/shade/relocation/SimpleRelocatorTest.java b/src/test/java/org/apache/maven/plugins/shade/relocation/SimpleRelocatorTest.java index 13dabace..eab51545 100644 --- a/src/test/java/org/apache/maven/plugins/shade/relocation/SimpleRelocatorTest.java +++ b/src/test/java/org/apache/maven/plugins/shade/relocation/SimpleRelocatorTest.java @@ -40,32 +40,32 @@ public void testCanRelocatePath() SimpleRelocator relocator; relocator = new SimpleRelocator( "org.foo", null, null, null ); - assertEquals( true, relocator.canRelocatePath( "org/foo/Class" ) ); - assertEquals( true, relocator.canRelocatePath( "org/foo/Class.class" ) ); - assertEquals( true, relocator.canRelocatePath( "org/foo/bar/Class" ) ); - assertEquals( true, relocator.canRelocatePath( "org/foo/bar/Class.class" ) ); - assertEquals( false, relocator.canRelocatePath( "com/foo/bar/Class" ) ); - assertEquals( false, relocator.canRelocatePath( "com/foo/bar/Class.class" ) ); - assertEquals( false, relocator.canRelocatePath( "org/Foo/Class" ) ); - assertEquals( false, relocator.canRelocatePath( "org/Foo/Class.class" ) ); + assertTrue( relocator.canRelocatePath( "org/foo/Class" ) ); + assertTrue( relocator.canRelocatePath( "org/foo/Class.class" ) ); + assertTrue( relocator.canRelocatePath( "org/foo/bar/Class" ) ); + assertTrue( relocator.canRelocatePath( "org/foo/bar/Class.class" ) ); + assertFalse( relocator.canRelocatePath( "com/foo/bar/Class" ) ); + assertFalse( relocator.canRelocatePath( "com/foo/bar/Class.class" ) ); + assertFalse( relocator.canRelocatePath( "org/Foo/Class" ) ); + assertFalse( relocator.canRelocatePath( "org/Foo/Class.class" ) ); relocator = new SimpleRelocator( "org.foo", null, null, Arrays.asList( "org.foo.Excluded", "org.foo.public.*", "org.foo.Public*Stuff") ); - assertEquals( true, relocator.canRelocatePath( "org/foo/Class" ) ); - assertEquals( true, relocator.canRelocatePath( "org/foo/Class.class" ) ); - assertEquals( true, relocator.canRelocatePath( "org/foo/excluded" ) ); - assertEquals( false, relocator.canRelocatePath( "org/foo/Excluded" ) ); - assertEquals( false, relocator.canRelocatePath( "org/foo/Excluded.class" ) ); - assertEquals( false, relocator.canRelocatePath( "org/foo/public" ) ); - assertEquals( false, relocator.canRelocatePath( "org/foo/public/Class" ) ); - assertEquals( false, relocator.canRelocatePath( "org/foo/public/Class.class" ) ); - assertEquals( true, relocator.canRelocatePath( "org/foo/publicRELOC/Class" ) ); - assertEquals( true, relocator.canRelocatePath( "org/foo/PrivateStuff" ) ); - assertEquals( true, relocator.canRelocatePath( "org/foo/PrivateStuff.class" ) ); - assertEquals( false, relocator.canRelocatePath( "org/foo/PublicStuff" ) ); - assertEquals( false, relocator.canRelocatePath( "org/foo/PublicStuff.class" ) ); - assertEquals( false, relocator.canRelocatePath( "org/foo/PublicUtilStuff" ) ); - assertEquals( false, relocator.canRelocatePath( "org/foo/PublicUtilStuff.class" ) ); + assertTrue( relocator.canRelocatePath( "org/foo/Class" ) ); + assertTrue( relocator.canRelocatePath( "org/foo/Class.class" ) ); + assertTrue( relocator.canRelocatePath( "org/foo/excluded" ) ); + assertFalse( relocator.canRelocatePath( "org/foo/Excluded" ) ); + assertFalse( relocator.canRelocatePath( "org/foo/Excluded.class" ) ); + assertFalse( relocator.canRelocatePath( "org/foo/public" ) ); + assertFalse( relocator.canRelocatePath( "org/foo/public/Class" ) ); + assertFalse( relocator.canRelocatePath( "org/foo/public/Class.class" ) ); + assertTrue( relocator.canRelocatePath( "org/foo/publicRELOC/Class" ) ); + assertTrue( relocator.canRelocatePath( "org/foo/PrivateStuff" ) ); + assertTrue( relocator.canRelocatePath( "org/foo/PrivateStuff.class" ) ); + assertFalse( relocator.canRelocatePath( "org/foo/PublicStuff" ) ); + assertFalse( relocator.canRelocatePath( "org/foo/PublicStuff.class" ) ); + assertFalse( relocator.canRelocatePath( "org/foo/PublicUtilStuff" ) ); + assertFalse( relocator.canRelocatePath( "org/foo/PublicUtilStuff.class" ) ); } public void testCanRelocateClass() @@ -73,22 +73,22 @@ public void testCanRelocateClass() SimpleRelocator relocator; relocator = new SimpleRelocator( "org.foo", null, null, null ); - assertEquals( true, relocator.canRelocateClass( "org.foo.Class" ) ); - assertEquals( true, relocator.canRelocateClass( "org.foo.bar.Class" ) ); - assertEquals( false, relocator.canRelocateClass( "com.foo.bar.Class" ) ); - assertEquals( false, relocator.canRelocateClass( "org.Foo.Class" ) ); + assertTrue( relocator.canRelocateClass( "org.foo.Class" ) ); + assertTrue( relocator.canRelocateClass( "org.foo.bar.Class" ) ); + assertFalse( relocator.canRelocateClass( "com.foo.bar.Class" ) ); + assertFalse( relocator.canRelocateClass( "org.Foo.Class" ) ); relocator = new SimpleRelocator( "org.foo", null, null, Arrays.asList( "org.foo.Excluded", "org.foo.public.*", "org.foo.Public*Stuff") ); - assertEquals( true, relocator.canRelocateClass( "org.foo.Class" ) ); - assertEquals( true, relocator.canRelocateClass( "org.foo.excluded" ) ); - assertEquals( false, relocator.canRelocateClass( "org.foo.Excluded" ) ); - assertEquals( false, relocator.canRelocateClass( "org.foo.public" ) ); - assertEquals( false, relocator.canRelocateClass( "org.foo.public.Class" ) ); - assertEquals( true, relocator.canRelocateClass( "org.foo.publicRELOC.Class" ) ); - assertEquals( true, relocator.canRelocateClass( "org.foo.PrivateStuff" ) ); - assertEquals( false, relocator.canRelocateClass( "org.foo.PublicStuff" ) ); - assertEquals( false, relocator.canRelocateClass( "org.foo.PublicUtilStuff" ) ); + assertTrue( relocator.canRelocateClass( "org.foo.Class" ) ); + assertTrue( relocator.canRelocateClass( "org.foo.excluded" ) ); + assertFalse( relocator.canRelocateClass( "org.foo.Excluded" ) ); + assertFalse( relocator.canRelocateClass( "org.foo.public" ) ); + assertFalse( relocator.canRelocateClass( "org.foo.public.Class" ) ); + assertTrue( relocator.canRelocateClass( "org.foo.publicRELOC.Class" ) ); + assertTrue( relocator.canRelocateClass( "org.foo.PrivateStuff" ) ); + assertFalse( relocator.canRelocateClass( "org.foo.PublicStuff" ) ); + assertFalse( relocator.canRelocateClass( "org.foo.PublicUtilStuff" ) ); } public void testCanRelocateRawString() @@ -96,10 +96,10 @@ public void testCanRelocateRawString() SimpleRelocator relocator; relocator = new SimpleRelocator( "org/foo", null, null, null, true ); - assertEquals( true, relocator.canRelocatePath( "(I)org/foo/bar/Class;" ) ); + assertTrue( relocator.canRelocatePath( "(I)org/foo/bar/Class;" ) ); relocator = new SimpleRelocator( "^META-INF/org.foo.xml$", null, null, null, true ); - assertEquals( true, relocator.canRelocatePath( "META-INF/org.foo.xml" ) ); + assertTrue( relocator.canRelocatePath( "META-INF/org.foo.xml" ) ); } //MSHADE-119, make sure that the easy part of this works. @@ -167,12 +167,12 @@ public void testRelocateMavenFiles() SimpleRelocator relocator = new SimpleRelocator( "META-INF/maven", "META-INF/shade/maven", null, Collections.singletonList( "META-INF/maven/com.foo.bar/artifactId/pom.*" ) ); - assertEquals( false, relocator.canRelocatePath( "META-INF/maven/com.foo.bar/artifactId/pom.properties" ) ); - assertEquals( false, relocator.canRelocatePath( "META-INF/maven/com.foo.bar/artifactId/pom.xml" ) ); - assertEquals( true, relocator.canRelocatePath( "META-INF/maven/com/foo/bar/artifactId/pom.properties" ) ); - assertEquals( true, relocator.canRelocatePath( "META-INF/maven/com/foo/bar/artifactId/pom.xml" ) ); - assertEquals( true, relocator.canRelocatePath( "META-INF/maven/com-foo-bar/artifactId/pom.properties" ) ); - assertEquals( true, relocator.canRelocatePath( "META-INF/maven/com-foo-bar/artifactId/pom.xml" ) ); + assertFalse( relocator.canRelocatePath( "META-INF/maven/com.foo.bar/artifactId/pom.properties" ) ); + assertFalse( relocator.canRelocatePath( "META-INF/maven/com.foo.bar/artifactId/pom.xml" ) ); + assertTrue( relocator.canRelocatePath( "META-INF/maven/com/foo/bar/artifactId/pom.properties" ) ); + assertTrue( relocator.canRelocatePath( "META-INF/maven/com/foo/bar/artifactId/pom.xml" ) ); + assertTrue( relocator.canRelocatePath( "META-INF/maven/com-foo-bar/artifactId/pom.properties" ) ); + assertTrue( relocator.canRelocatePath( "META-INF/maven/com-foo-bar/artifactId/pom.xml" ) ); } } diff --git a/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java b/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java index a9f78dbd..bde428af 100644 --- a/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java +++ b/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java @@ -27,6 +27,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; import java.util.jar.JarEntry; @@ -52,7 +53,7 @@ public void relocatedClasses() throws Exception { List relocators = Lists.newArrayList( relocator ); String content = "org.foo.Service\norg.foo.exclude.OtherService\n"; - byte[] contentBytes = content.getBytes( "UTF-8" ); + byte[] contentBytes = content.getBytes( StandardCharsets.UTF_8 ); InputStream contentStream = new ByteArrayInputStream( contentBytes ); String contentResource = "META-INF/services/org.foo.something.another"; String contentResourceShaded = "META-INF/services/borg.foo.something.another"; @@ -96,7 +97,7 @@ public void concatenation() throws Exception { List relocators = Lists.newArrayList( relocator ); String content = "org.foo.Service\n"; - byte[] contentBytes = content.getBytes( "UTF-8" ); + byte[] contentBytes = content.getBytes( StandardCharsets.UTF_8 ); InputStream contentStream = new ByteArrayInputStream( contentBytes ); String contentResource = "META-INF/services/org.something.another"; @@ -105,7 +106,7 @@ public void concatenation() throws Exception { contentStream.close(); content = "org.blah.Service\n"; - contentBytes = content.getBytes( "UTF-8" ); + contentBytes = content.getBytes( StandardCharsets.UTF_8 ); contentStream = new ByteArrayInputStream( contentBytes ); contentResource = "META-INF/services/org.something.another";