Skip to content

Sanitize invalid Automatic-Module-Name instead of failing - #402

Merged
gnodet merged 1 commit into
apache:masterfrom
gnodet:fix/auto-module-name-validation
Oct 9, 2026
Merged

gnodet merged 1 commit into
apache:masterfrom
gnodet:fix/auto-module-name-validation

Conversation

@gnodet

@gnodet gnodet commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

Fixes apache/maven-jar-plugin#596

Projects that configure Automatic-Module-Name in <archive><manifestEntries> using a property derived from the artifactId (e.g. ${project.groupId}.${project.artifactId}) can produce invalid JPMS module names when the artifactId contains hyphens. This previously caused a hard build failure with ManifestException: Invalid automatic module name.

This change sanitizes the invalid name using the same algorithm the JDK applies when deriving an automatic module name from a JAR file name (ModulePath.cleanModuleName()): non-alphanumeric characters are replaced with ., repeated dots are collapsed, and leading/trailing dots are stripped. A warning is emitted so the user is aware.

For example, org.apache.geronimo.arthur.integration-test becomes org.apache.geronimo.arthur.integration.test.

If the sanitized name is still not a valid Java module name (e.g. starts with a digit, contains a keyword), the attribute is omitted with a warning — it cannot be fixed automatically in that case.

Affected projects include:

  • geronimo-arthur — e.g. org.apache.geronimo.arthur.integration-test
  • karaf-winegrower — e.g. org.apache.winegrower.cepages.winegrower-cepage-osgi-cdi

Changes:

  • MavenArchiver.java: Add cleanModuleName() sanitization method; replace hard failure with sanitize-then-warn for invalid module names; only omit the attribute when sanitization still yields an invalid name
  • MavenArchiverTest.java: Updated tests to verify sanitization behavior; added cleanModuleNames parameterized test for the sanitization algorithm

@gnodet
gnodet force-pushed the fix/auto-module-name-validation branch from 0b97ab1 to 3d8fa92 Compare September 18, 2026 06:42
@gnodet gnodet changed the title [MJAR-596] Warn instead of fail for invalid Automatic-Module-Name [MJAR-596] Sanitize invalid Automatic-Module-Name instead of failing Sep 18, 2026
@slachiewicz slachiewicz added the enhancement New feature or request label Sep 19, 2026

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid change. I verified cleanModuleName() is functionally equivalent to the JDK's ModulePath.cleanModuleName() — same regex replacement strategy, same edge-case handling. The two new test methods (manifestWithUnsanitizableAutomaticModuleNameWarnsAndOmits, manifestWithHyphenatedAutomaticModuleNameIsSanitized) cover both sanitization paths well. I confirmed SourceVersion.isName("123.in.valid.new.name") returns false (both the digit-leading segment and "new" being a keyword contribute), so the unsanitizable test value is correctly chosen. All tests pass locally.

One minor nit below.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed after b340abf. The previous nit (move NON_ALPHANUM and REPEATING_DOTS constants up with the other static final fields) is addressed — both patterns now sit right after SPECIFICATION_VERSION_PATTERN, consistent with the class convention. No other changes in this commit. Good to merge.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet gnodet added this to the 4.0.0-beta-5 milestone Sep 28, 2026
@elharo elharo changed the title [MJAR-596] Sanitize invalid Automatic-Module-Name instead of failing Sanitize invalid Automatic-Module-Name instead of failing Sep 30, 2026
@gnodet gnodet added bug Something isn't working java Pull requests that update Java code and removed enhancement New feature or request labels Oct 9, 2026
gnodet added a commit that referenced this pull request Oct 9, 2026
…-archiver-3.x]

- Replace hard build failure with warn+sanitize for invalid JPMS module names in manifestEntries
- Apply JDK ModulePath.cleanModuleName() algorithm: replace non-alphanum with '.', collapse repeated dots, strip leading/trailing dots
- Emit warning so the user is notified of the sanitization
- Omit the attribute (with warning) when the sanitized name is still invalid
- Add parameterized tests for the sanitization algorithm and the warn+omit path

Backport of #402 to maven-archiver-3.x.
- Replace hard ManifestException with warn+sanitize for invalid JPMS module names in manifestEntries
- Apply JDK ModulePath.cleanModuleName() algorithm: replace non-alphanumeric chars with '.', collapse repeated dots, strip leading/trailing dots
- Emit warning so user is notified of the sanitization (e.g. 'org.apache.foo-bar' → 'org.apache.foo.bar')
- Omit the attribute with a warning when the sanitized name is still invalid (digit-leading, reserved keyword)
- Add cleanModuleNames parameterized test and update manifestWithInvalidAutomaticModuleName test to verify warn+omit path
@gnodet
gnodet force-pushed the fix/auto-module-name-validation branch from b340abf to 6fa3fed Compare October 9, 2026 09:18
@gnodet
gnodet merged commit ee95d81 into apache:master Oct 9, 2026
1 check passed
@ascheman ascheman modified the milestones: 4.0.0-beta-5, 4.0.0-beta-6 Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working java Pull requests that update Java code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Derived automatic module name validation fails builds for non-modular projects since 3.4.2

4 participants