diff --git a/src/main/java/org/apache/maven/archiver/MavenArchiver.java b/src/main/java/org/apache/maven/archiver/MavenArchiver.java index 66d9353..f4f2636 100644 --- a/src/main/java/org/apache/maven/archiver/MavenArchiver.java +++ b/src/main/java/org/apache/maven/archiver/MavenArchiver.java @@ -38,6 +38,7 @@ import org.codehaus.plexus.interpolation.ValueSource; import org.apache.maven.shared.utils.StringUtils; +import javax.lang.model.SourceVersion; import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -91,6 +92,11 @@ public class MavenArchiver ARTIFACT_EXPRESSION_PREFIXES = artifactExpressionPrefixes; } + static boolean isValidModuleName( String name ) + { + return SourceVersion.isName( name ); + } + private JarArchiver archiver; private File archiveFile; @@ -645,6 +651,15 @@ public void createArchive( MavenSession session, MavenProject project, // "Forced build is disabled, but disabling the forced mode isn't supported by the archiver." ); } + String automaticModuleName = manifest.getMainSection().getAttributeValue( "Automatic-Module-Name" ); + if ( automaticModuleName != null ) + { + if ( !isValidModuleName( automaticModuleName ) ) + { + throw new ManifestException( "Invalid automatic module name: '" + automaticModuleName + "'" ); + } + } + // create archive archiver.createArchive(); } diff --git a/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java b/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java index 4c0d799..6803f24 100644 --- a/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java +++ b/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java @@ -82,6 +82,30 @@ public boolean equals( Object o ) } } + @Test + public void testInvalidModuleNames() + { + assertFalse( MavenArchiver.isValidModuleName( "" ) ); + assertFalse( MavenArchiver.isValidModuleName( "." ) ); + assertFalse( MavenArchiver.isValidModuleName( "dash-is-invalid" ) ); + assertFalse( MavenArchiver.isValidModuleName( "plus+is+invalid" ) ); + assertFalse( MavenArchiver.isValidModuleName( "colon:is:invalid" ) ); + assertFalse( MavenArchiver.isValidModuleName( "new.class" ) ); + assertFalse( MavenArchiver.isValidModuleName( "123.at.start.is.invalid" ) ); + assertFalse( MavenArchiver.isValidModuleName( "digit.at.123start.is.invalid" ) ); + } + + @Test + public void testValidModuleNames() + { + assertTrue( MavenArchiver.isValidModuleName( "a" ) ); + assertTrue( MavenArchiver.isValidModuleName( "a.b" ) ); + assertTrue( MavenArchiver.isValidModuleName( "a_b" ) ); + assertTrue( MavenArchiver.isValidModuleName( "trailing0.digits123.are456.ok789" ) ); + assertTrue( MavenArchiver.isValidModuleName( "UTF8.chars.are.okay.äëïöüẍ" ) ); + assertTrue( MavenArchiver.isValidModuleName( "ℤ€ℕ" ) ); + } + @Test public void testGetManifestExtensionList() throws Exception @@ -500,6 +524,7 @@ public void testManifestEntries() Map manifestEntries = new HashMap(); manifestEntries.put( "foo", "bar" ); manifestEntries.put( "first-name", "olivier" ); + manifestEntries.put( "Automatic-Module-Name", "org.apache.maven.archiver" ); manifestEntries.put( "keyWithEmptyValue", null ); config.setManifestEntries( manifestEntries ); @@ -532,6 +557,7 @@ public void testManifestEntries() assertEquals( "bar", manifest.get( new Attributes.Name( "foo" ) ) ); assertEquals( "olivier", manifest.get( new Attributes.Name( "first-name" ) ) ); + assertEquals( "org.apache.maven.archiver", manifest.getValue( "Automatic-Module-Name" ) ); assertEquals( System.getProperty( "java.version" ), manifest.get( new Attributes.Name( "Build-Jdk" ) ) ); assertEquals( System.getProperty( "user.name" ), manifest.get( new Attributes.Name( "Built-By" ) ) ); @@ -544,6 +570,33 @@ public void testManifestEntries() assertEquals( "value", manifest.get( new Attributes.Name( "key" ) ) ); } + @Test + public void testManifestWithInvalidAutomaticModuleNameThrowsOnCreateArchive() + throws Exception + { + File jarFile = new File( "target/test/dummy.jar" ); + JarArchiver jarArchiver = getCleanJarArchiver( jarFile ); + + MavenArchiver archiver = getMavenArchiver( jarArchiver ); + + MavenSession session = getDummySession(); + MavenProject project = getDummyProject(); + MavenArchiveConfiguration config = new MavenArchiveConfiguration(); + + Map manifestEntries = new HashMap(); + manifestEntries.put( "Automatic-Module-Name", "123.in-valid.new.name" ); + config.setManifestEntries( manifestEntries ); + + try + { + archiver.createArchive( session, project, config ); + } + catch ( ManifestException e ) + { + assertEquals( "Invalid automatic module name: '123.in-valid.new.name'", e.getMessage() ); + } + } + @Test public void testCreatedByManifestEntryWithoutMavenVersion() throws Exception