From 6cddbbb245729769215be41c55f532a9e4f35831 Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Wed, 14 Jan 2026 14:40:01 -0600 Subject: [PATCH] Add support for automatic module name Fixes #520 --- src/it/automatic-name/pom.xml | 45 +++++++++++ .../apache/maven/its/it/issue520/Main.java | 25 +++++++ src/it/automatic-name/verify.groovy | 74 +++++++++++++++++++ .../maven/plugins/jar/AbstractJarMojo.java | 18 +++++ 4 files changed, 162 insertions(+) create mode 100644 src/it/automatic-name/pom.xml create mode 100644 src/it/automatic-name/src/main/java/org/apache/maven/its/it/issue520/Main.java create mode 100644 src/it/automatic-name/verify.groovy diff --git a/src/it/automatic-name/pom.xml b/src/it/automatic-name/pom.xml new file mode 100644 index 0000000..da769e5 --- /dev/null +++ b/src/it/automatic-name/pom.xml @@ -0,0 +1,45 @@ + + + + 4.0.0 + org.apache.maven.plugins + automatic-name + 1.0-SNAPSHOT + jar + automatic-name-it + + jar plugin it + + + 11 + + + + + org.apache.maven.plugins + maven-jar-plugin + @project.version@ + + org.apache.maven.its.it.issue520 + + + + + diff --git a/src/it/automatic-name/src/main/java/org/apache/maven/its/it/issue520/Main.java b/src/it/automatic-name/src/main/java/org/apache/maven/its/it/issue520/Main.java new file mode 100644 index 0000000..cbee9e1 --- /dev/null +++ b/src/it/automatic-name/src/main/java/org/apache/maven/its/it/issue520/Main.java @@ -0,0 +1,25 @@ +/* + * 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.its.it.issue520; + +public final class Main { + public static void main(String[] args) { + System.out.println("Hello World!"); + } +} \ No newline at end of file diff --git a/src/it/automatic-name/verify.groovy b/src/it/automatic-name/verify.groovy new file mode 100644 index 0000000..a9b16e8 --- /dev/null +++ b/src/it/automatic-name/verify.groovy @@ -0,0 +1,74 @@ + +/* + * 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. + */ + +import java.io.*; +import java.util.*; +import java.util.jar.*; +import org.apache.commons.io.*; + +boolean result = true; + +try +{ + File target = new File( basedir, "target" ); + if ( !target.exists() || !target.isDirectory() ) + { + System.err.println( "target file is missing or not a directory." ); + return false; + } + + File artifact = new File( target, "automatic-name-1.0-SNAPSHOT.jar" ); + if ( !artifact.exists() || artifact.isDirectory() ) + { + System.err.println( "artifact file " + file + " is missing or a directory." ); + return false; + } + + JarFile jar = new JarFile( artifact ); + Enumeration jarEntries = jar.entries(); + while ( jarEntries.hasMoreElements() ) + { + JarEntry entry = (JarEntry) jarEntries.nextElement(); + if ( !entry.isDirectory() ) + { + // Only compare files + if ( entry.getName().equals( "META-INF/MANIFEST.MF" ) ) + { + String manifest = IOUtils.toString( jar.getInputStream ( entry ) ); + int index = manifest.indexOf( "Automatic-Module-Name: org.apache.maven.its.it.issue520" ); + if ( index <= 0 ) + { + System.err.println( "MANIFEST doesn't contain: 'Automatic-Module-Name: org.apache.maven.its.it.issue520'" ); + return false; + } + return true; + } + } + } + System.err.println( "MANIFEST.MF not found" ); + return false; +} +catch( Throwable e ) +{ + e.printStackTrace(); + result = false; +} + +return result; diff --git a/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java b/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java index 058ac05..b8e7fd5 100644 --- a/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java +++ b/src/main/java/org/apache/maven/plugins/jar/AbstractJarMojo.java @@ -154,6 +154,15 @@ public abstract class AbstractJarMojo implements org.apache.maven.api.plugin.Moj @Parameter(property = "maven.jar.detectMultiReleaseJar", defaultValue = "true") private boolean detectMultiReleaseJar; + /** + * Establish the automatic module name of the module in the JAR. + * Ignored if the JAR contains a {@code module-info.class} file. + * + * @since 3.6.0 + */ + @Parameter(property = "maven.jar.automaticModuleName") + private String automaticModuleName; + /** * The MOJO logger. */ @@ -257,6 +266,15 @@ public Path createArchive() throws MojoException { boolean containsModuleDescriptor = Arrays.stream(includedFiles).anyMatch(p -> p.endsWith(MODULE_DESCRIPTOR_FILE_NAME)); + if (automaticModuleName != null && !automaticModuleName.isEmpty()) { + if (containsModuleDescriptor) { + getLog().warn("Not setting automatic module name, because a module descriptor was found."); + } else { + getLog().debug("Adding 'Automatic-Module-Name: " + automaticModuleName + "' manifest entry."); + archive.addManifestEntry("Automatic-Module-Name", automaticModuleName); + } + } + String archiverName = containsModuleDescriptor ? "mjar" : "jar"; MavenArchiver archiver = new MavenArchiver();