Skip to content

Commit

Permalink
o Refactored Code
Browse files Browse the repository at this point in the history
   - introduced some handle methods to get shorter method length.
 o Removed System.out.println from test code


git-svn-id: https://svn.apache.org/repos/asf/maven/shared/trunk@1738445 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
khmarbaise committed Apr 10, 2016
1 parent 00a8e43 commit 6e7683c
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 98 deletions.
206 changes: 114 additions & 92 deletions src/main/java/org/apache/maven/archiver/MavenArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,32 +275,7 @@ protected Manifest getManifest( MavenSession session, MavenProject project, Mani
{
List<ValueSource> valueSources = new ArrayList<ValueSource>();

valueSources.add( new PrefixedObjectValueSource( ARTIFACT_EXPRESSION_PREFIXES, artifact,
true ) );
valueSources.add( new PrefixedObjectValueSource( ARTIFACT_EXPRESSION_PREFIXES,
artifact.getArtifactHandler(), true ) );

Properties extraExpressions = new Properties();
// FIXME: This query method SHOULD NOT affect the internal
// state of the artifact version, but it does.
if ( !artifact.isSnapshot() )
{
extraExpressions.setProperty( "baseVersion", artifact.getVersion() );
}

extraExpressions.setProperty( "groupIdPath", artifact.getGroupId().replace( '.', '/' ) );
if ( StringUtils.isNotEmpty( artifact.getClassifier() ) )
{
extraExpressions.setProperty( "dashClassifier", "-" + artifact.getClassifier() );
extraExpressions.setProperty( "dashClassifier?", "-" + artifact.getClassifier() );
}
else
{
extraExpressions.setProperty( "dashClassifier", "" );
extraExpressions.setProperty( "dashClassifier?", "" );
}
valueSources.add( new PrefixedPropertiesValueSource( ARTIFACT_EXPRESSION_PREFIXES,
extraExpressions, true ) );
handleExtraExpression( artifact, valueSources );

for ( ValueSource vs : valueSources )
{
Expand Down Expand Up @@ -386,42 +361,12 @@ else if ( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_CUSTOM.equals( layoutType

if ( config.isAddDefaultSpecificationEntries() )
{
addManifestAttribute( m, entries, "Specification-Title", project.getName() );

try
{
ArtifactVersion version = project.getArtifact().getSelectedVersion();
String specVersion = String.format( "%s.%s", version.getMajorVersion(), version.getMinorVersion() );
addManifestAttribute( m, entries, "Specification-Version", specVersion );
}
catch ( OverConstrainedVersionException e )
{
throw new ManifestException( "Failed to get selected artifact version to calculate"
+ " the specification version: " + e.getMessage() );
}

if ( project.getOrganization() != null )
{
addManifestAttribute( m, entries, "Specification-Vendor", project.getOrganization().getName() );
}
handleSpecificationEntries( project, entries, m );
}

if ( config.isAddDefaultImplementationEntries() )
{
addManifestAttribute( m, entries, "Implementation-Title", project.getName() );
addManifestAttribute( m, entries, "Implementation-Version", project.getVersion() );
// MJAR-5
addManifestAttribute( m, entries, "Implementation-Vendor-Id", project.getGroupId() );

if ( project.getOrganization() != null )
{
addManifestAttribute( m, entries, "Implementation-Vendor", project.getOrganization().getName() );
}

if ( project.getUrl() != null )
{
addManifestAttribute( m, entries, "Implementation-URL", project.getUrl() );
}
handleImplementationEntries( project, entries, m );
}

String mainClass = config.getMainClass();
Expand All @@ -430,57 +375,134 @@ else if ( ManifestConfiguration.CLASSPATH_LAYOUT_TYPE_CUSTOM.equals( layoutType
addManifestAttribute( m, entries, "Main-Class", mainClass );
}

// Added extensions
if ( config.isAddExtensions() )
{
// TODO: this is only for applets - should we distinguish them as a packaging?
StringBuilder extensionsList = new StringBuilder();
Set<Artifact> artifacts = (Set<Artifact>) project.getArtifacts();
handleExtensions( project, entries, m );
}

return m;
}

private void handleExtraExpression( Artifact artifact, List<ValueSource> valueSources )
{
valueSources.add( new PrefixedObjectValueSource( ARTIFACT_EXPRESSION_PREFIXES, artifact,
true ) );
valueSources.add( new PrefixedObjectValueSource( ARTIFACT_EXPRESSION_PREFIXES,
artifact.getArtifactHandler(), true ) );

Properties extraExpressions = new Properties();
// FIXME: This query method SHOULD NOT affect the internal
// state of the artifact version, but it does.
if ( !artifact.isSnapshot() )
{
extraExpressions.setProperty( "baseVersion", artifact.getVersion() );
}

extraExpressions.setProperty( "groupIdPath", artifact.getGroupId().replace( '.', '/' ) );
if ( StringUtils.isNotEmpty( artifact.getClassifier() ) )
{
extraExpressions.setProperty( "dashClassifier", "-" + artifact.getClassifier() );
extraExpressions.setProperty( "dashClassifier?", "-" + artifact.getClassifier() );
}
else
{
extraExpressions.setProperty( "dashClassifier", "" );
extraExpressions.setProperty( "dashClassifier?", "" );
}
valueSources.add( new PrefixedPropertiesValueSource( ARTIFACT_EXPRESSION_PREFIXES,
extraExpressions, true ) );
}

private void handleExtensions( MavenProject project, Map<String, String> entries, Manifest m )
throws ManifestException
{
// TODO: this is only for applets - should we distinguish them as a packaging?
StringBuilder extensionsList = new StringBuilder();
Set<Artifact> artifacts = (Set<Artifact>) project.getArtifacts();

for ( Artifact artifact : artifacts )
for ( Artifact artifact : artifacts )
{
if ( !Artifact.SCOPE_TEST.equals( artifact.getScope() ) )
{
if ( !Artifact.SCOPE_TEST.equals( artifact.getScope() ) )
if ( "jar".equals( artifact.getType() ) )
{
if ( "jar".equals( artifact.getType() ) )
if ( extensionsList.length() > 0 )
{
if ( extensionsList.length() > 0 )
{
extensionsList.append( " " );
}
extensionsList.append( artifact.getArtifactId() );
extensionsList.append( " " );
}
extensionsList.append( artifact.getArtifactId() );
}
}
}

if ( extensionsList.length() > 0 )
{
addManifestAttribute( m, entries, "Extension-List", extensionsList.toString() );
}
if ( extensionsList.length() > 0 )
{
addManifestAttribute( m, entries, "Extension-List", extensionsList.toString() );
}

for ( Object artifact1 : artifacts )
for ( Object artifact1 : artifacts )
{
// TODO: the correct solution here would be to have an extension type, and to read
// the real extension values either from the artifact's manifest or some part of the POM
Artifact artifact = (Artifact) artifact1;
if ( "jar".equals( artifact.getType() ) )
{
// TODO: the correct solution here would be to have an extension type, and to read
// the real extension values either from the artifact's manifest or some part of the POM
Artifact artifact = (Artifact) artifact1;
if ( "jar".equals( artifact.getType() ) )
{
String artifactId = artifact.getArtifactId().replace( '.', '_' );
String ename = artifactId + "-Extension-Name";
addManifestAttribute( m, entries, ename, artifact.getArtifactId() );
String iname = artifactId + "-Implementation-Version";
addManifestAttribute( m, entries, iname, artifact.getVersion() );
String artifactId = artifact.getArtifactId().replace( '.', '_' );
String ename = artifactId + "-Extension-Name";
addManifestAttribute( m, entries, ename, artifact.getArtifactId() );
String iname = artifactId + "-Implementation-Version";
addManifestAttribute( m, entries, iname, artifact.getVersion() );

if ( artifact.getRepository() != null )
{
iname = artifactId + "-Implementation-URL";
String url = artifact.getRepository().getUrl() + "/" + artifact.toString();
addManifestAttribute( m, entries, iname, url );
}
if ( artifact.getRepository() != null )
{
iname = artifactId + "-Implementation-URL";
String url = artifact.getRepository().getUrl() + "/" + artifact.toString();
addManifestAttribute( m, entries, iname, url );
}
}
}
}

return m;
private void handleImplementationEntries( MavenProject project, Map<String, String> entries, Manifest m )
throws ManifestException
{
addManifestAttribute( m, entries, "Implementation-Title", project.getName() );
addManifestAttribute( m, entries, "Implementation-Version", project.getVersion() );
// MJAR-5
addManifestAttribute( m, entries, "Implementation-Vendor-Id", project.getGroupId() );

if ( project.getOrganization() != null )
{
addManifestAttribute( m, entries, "Implementation-Vendor", project.getOrganization().getName() );
}

if ( project.getUrl() != null )
{
addManifestAttribute( m, entries, "Implementation-URL", project.getUrl() );
}
}

private void handleSpecificationEntries( MavenProject project, Map<String, String> entries, Manifest m )
throws ManifestException
{
addManifestAttribute( m, entries, "Specification-Title", project.getName() );

try
{
ArtifactVersion version = project.getArtifact().getSelectedVersion();
String specVersion = String.format( "%s.%s", version.getMajorVersion(), version.getMinorVersion() );
addManifestAttribute( m, entries, "Specification-Version", specVersion );
}
catch ( OverConstrainedVersionException e )
{
throw new ManifestException( "Failed to get selected artifact version to calculate"
+ " the specification version: " + e.getMessage() );
}

if ( project.getOrganization() != null )
{
addManifestAttribute( m, entries, "Specification-Vendor", project.getOrganization().getName() );
}
}

private void addCustomEntries( Manifest m, Map<String, String> entries, ManifestConfiguration config )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,6 @@ public boolean isAddExtensions()

assertNotNull( manifest.getMainAttributes() );

for ( Map.Entry<String, Attributes> entry : manifest.getEntries().entrySet() )
{
System.out.println( entry.getKey() + " " + entry.getValue().getValue( "Extension-List" ) );

}

assertEquals( null, manifest.getMainAttributes().getValue( "Extension-List" ) );

MockArtifact artifact1 = new MockArtifact();
Expand Down

0 comments on commit 6e7683c

Please sign in to comment.