diff --git a/pom.xml b/pom.xml index 6e47a1a..b5b9b1a 100644 --- a/pom.xml +++ b/pom.xml @@ -85,21 +85,11 @@ - - org.codehaus.plexus - plexus-io - 3.4.1 - org.codehaus.plexus plexus-archiver 4.8.0 - - org.codehaus.plexus - plexus-utils - 3.5.1 - org.codehaus.plexus plexus-interpolation diff --git a/src/main/java/org/apache/maven/archiver/MavenArchiver.java b/src/main/java/org/apache/maven/archiver/MavenArchiver.java index 232decc..cab5ae9 100644 --- a/src/main/java/org/apache/maven/archiver/MavenArchiver.java +++ b/src/main/java/org/apache/maven/archiver/MavenArchiver.java @@ -56,7 +56,6 @@ import org.codehaus.plexus.interpolation.RecursionInterceptor; import org.codehaus.plexus.interpolation.StringSearchInterpolator; import org.codehaus.plexus.interpolation.ValueSource; -import org.codehaus.plexus.util.StringUtils; import static org.apache.maven.archiver.ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_CUSTOM; import static org.apache.maven.archiver.ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_REPOSITORY; @@ -390,9 +389,10 @@ private void handleExtraExpression(Artifact artifact, List valueSou } extraExpressions.setProperty("groupIdPath", artifact.getGroupId().replace('.', '/')); - if (StringUtils.isNotEmpty(artifact.getClassifier())) { - extraExpressions.setProperty("dashClassifier", "-" + artifact.getClassifier()); - extraExpressions.setProperty("dashClassifier?", "-" + artifact.getClassifier()); + String classifier = artifact.getClassifier(); + if (classifier != null && !classifier.isEmpty()) { + extraExpressions.setProperty("dashClassifier", "-" + classifier); + extraExpressions.setProperty("dashClassifier?", "-" + classifier); } else { extraExpressions.setProperty("dashClassifier", ""); extraExpressions.setProperty("dashClassifier?", ""); @@ -772,7 +772,7 @@ public static Optional parseBuildOutputTimestamp(String outputTimestamp } // Number representing seconds since the epoch - if ((outputTimestamp != null && !outputTimestamp.isEmpty()) && StringUtils.isNumeric(outputTimestamp)) { + if (isNumeric(outputTimestamp)) { return Optional.of(Instant.ofEpochSecond(Long.parseLong(outputTimestamp))); } @@ -800,6 +800,21 @@ public static Optional parseBuildOutputTimestamp(String outputTimestamp } } + private static boolean isNumeric(String str) { + + if (str.isEmpty()) { + return false; + } + + for (char c : str.toCharArray()) { + if (!Character.isDigit(c)) { + return false; + } + } + + return true; + } + /** * Configure Reproducible Builds archive creation if a timestamp is provided. * diff --git a/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java b/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java index e2c27c4..2ad0176 100644 --- a/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java +++ b/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java @@ -23,6 +23,10 @@ import java.io.InputStream; import java.net.URI; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.attribute.FileTime; import java.time.Instant; import java.time.format.DateTimeParseException; import java.util.ArrayList; @@ -38,6 +42,7 @@ import java.util.jar.Attributes; import java.util.jar.JarFile; import java.util.jar.Manifest; +import java.util.stream.Stream; import java.util.zip.ZipEntry; import org.apache.maven.artifact.Artifact; @@ -55,8 +60,6 @@ import org.apache.maven.project.MavenProject; import org.codehaus.plexus.archiver.jar.JarArchiver; import org.codehaus.plexus.archiver.jar.ManifestException; -import org.codehaus.plexus.util.FileUtils; -import org.codehaus.plexus.util.StringUtils; import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.RepositorySystemSession; import org.junit.jupiter.api.Test; @@ -69,6 +72,7 @@ import org.junit.jupiter.params.provider.ValueSource; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; class MavenArchiverTest { @@ -239,8 +243,13 @@ void testRecreation() throws Exception { MavenArchiveConfiguration config = new MavenArchiveConfiguration(); config.setForced(false); - File directory = new File("target/maven-archiver"); - org.apache.commons.io.FileUtils.deleteDirectory(directory); + Path directory = Paths.get("target", "maven-archiver"); + if (Files.exists(directory)) { + try (Stream paths = Files.walk(directory)) { + paths.sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); + } + } + archiver.createArchive(session, project, config); assertThat(jarFile).exists(); @@ -248,9 +257,10 @@ void testRecreation() throws Exception { jarFile.setLastModified(history); long time = jarFile.lastModified(); - List files = FileUtils.getFiles(directory, "**/**", null, true); - for (File file : files) { - assertThat(file.setLastModified(time)).isTrue(); + try (Stream paths = Files.walk(directory)) { + FileTime fileTime = FileTime.fromMillis(time); + paths.forEach(path -> assertThatCode(() -> Files.setLastModifiedTime(path, fileTime)) + .doesNotThrowAnyException()); } archiver.createArchive(session, project, config); @@ -527,8 +537,7 @@ void testManifestEntries() throws Exception { assertThat(manifest) .containsEntry(new Attributes.Name("Build-Jdk-Spec"), System.getProperty("java.specification.version")); - assertThat(StringUtils.isEmpty(manifest.getValue(new Attributes.Name("keyWithEmptyValue")))) - .isTrue(); + assertThat(manifest.getValue(new Attributes.Name("keyWithEmptyValue"))).isEmpty(); assertThat(manifest).containsKey(new Attributes.Name("keyWithEmptyValue")); manifest = jarFileManifest.getAttributes("UserSection"); @@ -616,8 +625,8 @@ void testDefaultClassPathValue() throws Exception { final Manifest manifest = getJarFileManifest(jarFile); String classPath = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH); assertThat(classPath).isNotNull(); - assertThat(StringUtils.split(classPath, " ")) - .containsExactly("dummy1-1.0.jar", "dummy2-1.5.jar", "dummy3-2.0.jar"); + assertThat(classPath.split(" ")) + .containsExactly("dummy1-1.0.jar", "dummy2-1.5.jar", "dummy3-2.0-classifier.jar"); } private void deleteAndAssertNotPresent(File jarFile) { @@ -650,8 +659,8 @@ void testDefaultClassPathValue_WithSnapshot() throws Exception { final Manifest manifest = getJarFileManifest(jarFile); String classPath = manifest.getMainAttributes().getValue(Attributes.Name.CLASS_PATH); assertThat(classPath).isNotNull(); - assertThat(StringUtils.split(classPath, " ")) - .containsExactly("dummy1-1.1-20081022.112233-1.jar", "dummy2-1.5.jar", "dummy3-2.0.jar"); + assertThat(classPath.split(" ")) + .containsExactly("dummy1-1.1-20081022.112233-1.jar", "dummy2-1.5.jar", "dummy3-2.0-classifier.jar"); } @Test @@ -674,21 +683,21 @@ void testMavenRepoClassPathValue() throws Exception { archiver.createArchive(session, project, config); assertThat(jarFile).exists(); Manifest manifest = archiver.getManifest(session, project, config); - String[] classPathEntries = StringUtils.split( - new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()), " "); + String[] classPathEntries = + new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()).split(" "); assertThat(classPathEntries) .containsExactly( "org/apache/dummy/dummy1/1.0.1/dummy1-1.0.jar", "org/apache/dummy/foo/dummy2/1.5/dummy2-1.5.jar", - "org/apache/dummy/bar/dummy3/2.0/dummy3-2.0.jar"); + "org/apache/dummy/bar/dummy3/2.0/dummy3-2.0-classifier.jar"); String classPath = getJarFileManifest(jarFile).getMainAttributes().getValue(Attributes.Name.CLASS_PATH); assertThat(classPath).isNotNull(); - assertThat(StringUtils.split(classPath, " ")) + assertThat(classPath.split(" ")) .containsExactly( "org/apache/dummy/dummy1/1.0.1/dummy1-1.0.jar", "org/apache/dummy/foo/dummy2/1.5/dummy2-1.5.jar", - "org/apache/dummy/bar/dummy3/2.0/dummy3-2.0.jar"); + "org/apache/dummy/bar/dummy3/2.0/dummy3-2.0-classifier.jar"); } @Test @@ -712,15 +721,16 @@ void shouldCreateArchiveWithSimpleClassPathLayoutWhileSettingSimpleLayoutExplici archiver.createArchive(session, project, config); assertThat(jarFile).exists(); Manifest manifest = archiver.getManifest(session, project, config); - String[] classPathEntries = StringUtils.split( - new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()), " "); - assertThat(classPathEntries).containsExactly("lib/dummy1-1.0.jar", "lib/dummy2-1.5.jar", "lib/dummy3-2.0.jar"); + String[] classPathEntries = + new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()).split(" "); + assertThat(classPathEntries) + .containsExactly("lib/dummy1-1.0.jar", "lib/dummy2-1.5.jar", "lib/dummy3-2.0-classifier.jar"); String classPath = getJarFileManifest(jarFile).getMainAttributes().getValue(Attributes.Name.CLASS_PATH); assertThat(classPath).isNotNull(); - assertThat(StringUtils.split(classPath, " ")) - .containsExactly("lib/dummy1-1.0.jar", "lib/dummy2-1.5.jar", "lib/dummy3-2.0.jar"); + assertThat(classPath.split(" ")) + .containsExactly("lib/dummy1-1.0.jar", "lib/dummy2-1.5.jar", "lib/dummy3-2.0-classifier.jar"); } @Test @@ -745,15 +755,16 @@ void shouldCreateArchiveCustomerLayoutSimple() throws Exception { archiver.createArchive(session, project, config); assertThat(jarFile).exists(); Manifest manifest = archiver.getManifest(session, project, config); - String[] classPathEntries = StringUtils.split( - new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()), " "); - assertThat(classPathEntries).containsExactly("lib/dummy1-1.0.jar", "lib/dummy2-1.5.jar", "lib/dummy3-2.0.jar"); + String[] classPathEntries = + new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()).split(" "); + assertThat(classPathEntries) + .containsExactly("lib/dummy1-1.0.jar", "lib/dummy2-1.5.jar", "lib/dummy3-2.0-classifier.jar"); String classPath = getJarFileManifest(jarFile).getMainAttributes().getValue(Attributes.Name.CLASS_PATH); assertThat(classPath).isNotNull(); - assertThat(StringUtils.split(classPath, " ")) - .containsExactly("lib/dummy1-1.0.jar", "lib/dummy2-1.5.jar", "lib/dummy3-2.0.jar"); + assertThat(classPath.split(" ")) + .containsExactly("lib/dummy1-1.0.jar", "lib/dummy2-1.5.jar", "lib/dummy3-2.0-classifier.jar"); } @Test @@ -778,16 +789,16 @@ void shouldCreateArchiveCustomLayoutSimpleNonUnique() throws Exception { archiver.createArchive(session, project, config); assertThat(jarFile).exists(); Manifest manifest = archiver.getManifest(session, project, config); - String[] classPathEntries = StringUtils.split( - new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()), " "); + String[] classPathEntries = + new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()).split(" "); assertThat(classPathEntries) - .containsExactly("lib/dummy1-1.0.1.jar", "lib/dummy2-1.5.jar", "lib/dummy3-2.0.jar"); + .containsExactly("lib/dummy1-1.0.1.jar", "lib/dummy2-1.5.jar", "lib/dummy3-2.0-classifier.jar"); String classPath = getJarFileManifest(jarFile).getMainAttributes().getValue(Attributes.Name.CLASS_PATH); assertThat(classPath).isNotNull(); - assertThat(StringUtils.split(classPath, " ")) - .containsExactly("lib/dummy1-1.0.1.jar", "lib/dummy2-1.5.jar", "lib/dummy3-2.0.jar"); + assertThat(classPath.split(" ")) + .containsExactly("lib/dummy1-1.0.1.jar", "lib/dummy2-1.5.jar", "lib/dummy3-2.0-classifier.jar"); } @Test @@ -812,22 +823,22 @@ void shouldCreateArchiveCustomLayoutRepository() throws Exception { archiver.createArchive(session, project, config); assertThat(jarFile).exists(); Manifest manifest = archiver.getManifest(session, project, config); - String[] classPathEntries = StringUtils.split( - new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()), " "); + String[] classPathEntries = + new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()).split(" "); assertThat(classPathEntries) .containsExactly( "lib/org/apache/dummy/dummy1/1.0.1/dummy1-1.0.jar", "lib/org/apache/dummy/foo/dummy2/1.5/dummy2-1.5.jar", - "lib/org/apache/dummy/bar/dummy3/2.0/dummy3-2.0.jar"); + "lib/org/apache/dummy/bar/dummy3/2.0/dummy3-2.0-classifier.jar"); String classPath = getJarFileManifest(jarFile).getMainAttributes().getValue(Attributes.Name.CLASS_PATH); assertThat(classPath).isNotNull(); - assertThat(StringUtils.split(classPath, " ")) + assertThat(classPath.split(" ")) .containsExactly( "lib/org/apache/dummy/dummy1/1.0.1/dummy1-1.0.jar", "lib/org/apache/dummy/foo/dummy2/1.5/dummy2-1.5.jar", - "lib/org/apache/dummy/bar/dummy3/2.0/dummy3-2.0.jar"); + "lib/org/apache/dummy/bar/dummy3/2.0/dummy3-2.0-classifier.jar"); } @Test @@ -852,22 +863,22 @@ void shouldCreateArchiveCustomLayoutRepositoryNonUnique() throws Exception { archiver.createArchive(session, project, config); assertThat(jarFile).exists(); Manifest manifest = archiver.getManifest(session, project, config); - String[] classPathEntries = StringUtils.split( - new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()), " "); + String[] classPathEntries = + new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()).split(" "); assertThat(classPathEntries) .containsExactly( "lib/org/apache/dummy/dummy1/1.0.1/dummy1-1.0.1.jar", "lib/org/apache/dummy/foo/dummy2/1.5/dummy2-1.5.jar", - "lib/org/apache/dummy/bar/dummy3/2.0/dummy3-2.0.jar"); + "lib/org/apache/dummy/bar/dummy3/2.0/dummy3-2.0-classifier.jar"); String classPath = getJarFileManifest(jarFile).getMainAttributes().getValue(Attributes.Name.CLASS_PATH); assertThat(classPath).isNotNull(); - assertThat(StringUtils.split(classPath, " ")) + assertThat(classPath.split(" ")) .containsExactly( "lib/org/apache/dummy/dummy1/1.0.1/dummy1-1.0.1.jar", "lib/org/apache/dummy/foo/dummy2/1.5/dummy2-1.5.jar", - "lib/org/apache/dummy/bar/dummy3/2.0/dummy3-2.0.jar"); + "lib/org/apache/dummy/bar/dummy3/2.0/dummy3-2.0-classifier.jar"); } @Test @@ -890,14 +901,15 @@ void shouldCreateArchiveWithSimpleClassPathLayoutUsingDefaults() throws Exceptio archiver.createArchive(session, project, config); assertThat(jarFile).exists(); Manifest manifest = archiver.getManifest(session, project, config); - String[] classPathEntries = StringUtils.split( - new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()), " "); - assertThat(classPathEntries).containsExactly("lib/dummy1-1.0.jar", "lib/dummy2-1.5.jar", "lib/dummy3-2.0.jar"); + String[] classPathEntries = + new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()).split(" "); + assertThat(classPathEntries) + .containsExactly("lib/dummy1-1.0.jar", "lib/dummy2-1.5.jar", "lib/dummy3-2.0-classifier.jar"); String classPath = getJarFileManifest(jarFile).getMainAttributes().getValue(Attributes.Name.CLASS_PATH); assertThat(classPath).isNotNull(); - assertThat(StringUtils.split(classPath, " ")) - .containsExactly("lib/dummy1-1.0.jar", "lib/dummy2-1.5.jar", "lib/dummy3-2.0.jar"); + assertThat(classPath.split(" ")) + .containsExactly("lib/dummy1-1.0.jar", "lib/dummy2-1.5.jar", "lib/dummy3-2.0-classifier.jar"); } @Test @@ -920,21 +932,21 @@ void testMavenRepoClassPathValue_WithSnapshot() throws Exception { assertThat(jarFile).exists(); Manifest manifest = archiver.getManifest(session, project, config); - String[] classPathEntries = StringUtils.split( - new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()), " "); + String[] classPathEntries = + new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()).split(" "); assertThat(classPathEntries) .containsExactly( "org/apache/dummy/dummy1/1.1-SNAPSHOT/dummy1-1.1-20081022.112233-1.jar", "org/apache/dummy/foo/dummy2/1.5/dummy2-1.5.jar", - "org/apache/dummy/bar/dummy3/2.0/dummy3-2.0.jar"); + "org/apache/dummy/bar/dummy3/2.0/dummy3-2.0-classifier.jar"); String classPath = getJarFileManifest(jarFile).getMainAttributes().getValue(Attributes.Name.CLASS_PATH); assertThat(classPath).isNotNull(); - assertThat(StringUtils.split(classPath, " ")) + assertThat(classPath.split(" ")) .containsExactly( "org/apache/dummy/dummy1/1.1-SNAPSHOT/dummy1-1.1-20081022.112233-1.jar", "org/apache/dummy/foo/dummy2/1.5/dummy2-1.5.jar", - "org/apache/dummy/bar/dummy3/2.0/dummy3-2.0.jar"); + "org/apache/dummy/bar/dummy3/2.0/dummy3-2.0-classifier.jar"); } @Test @@ -959,22 +971,22 @@ void testCustomClassPathValue() throws Exception { archiver.createArchive(session, project, config); assertThat(jarFile).exists(); Manifest manifest = archiver.getManifest(session, project, config); - String[] classPathEntries = StringUtils.split( - new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()), " "); + String[] classPathEntries = + new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()).split(" "); assertThat(classPathEntries) .containsExactly( "org/apache/dummy/dummy1/1.0/TEST-dummy1-1.0.jar", "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", - "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar"); + "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0-classifier.jar"); final Manifest manifest1 = getJarFileManifest(jarFile); String classPath = manifest1.getMainAttributes().getValue(Attributes.Name.CLASS_PATH); assertThat(classPath).isNotNull(); - assertThat(StringUtils.split(classPath, " ")) + assertThat(classPath.split(" ")) .containsExactly( "org/apache/dummy/dummy1/1.0/TEST-dummy1-1.0.jar", "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", - "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar"); + "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0-classifier.jar"); } @Test @@ -999,21 +1011,21 @@ void testCustomClassPathValue_WithSnapshotResolvedVersion() throws Exception { assertThat(jarFile).exists(); Manifest manifest = archiver.getManifest(session, project, config); - String[] classPathEntries = StringUtils.split( - new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()), " "); + String[] classPathEntries = + new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()).split(" "); assertThat(classPathEntries) .containsExactly( "org/apache/dummy/dummy1/1.1-SNAPSHOT/TEST-dummy1-1.1-20081022.112233-1.jar", "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", - "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar"); + "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0-classifier.jar"); String classPath = getJarFileManifest(jarFile).getMainAttributes().getValue(Attributes.Name.CLASS_PATH); assertThat(classPath).isNotNull(); - assertThat(StringUtils.split(classPath, " ")) + assertThat(classPath.split(" ")) .containsExactly( "org/apache/dummy/dummy1/1.1-SNAPSHOT/TEST-dummy1-1.1-20081022.112233-1.jar", "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", - "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar"); + "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0-classifier.jar"); } @Test @@ -1038,19 +1050,19 @@ void testCustomClassPathValue_WithSnapshotForcingBaseVersion() throws Exception archiver.createArchive(session, project, config); assertThat(jarFile).exists(); Manifest manifest = archiver.getManifest(session, project, config); - String[] classPathEntries = StringUtils.split( - new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()), " "); + String[] classPathEntries = + new String(manifest.getMainAttributes().getValue("Class-Path").getBytes()).split(" "); assertThat(classPathEntries[0]).isEqualTo("org/apache/dummy/dummy1/1.1-SNAPSHOT/TEST-dummy1-1.1-SNAPSHOT.jar"); assertThat(classPathEntries[1]).isEqualTo("org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar"); - assertThat(classPathEntries[2]).isEqualTo("org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar"); + assertThat(classPathEntries[2]).isEqualTo("org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0-classifier.jar"); String classPath = getJarFileManifest(jarFile).getMainAttributes().getValue(Attributes.Name.CLASS_PATH); assertThat(classPath).isNotNull(); - assertThat(StringUtils.split(classPath, " ")) + assertThat(classPath.split(" ")) .containsExactly( "org/apache/dummy/dummy1/1.1-SNAPSHOT/TEST-dummy1-1.1-SNAPSHOT.jar", "org/apache/dummy/foo/dummy2/1.5/TEST-dummy2-1.5.jar", - "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0.jar"); + "org/apache/dummy/bar/dummy3/2.0/TEST-dummy3-2.0-classifier.jar"); } @Test @@ -1185,6 +1197,7 @@ private MockArtifact getMockArtifact3() { artifact3.setVersion("2.0"); artifact3.setScope("runtime"); artifact3.setType("jar"); + artifact3.setClassifier("classifier"); artifact3.setFile(getClasspathFile(artifact3.getArtifactId() + "-" + artifact3.getVersion() + ".jar")); return artifact3; }