gnodet-bot commented on code in PR #618: URL: https://github.com/apache/maven-jar-plugin/pull/618#discussion_r4259045478
########## src/main/java/org/apache/maven/plugins/jar/ManifestBuilder.java: ########## @@ -0,0 +1,422 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.plugins.jar; + +import java.nio.file.Path; +import java.util.Map; +import java.util.jar.Attributes; +import java.util.jar.Manifest; + +import org.apache.maven.api.Dependency; +import org.apache.maven.api.PathScope; +import org.apache.maven.api.Project; +import org.apache.maven.api.Session; +import org.apache.maven.api.model.Model; +import org.apache.maven.api.model.Plugin; +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.services.DependencyResolver; +import org.apache.maven.api.services.DependencyResolverResult; +import org.apache.maven.api.services.Interpolator; +import org.apache.maven.api.xml.XmlNode; + +/** + * Builds a {@link Manifest} from the Maven project and plugin configuration, + * without depending on {@code maven-archiver} or {@code plexus-archiver}. + * + * <p>This class replaces the use of {@code MavenArchiver#getManifest} + * in this plugin, allowing removal of the {@code maven-archiver} and {@code plexus-archiver} dependencies.</p> + */ +final class ManifestBuilder { + + private static final String CLASSPATH_LAYOUT_TYPE_SIMPLE = "simple"; + private static final String CLASSPATH_LAYOUT_TYPE_REPOSITORY = "repository"; + private static final String CLASSPATH_LAYOUT_TYPE_CUSTOM = "custom"; + + private static final String SIMPLE_LAYOUT = + "${artifact.artifactId}-${artifact.version}${dashClassifier?}.${artifact.extension}"; + private static final String SIMPLE_LAYOUT_NONUNIQUE = + "${artifact.artifactId}-${artifact.baseVersion}${dashClassifier?}.${artifact.extension}"; + private static final String REPOSITORY_LAYOUT = + "${artifact.groupIdPath}/${artifact.artifactId}/${artifact.baseVersion}/${artifact.artifactId}" + + "-${artifact.version}${dashClassifier?}.${artifact.extension}"; + private static final String REPOSITORY_LAYOUT_NONUNIQUE = + "${artifact.groupIdPath}/${artifact.artifactId}/${artifact.baseVersion}/${artifact.artifactId}" + + "-${artifact.baseVersion}${dashClassifier?}.${artifact.extension}"; + + private final Session session; + private final Project project; + private final ArchiveConfiguration archive; + + /** + * The {@code Created-By} manifest value, e.g. {@code "Maven JAR Plugin 4.0.0"}. + */ + private String createdBy; + + /** + * Whether to add the {@code Build-Jdk-Spec} entry (from {@code java.specification.version}) + * when {@link ArchiveConfiguration.ManifestConfig#isAddBuildEnvironmentEntries()} is {@code true}. Review Comment: ⚠️ **Stale `@link`:** `ArchiveConfiguration.ManifestConfig#isAddBuildEnvironmentEntries()` does not exist — `ManifestConfig` exposes `addBuildEnvironmentEntries` as a bare package-private field with no generated getter. Either update the `@link` to reference the field directly (e.g. `{@link ArchiveConfiguration.ManifestConfig#addBuildEnvironmentEntries}`) or add the accessor. As-is, javadoc will emit a broken-link warning. ########## src/test/java/org/apache/maven/plugins/jar/ArchiveConfigurationTest.java: ########## @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.plugins.jar; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Unit tests for {@link ArchiveConfiguration} and its nested types. + */ +class ArchiveConfigurationTest { + + // ----------------------------------------------------------------------- + // ArchiveConfiguration — defaults + // ----------------------------------------------------------------------- + + @Test + void defaultsAreCorrect() { + ArchiveConfiguration config = new ArchiveConfiguration(); + assertTrue(config.compress, "compress should default to true"); + assertTrue(config.addMavenDescriptor, "addMavenDescriptor should default to true"); + assertTrue(config.forced, "forced should default to true"); + assertNull(config.manifestFile, "manifestFile should default to null"); + assertNull(config.pomPropertiesFile, "pomPropertiesFile should default to null"); + assertTrue(config.manifestEntries.isEmpty(), "manifestEntries should be empty by default"); + assertTrue(config.manifestSections.isEmpty(), "manifestSections should be empty by default"); + } + + @Test + void manifestLazyInitReturnsSameInstance() { + ArchiveConfiguration config = new ArchiveConfiguration(); + ArchiveConfiguration.ManifestConfig first = config.getManifest(); + assertNotNull(first, "getManifest() should never return null"); + assertNotSame(new ArchiveConfiguration().getManifest(), first, "different instances for different configs"); + // second call on same config returns the same object + assertEquals(first, config.getManifest(), "getManifest() should return the same instance on repeated calls"); + } + + // ----------------------------------------------------------------------- + // ArchiveConfiguration — manifest entries + // ----------------------------------------------------------------------- + + @Test + void manifestEntriesDirectAccess() { + ArchiveConfiguration config = new ArchiveConfiguration(); + assertTrue(config.manifestEntries.isEmpty()); + config.manifestEntries.put("My-Key", "my-value"); + assertFalse(config.manifestEntries.isEmpty()); + assertEquals("my-value", config.manifestEntries.get("My-Key")); + } + + @Test + void manifestEntriesOverwrite() { + ArchiveConfiguration config = new ArchiveConfiguration(); + config.manifestEntries.put("Key", "v1"); + config.manifestEntries.put("Key", "v2"); + assertEquals("v2", config.manifestEntries.get("Key")); + assertEquals(1, config.manifestEntries.size()); + } + + // ----------------------------------------------------------------------- + // ArchiveConfiguration — manifest sections + // ----------------------------------------------------------------------- + + @Test + void manifestSectionsEmptyByDefault() { + ArchiveConfiguration config = new ArchiveConfiguration(); + assertTrue(config.manifestSections.isEmpty()); + } + + @Test + void manifestSectionDirectFieldAccess() { + ArchiveConfiguration.ManifestSection section = new ArchiveConfiguration.ManifestSection(); + assertNull(section.name, "name should default to null"); + section.name = "my-section"; + assertEquals("my-section", section.name); + assertTrue(section.manifestEntries.isEmpty()); + section.manifestEntries.put("k", "v"); + assertEquals("v", section.manifestEntries.get("k")); + } + + // ----------------------------------------------------------------------- + // ManifestConfig — defaults + // ----------------------------------------------------------------------- + + @Test + void manifestConfigDefaults() { + ArchiveConfiguration.ManifestConfig manifest = new ArchiveConfiguration.ManifestConfig(); + assertNull(manifest.mainClass); + assertFalse(manifest.addClasspath); + assertEquals("", manifest.classpathPrefix); + assertTrue(manifest.addDefaultEntries); + assertFalse(manifest.addBuildEnvironmentEntries); + assertFalse(manifest.addDefaultSpecificationEntries); + assertFalse(manifest.addDefaultImplementationEntries); + assertEquals("simple", manifest.classpathLayoutType); + assertNull(manifest.customClasspathLayout); + assertTrue(manifest.useUniqueVersions); + } +} Review Comment: 💡 **Missing unit tests for `ManifestBuilder`:** `ArchiveConfigurationTest` only covers default field values of `ArchiveConfiguration` and its nested types. `ManifestBuilder` — the core new class in this PR — has zero dedicated tests. Consider adding a `ManifestBuilderTest` that covers at least: - `Build-Jdk-Spec` present/absent based on `addBuildEnvironmentEntries` flag - `Class-Path` generation for the simple and repository layout types - Extra manifest entries precedence (user-supplied value prepends computed `Class-Path`) - Manifest section with a null/empty name (NPE guard from above) New classes extracted from legacy code are particularly prone to subtle behavioural differences — tests here would act as a regression net. ########## src/main/java/org/apache/maven/plugins/jar/ManifestBuilder.java: ########## @@ -0,0 +1,422 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.maven.plugins.jar; + +import java.nio.file.Path; +import java.util.Map; +import java.util.jar.Attributes; +import java.util.jar.Manifest; + +import org.apache.maven.api.Dependency; +import org.apache.maven.api.PathScope; +import org.apache.maven.api.Project; +import org.apache.maven.api.Session; +import org.apache.maven.api.model.Model; +import org.apache.maven.api.model.Plugin; +import org.apache.maven.api.plugin.MojoException; +import org.apache.maven.api.services.DependencyResolver; +import org.apache.maven.api.services.DependencyResolverResult; +import org.apache.maven.api.services.Interpolator; +import org.apache.maven.api.xml.XmlNode; + +/** + * Builds a {@link Manifest} from the Maven project and plugin configuration, + * without depending on {@code maven-archiver} or {@code plexus-archiver}. + * + * <p>This class replaces the use of {@code MavenArchiver#getManifest} + * in this plugin, allowing removal of the {@code maven-archiver} and {@code plexus-archiver} dependencies.</p> + */ +final class ManifestBuilder { + + private static final String CLASSPATH_LAYOUT_TYPE_SIMPLE = "simple"; + private static final String CLASSPATH_LAYOUT_TYPE_REPOSITORY = "repository"; + private static final String CLASSPATH_LAYOUT_TYPE_CUSTOM = "custom"; + + private static final String SIMPLE_LAYOUT = + "${artifact.artifactId}-${artifact.version}${dashClassifier?}.${artifact.extension}"; + private static final String SIMPLE_LAYOUT_NONUNIQUE = + "${artifact.artifactId}-${artifact.baseVersion}${dashClassifier?}.${artifact.extension}"; + private static final String REPOSITORY_LAYOUT = + "${artifact.groupIdPath}/${artifact.artifactId}/${artifact.baseVersion}/${artifact.artifactId}" + + "-${artifact.version}${dashClassifier?}.${artifact.extension}"; + private static final String REPOSITORY_LAYOUT_NONUNIQUE = + "${artifact.groupIdPath}/${artifact.artifactId}/${artifact.baseVersion}/${artifact.artifactId}" + + "-${artifact.baseVersion}${dashClassifier?}.${artifact.extension}"; + + private final Session session; + private final Project project; + private final ArchiveConfiguration archive; + + /** + * The {@code Created-By} manifest value, e.g. {@code "Maven JAR Plugin 4.0.0"}. + */ + private String createdBy; + + /** + * Whether to add the {@code Build-Jdk-Spec} entry (from {@code java.specification.version}) + * when {@link ArchiveConfiguration.ManifestConfig#isAddBuildEnvironmentEntries()} is {@code true}. + * Set by {@code AbstractJarMojo} to the value of that flag. + */ + private boolean buildJdkSpecDefaultEntry = true; + + ManifestBuilder(Session session, Project project, ArchiveConfiguration archive) { + this.session = session; + this.project = project; + this.archive = archive; + } + + void setCreatedBy(String createdBy) { + this.createdBy = createdBy; + } + + void setBuildJdkSpecDefaultEntry(boolean buildJdkSpecDefaultEntry) { + this.buildJdkSpecDefaultEntry = buildJdkSpecDefaultEntry; + } + + /** + * Builds the manifest. + * + * @return the manifest, never {@code null} + * @throws MojoException if the manifest cannot be built + */ + Manifest build() { + ArchiveConfiguration.ManifestConfig config = archive.getManifest(); + Map<String, String> extraEntries = archive.manifestEntries.isEmpty() ? Map.of() : archive.manifestEntries; + + Manifest manifest = buildManifest(config, extraEntries); + + // Merge any extra entries from <archive><manifestEntries> + if (!extraEntries.isEmpty()) { + Attributes main = manifest.getMainAttributes(); + for (Map.Entry<String, String> entry : extraEntries.entrySet()) { + String key = entry.getKey(); + String value = sanitize(entry.getValue()); + if (key.equals(Attributes.Name.CLASS_PATH.toString()) && main.getValue(key) != null) { + // User-supplied value goes first (takes precedence), then programmatic one. + main.putValue(key, value + " " + main.getValue(key)); + } else { + main.putValue(key, value); + } + } + } + + // Extra manifest sections from <archive><manifestSections> + if (!archive.manifestSections.isEmpty()) { + for (ArchiveConfiguration.ManifestSection section : archive.manifestSections) { + Attributes attrs = new Attributes(); + if (!section.manifestEntries.isEmpty()) { + for (Map.Entry<String, String> e : section.manifestEntries.entrySet()) { + attrs.putValue(e.getKey(), sanitize(e.getValue())); + } + } + manifest.getEntries().put(section.name, attrs); Review Comment: ⚠️ **Potential NPE:** `section.name` defaults to `null` (no initialiser in `ManifestSection`). `java.util.jar.Manifest.getEntries().put(null, attrs)` will throw a `NullPointerException` at runtime when a `<manifestSection>` element is omitted or its `<name>` tag is missing. Add a guard before the `put` call: ```suggestion if (section.name == null || section.name.isEmpty()) { throw new MojoException("Each <manifestSection> must declare a non-empty <name>."); } manifest.getEntries().put(section.name, attrs); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
