gnodet commented on code in PR #618:
URL: https://github.com/apache/maven-jar-plugin/pull/618#discussion_r4275204467


##########
src/main/java/org/apache/maven/plugins/jar/ArchiveConfiguration.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * 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.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Configuration for archive creation, provided through the {@code <archive>} 
plugin parameter.
+ *
+ * <p>This class replaces {@code 
org.apache.maven.shared.archiver.MavenArchiveConfiguration},
+ * removing the dependency on {@code maven-archiver} and {@code 
plexus-archiver}.</p>
+ *
+ * <p><strong>Note:</strong> This class and its inner classes must remain 
{@code public} until
+ * <a href="https://github.com/apache/maven/pull/13470";>apache/maven#13470</a> 
is released,
+ * as Maven's XML/DI binding requires public visibility to inject nested 
configuration objects.</p>
+ *
+ * @since 4.0.0-beta-3
+ */
+public class ArchiveConfiguration {
+
+    boolean compress = true;
+
+    boolean addMavenDescriptor = true;
+
+    Path manifestFile;
+
+    ManifestConfig manifest;
+
+    Map<String, String> manifestEntries = new LinkedHashMap<>();
+
+    List<ManifestSection> manifestSections = new ArrayList<>();
+
+    boolean forced = true;
+
+    Path pomPropertiesFile;
+
+    /**
+     * Lazily initialises and returns the manifest configuration.
+     */
+    ManifestConfig getManifest() {
+        if (manifest == null) {
+            manifest = new ManifestConfig();

Review Comment:
   Good point. Making it `final` isn't possible because Maven's XML/DI binding 
replaces the field when the user supplies an `<archive><manifest>` block — 
`final` would cause an `IllegalAccessException` at injection time. The eager 
initialization (`= new ManifestConfig()`) achieves the same effect without the 
constraint: the lazy getter is now a simple accessor.



##########
src/main/java/org/apache/maven/plugins/jar/ManifestBuilder.java:
##########
@@ -0,0 +1,425 @@
+/*
+ * 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#addBuildEnvironmentEntries} 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) {
+                if (section.name == null || section.name.isEmpty()) {
+                    throw new MojoException("Each <manifestSection> must 
declare a non-empty <name>.");
+                }
+                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);
+            }
+        }
+
+        return manifest;
+    }
+
+    private Manifest buildManifest(ArchiveConfiguration.ManifestConfig config, 
Map<String, String> extraEntries) {
+        Manifest m = new Manifest();
+        Attributes main = m.getMainAttributes();
+        // Manifest-Version is required; java.util.jar.Manifest does not add 
it automatically.
+        main.put(Attributes.Name.MANIFEST_VERSION, "1.0");

Review Comment:
   Tested: `JarOutputStream` does not add `Manifest-Version` automatically. 
When the attribute is absent from the manifest passed to `JarOutputStream`, the 
resulting `META-INF/MANIFEST.MF` entry is empty (just `\r\n`), which is invalid 
per the JAR spec. The comment and the `put` call are intentional.



-- 
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]

Reply via email to