Skip to content

Commit

Permalink
Fail on invalid 'Automatic-Module-Name' in MANIFEST
Browse files Browse the repository at this point in the history
Maven Archiver now fails with a `ManifestException` when an invalid
`Automatic-Module-Name` is detected within the `META-INF/MANIFEST.MF`
entries.

For details see https://sormuras.github.io/blog/2018-11-16-invalid-automatic-module-names

https://issues.apache.org/jira/projects/MSHARED/issues/MSHARED-773
  • Loading branch information
sormuras committed Nov 17, 2018
1 parent cba663c commit 99b4c6b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/org/apache/maven/archiver/MavenArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/org/apache/maven/archiver/MavenArchiverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -500,6 +524,7 @@ public void testManifestEntries()
Map<String, String> manifestEntries = new HashMap<String, String>();
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 );

Expand Down Expand Up @@ -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" ) ) );
Expand All @@ -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<String, String> manifestEntries = new HashMap<String, String>();
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
Expand Down

0 comments on commit 99b4c6b

Please sign in to comment.