Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail on invalid 'Automatic-Module-Name' in MANIFEST #1

Merged
merged 1 commit into from
Nov 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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