desruisseaux commented on code in PR #618: URL: https://github.com/apache/maven-jar-plugin/pull/618#discussion_r4294243009
########## src/main/java/org/apache/maven/plugins/jar/ManifestBuilder.java: ########## @@ -0,0 +1,395 @@ +/* + * 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.Files; +import java.nio.file.Path; +import java.util.Map; +import java.util.Set; +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. + */ +final class ManifestBuilder { + + 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"; + + // Attribute name constants not predefined in java.util.jar.Attributes.Name + private static final Attributes.Name JAVA_VERSION = new Attributes.Name("Java-Version"); + private static final Attributes.Name BUILD_JDK_SPEC = new Attributes.Name("Build-Jdk-Spec"); + private static final Attributes.Name BUILD_JDK = new Attributes.Name("Build-Jdk"); + private static final Attributes.Name BUILT_BY = new Attributes.Name("Built-By"); + + 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; + + ManifestBuilder(Session session, Project project, ArchiveConfiguration archive) { + this.session = session; + this.project = project; + this.archive = archive; + } + + /** + * Builds the manifest. + * + * @return the manifest, never {@code null} + * @throws MojoException if the manifest cannot be built + */ + Manifest build() { + Manifest manifest = buildManifest(); + + // Merge any extra entries from <archive><manifestEntries> + Attributes main = manifest.getMainAttributes(); + for (Map.Entry<String, String> entry : archive.manifestEntries.entrySet()) { + String key = entry.getKey(); + String value = sanitize(entry.getValue()); + if (key.equals(Attributes.Name.CLASS_PATH.toString())) { + // User-supplied value goes first (takes precedence), then programmatic one. + main.merge( + Attributes.Name.CLASS_PATH, value, (existing, supplied) -> (String) supplied + ' ' + existing); + } else { + main.putValue(key, value); + } + } + + // Extra manifest sections from <archive><manifestSections> + for (ArchiveConfiguration.ManifestSection section : archive.manifestSections) { + if (section.name == null || section.name.isBlank()) { + throw new MojoException("Each <manifestSection> must declare a non-empty <name>."); + } + Attributes attrs = new Attributes(); + 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() { + Manifest manifest = new Manifest(); + Attributes main = manifest.getMainAttributes(); + // Manifest-Version must be present or java.util.jar.Manifest.write() silently drops all main attributes. + main.put(Attributes.Name.MANIFEST_VERSION, "1.0"); + + ArchiveConfiguration.ManifestConfig config = archive.manifest; + if (config.addDefaultEntries) { + addDefaultEntries(main); + } + + if (config.addBuildEnvironmentEntries) { + addBuildEnvironmentEntries(main); + } + + if (config.addClasspath) { + String classpath = buildClasspath(); + if (!classpath.isEmpty()) { + main.put(Attributes.Name.CLASS_PATH, classpath); + } + } + + if (config.addDefaultSpecificationEntries) { + addSpecificationEntries(main); + } + + if (config.addDefaultImplementationEntries) { + addImplementationEntries(main); + } + + String mainClass = config.mainClass; + if (mainClass != null && !mainClass.isEmpty()) { + putIfAbsent(main, Attributes.Name.MAIN_CLASS, mainClass); + } + + return manifest; + } + + private void addDefaultEntries(Attributes main) { + // Java-Version reflects the compiler target (e.g. "11"), as set by maven-compiler-plugin. + String jdk = discoverJavaRelease(project.getModel()); + if (jdk != null) { + putIfAbsent(main, JAVA_VERSION, jdk); + } + } + + private void addBuildEnvironmentEntries(Attributes main) { + // Build-Jdk-Spec reflects the runtime JDK specification version. + String specVersion = System.getProperty("java.specification.version"); + if (specVersion != null) { + putIfAbsent(main, BUILD_JDK_SPEC, specVersion); + } + String javaVersion = System.getProperty("java.version"); + if (javaVersion != null) { + putIfAbsent(main, BUILD_JDK, javaVersion); + } + String user = System.getProperty("user.name"); + if (user != null) { + putIfAbsent(main, BUILT_BY, user); Review Comment: Can be simplified by a calls to `putIfAbsentNonEmpty` instead. -- 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]
