diff --git a/pom.xml b/pom.xml index c6b619e7..d0a221d9 100644 --- a/pom.xml +++ b/pom.xml @@ -63,7 +63,7 @@ under the License. - 7 + 8 3.2.5 1.7.5 1.0.0.v20140518 @@ -232,10 +232,6 @@ under the License. deploy - - - ${https.protocols} - diff --git a/src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java b/src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java index 5d950438..11bb2859 100644 --- a/src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java +++ b/src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java @@ -21,12 +21,12 @@ import java.io.File; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.Writer; +import java.nio.file.Files; import java.util.Enumeration; import java.util.Objects; import java.util.jar.JarEntry; @@ -179,14 +179,9 @@ void initProperties() if ( pomFile == null ) { boolean foundPom = false; - - JarFile jarFile = null; - try + try ( JarFile jarFile = new JarFile( file ) ) { Pattern pomEntry = Pattern.compile( "META-INF/maven/.*/pom\\.xml" ); - - jarFile = new JarFile( file ); - Enumeration jarEntries = jarFile.entries(); while ( jarEntries.hasMoreElements() ) @@ -196,41 +191,23 @@ void initProperties() if ( pomEntry.matcher( entry.getName() ).matches() ) { getLog().debug( "Using " + entry.getName() + " as pomFile" ); - foundPom = true; - - InputStream pomInputStream = null; - OutputStream pomOutputStream = null; - - try + String base = file.getName(); + if ( base.indexOf( '.' ) > 0 ) { - pomInputStream = jarFile.getInputStream( entry ); + base = base.substring( 0, base.lastIndexOf( '.' ) ); + } + pomFile = new File( file.getParentFile(), base + ".pom" ); - String base = file.getName(); - if ( base.indexOf( '.' ) > 0 ) + try ( InputStream pomInputStream = jarFile.getInputStream( entry ) ) + { + try ( OutputStream pomOutputStream = Files.newOutputStream( pomFile.toPath() ) ) { - base = base.substring( 0, base.lastIndexOf( '.' ) ); + IOUtil.copy( pomInputStream, pomOutputStream ); } - pomFile = new File( file.getParentFile(), base + ".pom" ); - - pomOutputStream = new FileOutputStream( pomFile ); - - IOUtil.copy( pomInputStream, pomOutputStream ); - - pomOutputStream.close(); - pomOutputStream = null; - pomInputStream.close(); - pomInputStream = null; - processModel( readModel( pomFile ) ); - break; } - finally - { - IOUtil.close( pomInputStream ); - IOUtil.close( pomOutputStream ); - } } } @@ -243,20 +220,6 @@ void initProperties() { // ignore, artifact not packaged by Maven } - finally - { - if ( jarFile != null ) - { - try - { - jarFile.close(); - } - catch ( IOException e ) - { - // we did our best - } - } - } } else { diff --git a/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java b/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java index d8538705..0700dfbf 100644 --- a/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java @@ -25,6 +25,7 @@ import java.io.File; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Model; @@ -43,7 +44,7 @@ public class DeployFileMojoTest extends AbstractMojoTestCase { - private String LOCAL_REPO = getBasedir() + "/target/local-repo"; + private final String LOCAL_REPO = getBasedir() + "/target/local-repo"; private List expectedFiles; @@ -151,14 +152,14 @@ public void testBasicDeployFile() assertEquals( "POM was created from deploy:deploy-file", model.getDescription() ); //check the remote-repo - expectedFiles = new ArrayList(); - fileList = new ArrayList(); + expectedFiles = new ArrayList<>(); + fileList = new ArrayList<>(); File repo = new File( remoteRepo, "deploy-file-test" ); File[] files = repo.listFiles(); - for (File file1 : files) { + for (File file1 : Objects.requireNonNull( files ) ) { addFileToList(file1, fileList); } @@ -285,7 +286,7 @@ private void addFileToList( File file, List fileList ) File[] files = file.listFiles(); - for (File file1 : files) { + for (File file1 : Objects.requireNonNull( files ) ) { addFileToList(file1, fileList); } } diff --git a/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoUnitTest.java b/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoUnitTest.java index a7bbd8b7..0edf72f1 100644 --- a/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoUnitTest.java +++ b/src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoUnitTest.java @@ -70,13 +70,14 @@ public void setModel(Model model) { this.model = model; } - protected Model readModel(File pomFile) throws MojoExecutionException { + protected Model readModel(File pomFile) + { return model; } } @Test - public void testProcessPomFromPomFileWithParent1() throws MojoExecutionException + public void testProcessPomFromPomFileWithParent1() { mojo.setPomFile( new File( "foo.bar" ) ); @@ -92,7 +93,7 @@ public void testProcessPomFromPomFileWithParent1() throws MojoExecutionException } @Test - public void testProcessPomFromPomFileWithParent2() throws MojoExecutionException + public void testProcessPomFromPomFileWithParent2() { mojo.setPomFile( new File( "foo.bar" ) ); setMojoModel( mojo.model, null, "artifact", null, null, parent ); @@ -108,7 +109,7 @@ public void testProcessPomFromPomFileWithParent2() throws MojoExecutionException } @Test - public void testProcessPomFromPomFileWithParent3() throws MojoExecutionException + public void testProcessPomFromPomFileWithParent3() { mojo.setPomFile( new File( "foo.bar" ) ); setMojoModel( mojo.model, null, "artifact", "version", null, parent ); diff --git a/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java b/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java index f34abd06..54bc16fa 100644 --- a/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java @@ -29,6 +29,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; @@ -62,13 +63,13 @@ public class DeployMojoTest private File localRepo; - private String LOCAL_REPO = getBasedir() + "/target/local-repo"; + private final String LOCAL_REPO = getBasedir() + "/target/local-repo"; - private String REMOTE_REPO = getBasedir() + "/target/remote-repo"; + private final String REMOTE_REPO = getBasedir() + "/target/remote-repo"; DeployArtifactStub artifact; - MavenProjectStub project = new MavenProjectStub(); + final MavenProjectStub project = new MavenProjectStub(); private MavenSession session; @@ -180,8 +181,8 @@ public void testBasicDeploy() mojo.execute(); //check the artifact in local repository - List expectedFiles = new ArrayList(); - List fileList = new ArrayList(); + List expectedFiles = new ArrayList<>(); + List fileList = new ArrayList<>(); expectedFiles.add( "org" ); expectedFiles.add( "apache" ); @@ -202,7 +203,7 @@ public void testBasicDeploy() File[] files = localRepo.listFiles(); - for (File file2 : files) { + for (File file2 : Objects.requireNonNull( files ) ) { addFileToList(file2, fileList); } @@ -211,8 +212,8 @@ public void testBasicDeploy() assertEquals( 0, getSizeOfExpectedFiles( fileList, expectedFiles ) ); //check the artifact in remote repository - expectedFiles = new ArrayList(); - fileList = new ArrayList(); + expectedFiles = new ArrayList<>(); + fileList = new ArrayList<>(); expectedFiles.add( "org" ); expectedFiles.add( "apache" ); @@ -238,7 +239,7 @@ public void testBasicDeploy() files = remoteRepo.listFiles(); - for (File file1 : files) { + for (File file1 : Objects.requireNonNull( files ) ) { addFileToList(file1, fileList); } @@ -349,8 +350,8 @@ public void testBasicDeployWithPackagingAsPom() mojo.execute(); - List expectedFiles = new ArrayList(); - List fileList = new ArrayList(); + List expectedFiles = new ArrayList<>(); + List fileList = new ArrayList<>(); expectedFiles.add( "org" ); expectedFiles.add( "apache" ); @@ -372,7 +373,7 @@ public void testBasicDeployWithPackagingAsPom() File[] files = remoteRepo.listFiles(); - for (File file : files) { + for (File file : Objects.requireNonNull( files ) ) { addFileToList(file, fileList); } @@ -467,8 +468,8 @@ public void testDeployWithAttachedArtifacts() mojo.execute(); //check the artifacts in remote repository - List expectedFiles = new ArrayList(); - List fileList = new ArrayList(); + List expectedFiles = new ArrayList<>(); + List fileList = new ArrayList<>(); expectedFiles.add( "org" ); expectedFiles.add( "apache" ); @@ -506,7 +507,7 @@ public void testDeployWithAttachedArtifacts() File[] files = remoteRepo.listFiles(); - for (File file1 : files) { + for (File file1 : Objects.requireNonNull( files ) ) { addFileToList(file1, fileList); } @@ -720,7 +721,7 @@ private void addFileToList( File file, List fileList ) File[] files = file.listFiles(); - for (File file1 : files) { + for (File file1 : Objects.requireNonNull( files ) ) { addFileToList(file1, fileList); } } diff --git a/src/test/java/org/apache/maven/plugins/deploy/Utils.java b/src/test/java/org/apache/maven/plugins/deploy/Utils.java index f4acbc9d..be963420 100644 --- a/src/test/java/org/apache/maven/plugins/deploy/Utils.java +++ b/src/test/java/org/apache/maven/plugins/deploy/Utils.java @@ -30,6 +30,7 @@ /** * A utility class to assist testing. + * used in IntegrationTests like attach-jar-checksum-snapshot, attach-jar-checksum-snapshot * * @author Benjamin Bentmann */ diff --git a/src/test/java/org/apache/maven/plugins/deploy/stubs/ArtifactRepositoryStub.java b/src/test/java/org/apache/maven/plugins/deploy/stubs/ArtifactRepositoryStub.java index 92db6c19..238c5f79 100644 --- a/src/test/java/org/apache/maven/plugins/deploy/stubs/ArtifactRepositoryStub.java +++ b/src/test/java/org/apache/maven/plugins/deploy/stubs/ArtifactRepositoryStub.java @@ -36,7 +36,7 @@ public class ArtifactRepositoryStub private String url; - private String basedir = System.getProperty( "basedir" ); + private final String basedir = System.getProperty( "basedir" ); public ArtifactRepositoryStub() { diff --git a/src/test/java/org/apache/maven/plugins/deploy/stubs/DeployArtifactStub.java b/src/test/java/org/apache/maven/plugins/deploy/stubs/DeployArtifactStub.java index 24f31d58..8993ed99 100644 --- a/src/test/java/org/apache/maven/plugins/deploy/stubs/DeployArtifactStub.java +++ b/src/test/java/org/apache/maven/plugins/deploy/stubs/DeployArtifactStub.java @@ -101,7 +101,7 @@ public void addMetadata( ArtifactMetadata metadata ) { if ( metadataMap == null ) { - metadataMap = new HashMap(); + metadataMap = new HashMap<>(); } ArtifactMetadata m = metadataMap.get( metadata.getKey() ); @@ -117,7 +117,7 @@ public void addMetadata( ArtifactMetadata metadata ) public Collection getMetadataList() { - return metadataMap == null ? Collections.emptyList() : metadataMap.values(); + return metadataMap == null ? Collections.emptyList() : metadataMap.values(); } public boolean isRelease()