You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "gnodet (via GitHub)" <gi...@apache.org> on 2026/09/25 18:51:49 UTC
[PR] [MJAR-596] Sanitize invalid Automatic-Module-Name from manifest files instead of failing [maven-jar-plugin]
gnodet opened a new pull request, #610:
URL: https://github.com/apache/maven-jar-plugin/pull/610
[MJAR-596] Sanitize invalid Automatic-Module-Name from manifest files instead of failing
When Automatic-Module-Name in a MANIFEST.MF file (e.g. read from the compiled
output directory) is not a valid Java module name, apply the same sanitization
algorithm the JDK uses when deriving an automatic module name from a JAR file
name (ModulePath.cleanModuleName): replace non-alphanumeric characters with
'.', collapse repeated dots, strip leading/trailing dots.
This handles the common case where a project sets Automatic-Module-Name to
"${project.groupId}.${project.artifactId}" and the artifactId contains hyphens
(e.g. integration-test), which are legal Maven artifact identifiers but not
valid JPMS module name segments.
If the sanitized name is still not a valid Java module name (e.g. starts with a
digit or contains a Java keyword), the attribute is omitted with a warning.
If the name is explicitly declared in the POM via <archive><manifestEntries>,
the build still fails immediately (MJAR-260 behaviour).
Fixes: https://github.com/apache/maven-jar-plugin/issues/596
--
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: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MJAR-596] Sanitize invalid Automatic-Module-Name from manifest files instead of failing [maven-jar-plugin]
Posted by "desruisseaux (via GitHub)" <gi...@apache.org>.
desruisseaux commented on code in PR #610:
URL: https://github.com/apache/maven-jar-plugin/pull/610#discussion_r4115128597
##########
src/main/java/org/apache/maven/plugins/jar/ToolExecutor.java:
##########
@@ -69,6 +70,104 @@ final class ToolExecutor {
*/
private static final String CREATED_BY = "Created-By";
+ /**
+ * Matches any character that is not an ASCII letter or digit.
+ * Used by {@link #cleanModuleName(String)} to mirror the JDK's {@code ModulePath.cleanModuleName()} algorithm.
+ */
+ private static final Pattern NON_ALPHANUM = Pattern.compile("[^A-Za-z0-9]");
Review Comment:
Delete the two patterns if the `cleanModuleName` method is replaced as suggested.
--
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: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MJAR-596] Sanitize invalid Automatic-Module-Name from manifest files instead of failing [maven-jar-plugin]
Posted by "desruisseaux (via GitHub)" <gi...@apache.org>.
desruisseaux commented on code in PR #610:
URL: https://github.com/apache/maven-jar-plugin/pull/610#discussion_r4115053273
##########
src/it/MJAR-596-sanitize-automatic-module-name/verify.groovy:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.nio.file.*;
+import java.util.jar.*;
+
+/*
+ * MJAR-596: An invalid Automatic-Module-Name read from a MANIFEST.MF file (not from POM
+ * <manifestEntries>) must be sanitized to a valid name instead of failing the build.
+ *
+ * The MANIFEST.MF resource sets:
+ * Automatic-Module-Name: org.apache.maven.plugins.mjar596-integration-test
+ * The hyphen is invalid in a JPMS module name. The plugin must:
+ * - succeed (BUILD SUCCESS)
+ * - sanitize the name to: org.apache.maven.plugins.mjar596.integration.test
+ * - emit a [WARNING] about the sanitization
+ */
+
+boolean result = true;
+
+try
+{
+ // 1. Build must succeed.
+ File target = new File( basedir, "target" );
+ if ( !target.exists() || !target.isDirectory() )
Review Comment:
Replace by `assert`
--
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: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MJAR-596] Sanitize invalid Automatic-Module-Name from manifest files instead of failing [maven-jar-plugin]
Posted by "gnodet-bot (via GitHub)" <gi...@apache.org>.
gnodet-bot commented on code in PR #610:
URL: https://github.com/apache/maven-jar-plugin/pull/610#discussion_r4224696041
##########
src/main/java/org/apache/maven/plugins/jar/ToolExecutor.java:
##########
@@ -69,6 +69,108 @@ final class ToolExecutor {
*/
private static final String CREATED_BY = "Created-By";
+ /**
+ * Sanitizes a candidate automatic module name by replacing all characters that are neither
+ * valid Java identifier start/part characters nor {@code '.'} separators, then collapsing
+ * consecutive separators and stripping leading/trailing dots.
+ *
+ * <p>Unlike the JDK's {@code ModulePath.cleanModuleName()} which is ASCII-only, this method
+ * accepts any character that {@link Character#isJavaIdentifierStart} or
+ * {@link Character#isJavaIdentifierPart} considers valid, including supplementary Unicode
+ * code points (the encoding of {@code MANIFEST.MF} files is UTF-8).</p>
+ *
+ * @param name the raw, potentially invalid module name
+ * @return the sanitized name, or an empty string if nothing remains after sanitization
+ */
+ private static String cleanModuleName(final String name) {
+ final int length = name.length();
+ final var b = new StringBuilder(length);
+ boolean wantStart = true;
+ int n;
+ for (int i = 0; i < length; i += n) {
+ final int c = name.codePointAt(i);
+ n = Character.charCount(c);
+ if (wantStart) {
+ if (!Character.isJavaIdentifierStart(c)) {
+ continue;
+ }
+ wantStart = false;
+ } else {
+ if (!Character.isJavaIdentifierPart(c)) {
+ wantStart = true;
+ b.append('.');
+ continue;
+ }
+ }
+ b.appendCodePoint(c);
+ }
+ int end = b.length() - 1;
+ if (end >= 0 && b.charAt(end) == '.') {
+ b.setLength(end);
+ }
+ return b.toString();
+ }
+
+ /**
+ * Validates the {@code Automatic-Module-Name} attribute in the given manifest.
+ * If the attribute is absent or already valid, this method does nothing and returns {@code false}.
+ *
+ * <p>If the name is invalid and was explicitly declared in the POM via
+ * {@code <archive><manifestEntries><Automatic-Module-Name>}, the build fails immediately
+ * (the developer chose that name and must fix it).</p>
+ *
+ * <p>If the name is invalid and was read from a {@code MANIFEST.MF} file (e.g. from the
+ * compiled output directory), {@link #cleanModuleName(String)} is applied and a warning is
+ * logged. If the sanitized name is still invalid the attribute is removed and a warning is
+ * logged (MJAR-596).</p>
+ *
+ * @param manifest the merged manifest to inspect and potentially modify
+ * @return {@code true} if the manifest was modified and a temporary manifest file must be written
+ * @throws MojoException if the name was explicitly declared in POM configuration and is invalid
+ */
+ private boolean sanitizeAutomaticModuleName(Manifest manifest) {
+ String name = manifest.getMainAttributes().getValue("Automatic-Module-Name");
+ if (name == null || SourceVersion.isName(name)) {
+ return false;
+ }
+ /*
+ * If the invalid name originates from an explicit <archive><manifestEntries> declaration
+ * in the POM, fail the build so that the developer is alerted immediately — they set an
+ * invalid name on purpose and should fix it. This matches the historical maven-archiver
+ * behaviour (MJAR-260).
+ *
+ * If the name was read from a MANIFEST.MF file (either from the compiled output directory
+ * or from <archive><manifestFile>), apply cleanModuleName() to replace characters that are
+ * not valid Java identifier start/part characters with '.' separators. This handles the
+ * common case where a project sets Automatic-Module-Name to
+ * "${project.groupId}.${project.artifactId}" and the artifactId contains hyphens (MJAR-596).
+ */
+ String pluginName = (manifestFromPlugin != null)
+ ? manifestFromPlugin.getMainAttributes().getValue("Automatic-Module-Name")
+ : null;
+ if (name.equals(pluginName)) {
+ throw new MojoException("Invalid automatic module name: \"" + name + "\".");
+ }
+ String sanitized = cleanModuleName(name);
+ if (!sanitized.isEmpty() && SourceVersion.isName(sanitized)) {
+ logger.warn("Automatic-Module-Name \"" + name + "\" is not a valid Java module name."
+ + " It has been sanitized to \"" + sanitized + "\""
+ + " using the same algorithm the JDK uses to derive automatic module names"
+ + " from JAR file names."
+ + " Consider setting a valid name explicitly"
Review Comment:
💡 **Nit:** The warning says "using the same algorithm the JDK uses" but the Javadoc directly above (`cleanModuleName`) says _"Unlike the JDK's `ModulePath.cleanModuleName()` which is ASCII-only"_. The algorithm was intentionally broadened — the warning text should reflect that.
```suggestion
logger.warn("Automatic-Module-Name \"" + name + "\" is not a valid Java module name."
+ " It has been sanitized to \"" + sanitized + "\""
+ " using an algorithm similar to the one the JDK uses to derive automatic module names"
+ " from JAR file names."
```
--
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: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MJAR-596] Sanitize invalid Automatic-Module-Name from manifest files instead of failing [maven-jar-plugin]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet merged PR #610:
URL: https://github.com/apache/maven-jar-plugin/pull/610
--
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: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MJAR-596] Sanitize invalid Automatic-Module-Name from manifest files instead of failing [maven-jar-plugin]
Posted by "desruisseaux (via GitHub)" <gi...@apache.org>.
desruisseaux commented on code in PR #610:
URL: https://github.com/apache/maven-jar-plugin/pull/610#discussion_r4115126239
##########
src/main/java/org/apache/maven/plugins/jar/ToolExecutor.java:
##########
@@ -69,6 +70,104 @@ final class ToolExecutor {
*/
private static final String CREATED_BY = "Created-By";
+ /**
+ * Matches any character that is not an ASCII letter or digit.
+ * Used by {@link #cleanModuleName(String)} to mirror the JDK's {@code ModulePath.cleanModuleName()} algorithm.
+ */
+ private static final Pattern NON_ALPHANUM = Pattern.compile("[^A-Za-z0-9]");
+
+ /**
+ * Matches two or more consecutive dots.
+ * Used by {@link #cleanModuleName(String)} to collapse repeated separators.
+ */
+ private static final Pattern REPEATING_DOTS = Pattern.compile("\\.{2,}");
+
+ /**
+ * Sanitizes a candidate automatic module name by applying the same algorithm that the JDK uses
+ * to derive an automatic module name from a JAR file name
+ * (see {@code jdk.internal.module.ModulePath.cleanModuleName()}):
+ * non-alphanumeric characters (including hyphens) are replaced with {@code '.'}, repeated dots
+ * are collapsed to a single dot, and leading/trailing dots are stripped.
+ *
+ * @param name the raw, potentially invalid module name
+ * @return the sanitized name, or an empty string if nothing remains after sanitization
+ */
+ private static String cleanModuleName(String name) {
+ name = NON_ALPHANUM.matcher(name).replaceAll(".");
+ name = REPEATING_DOTS.matcher(name).replaceAll(".");
+ int len = name.length();
+ if (len > 0 && name.charAt(0) == '.') {
+ name = name.replaceFirst("^\\.+", "");
+ }
+ len = name.length();
+ if (len > 0 && name.charAt(len - 1) == '.') {
+ name = name.replaceFirst("\\.+$", "");
+ }
Review Comment:
I propose to replace the full method by the following code. It accepts any valid Java identifier, including with supplementary Unicode characters (the encoding of `MANIFEST.MF` files is UTF-8).
```java
private static String cleanModuleName(final String name) {
final int length = name.length();
final var b = new StringBuilder(length);
boolean wantStart = true;
int n;
for (int i = 0; i < length; i += n) {
final int c = name.codePointAt(i);
n = Character.charCount(c);
if (wantStart) {
if (!Character.isJavaIdentifierStart(c)) {
continue;
}
wantStart = false;
} else {
if (!Character.isJavaIdentifierPart(c)) {
wantStart = true;
b.append('.');
continue;
}
}
b.appendCodePoint(c);
}
int end = b.length() - 1;
if (end >= 0 && b.charAt(end) == '.') {
b.setLength(end);
}
return b.toString();
}
```
--
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: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MJAR-596] Sanitize invalid Automatic-Module-Name from manifest files instead of failing [maven-jar-plugin]
Posted by "desruisseaux (via GitHub)" <gi...@apache.org>.
desruisseaux commented on code in PR #610:
URL: https://github.com/apache/maven-jar-plugin/pull/610#discussion_r4115137778
##########
src/main/java/org/apache/maven/plugins/jar/ToolExecutor.java:
##########
@@ -69,6 +70,104 @@ final class ToolExecutor {
*/
private static final String CREATED_BY = "Created-By";
+ /**
+ * Matches any character that is not an ASCII letter or digit.
+ * Used by {@link #cleanModuleName(String)} to mirror the JDK's {@code ModulePath.cleanModuleName()} algorithm.
+ */
+ private static final Pattern NON_ALPHANUM = Pattern.compile("[^A-Za-z0-9]");
+
+ /**
+ * Matches two or more consecutive dots.
+ * Used by {@link #cleanModuleName(String)} to collapse repeated separators.
+ */
+ private static final Pattern REPEATING_DOTS = Pattern.compile("\\.{2,}");
+
+ /**
+ * Sanitizes a candidate automatic module name by applying the same algorithm that the JDK uses
+ * to derive an automatic module name from a JAR file name
+ * (see {@code jdk.internal.module.ModulePath.cleanModuleName()}):
+ * non-alphanumeric characters (including hyphens) are replaced with {@code '.'}, repeated dots
+ * are collapsed to a single dot, and leading/trailing dots are stripped.
+ *
+ * @param name the raw, potentially invalid module name
+ * @return the sanitized name, or an empty string if nothing remains after sanitization
+ */
+ private static String cleanModuleName(String name) {
+ name = NON_ALPHANUM.matcher(name).replaceAll(".");
+ name = REPEATING_DOTS.matcher(name).replaceAll(".");
+ int len = name.length();
+ if (len > 0 && name.charAt(0) == '.') {
+ name = name.replaceFirst("^\\.+", "");
+ }
+ len = name.length();
+ if (len > 0 && name.charAt(len - 1) == '.') {
+ name = name.replaceFirst("\\.+$", "");
+ }
+ return name;
+ }
+
+ /**
+ * Validates the {@code Automatic-Module-Name} attribute in the given manifest.
+ * If the attribute is absent or already valid, this method does nothing and returns {@code false}.
+ *
+ * <p>If the name is invalid and was explicitly declared in the POM via
+ * {@code <archive><manifestEntries><Automatic-Module-Name>}, the build fails immediately
+ * (the developer chose that name and must fix it).</p>
+ *
+ * <p>If the name is invalid and was read from a {@code MANIFEST.MF} file (e.g. from the
+ * compiled output directory), the {@linkplain #cleanModuleName(String) JDK sanitization
+ * algorithm} is applied and a warning is logged. If the sanitized name is still invalid
+ * the attribute is removed and a warning is logged (MJAR-596).</p>
+ *
+ * @param manifest the merged manifest to inspect and potentially modify
+ * @return {@code true} if the manifest was modified and a temporary manifest file must be written
+ * @throws MojoException if the name was explicitly declared in POM configuration and is invalid
+ */
+ private boolean sanitizeAutomaticModuleName(Manifest manifest) {
+ String name = manifest.getMainAttributes().getValue("Automatic-Module-Name");
+ if (name == null || SourceVersion.isName(name)) {
+ return false;
+ }
+ /*
+ * If the invalid name originates from an explicit <archive><manifestEntries> declaration
+ * in the POM, fail the build so that the developer is alerted immediately — they set an
+ * invalid name on purpose and should fix it. This matches the historical maven-archiver
+ * behaviour (MJAR-260).
+ *
+ * If the name was read from a MANIFEST.MF file (either from the compiled output directory
+ * or from <archive><manifestFile>), apply the same sanitization algorithm the JDK uses
+ * when deriving an automatic module name from a JAR filename
Review Comment:
Shouldn't we fail the build in this case too? I presume that the content of this `MANIFEST.MF` has been specified by the user in some way, in which case it may have done a mistake.
--
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: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [MJAR-596] Sanitize invalid Automatic-Module-Name from manifest files instead of failing [maven-jar-plugin]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #610:
URL: https://github.com/apache/maven-jar-plugin/pull/610#discussion_r4224522151
##########
src/main/java/org/apache/maven/plugins/jar/ToolExecutor.java:
##########
@@ -69,6 +70,104 @@ final class ToolExecutor {
*/
private static final String CREATED_BY = "Created-By";
+ /**
+ * Matches any character that is not an ASCII letter or digit.
+ * Used by {@link #cleanModuleName(String)} to mirror the JDK's {@code ModulePath.cleanModuleName()} algorithm.
+ */
+ private static final Pattern NON_ALPHANUM = Pattern.compile("[^A-Za-z0-9]");
+
+ /**
+ * Matches two or more consecutive dots.
+ * Used by {@link #cleanModuleName(String)} to collapse repeated separators.
+ */
+ private static final Pattern REPEATING_DOTS = Pattern.compile("\\.{2,}");
+
+ /**
+ * Sanitizes a candidate automatic module name by applying the same algorithm that the JDK uses
+ * to derive an automatic module name from a JAR file name
+ * (see {@code jdk.internal.module.ModulePath.cleanModuleName()}):
+ * non-alphanumeric characters (including hyphens) are replaced with {@code '.'}, repeated dots
+ * are collapsed to a single dot, and leading/trailing dots are stripped.
+ *
+ * @param name the raw, potentially invalid module name
+ * @return the sanitized name, or an empty string if nothing remains after sanitization
+ */
+ private static String cleanModuleName(String name) {
+ name = NON_ALPHANUM.matcher(name).replaceAll(".");
+ name = REPEATING_DOTS.matcher(name).replaceAll(".");
+ int len = name.length();
+ if (len > 0 && name.charAt(0) == '.') {
+ name = name.replaceFirst("^\\.+", "");
+ }
+ len = name.length();
+ if (len > 0 && name.charAt(len - 1) == '.') {
+ name = name.replaceFirst("\\.+$", "");
+ }
+ return name;
+ }
+
+ /**
+ * Validates the {@code Automatic-Module-Name} attribute in the given manifest.
+ * If the attribute is absent or already valid, this method does nothing and returns {@code false}.
+ *
+ * <p>If the name is invalid and was explicitly declared in the POM via
+ * {@code <archive><manifestEntries><Automatic-Module-Name>}, the build fails immediately
+ * (the developer chose that name and must fix it).</p>
+ *
+ * <p>If the name is invalid and was read from a {@code MANIFEST.MF} file (e.g. from the
+ * compiled output directory), the {@linkplain #cleanModuleName(String) JDK sanitization
+ * algorithm} is applied and a warning is logged. If the sanitized name is still invalid
+ * the attribute is removed and a warning is logged (MJAR-596).</p>
+ *
+ * @param manifest the merged manifest to inspect and potentially modify
+ * @return {@code true} if the manifest was modified and a temporary manifest file must be written
+ * @throws MojoException if the name was explicitly declared in POM configuration and is invalid
+ */
+ private boolean sanitizeAutomaticModuleName(Manifest manifest) {
+ String name = manifest.getMainAttributes().getValue("Automatic-Module-Name");
+ if (name == null || SourceVersion.isName(name)) {
+ return false;
+ }
+ /*
+ * If the invalid name originates from an explicit <archive><manifestEntries> declaration
+ * in the POM, fail the build so that the developer is alerted immediately — they set an
+ * invalid name on purpose and should fix it. This matches the historical maven-archiver
+ * behaviour (MJAR-260).
+ *
+ * If the name was read from a MANIFEST.MF file (either from the compiled output directory
+ * or from <archive><manifestFile>), apply the same sanitization algorithm the JDK uses
+ * when deriving an automatic module name from a JAR filename
Review Comment:
Good question. The distinction we're making is:
- **POM `<manifestEntries>`**: the developer explicitly authored that name → fail fast so they fix it (MJAR-260 behaviour).
- **`MANIFEST.MF` resource** (either from `src/main/resources/META-INF/` or `<archive><manifestFile>`): the file is often part of a dependency JAR being repackaged, or generated by a build step the developer doesn't directly control. Failing the build in that case would be surprising — the developer may not even know the file exists. The JDK itself applies sanitization rather than failing when it encounters an invalid name in a JAR file name. Dropping the attribute with a warning follows the same principle: give the build a chance to succeed while making the problem visible.
That said, if a sanitized name cannot be produced at all (empty result), removing the attribute silently enough is the conservative choice — the alternative (a totally broken module name) is worse for consumers.
--
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: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org