Skip to content

Commit

Permalink
Made the attachsignatures goal clean up the project after the gpg plu…
Browse files Browse the repository at this point in the history
…gin execution.
  • Loading branch information
nawroth committed May 9, 2012
1 parent 08e6f13 commit d126395
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 32 deletions.
1 change: 1 addition & 0 deletions example-attach/pom.xml
Expand Up @@ -28,6 +28,7 @@
</execution>
<execution>
<id>attach-artifact-signatures</id>
<phase>none</phase>
<goals>
<goal>attachsignatures</goal>
</goals>
Expand Down
Expand Up @@ -20,8 +20,9 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
Expand All @@ -31,8 +32,8 @@
import org.codehaus.plexus.util.FileUtils;

/**
* Attaches all unattached signatures of signed artifacts in the build directory
* with the project.
* Attaches all signatures of signed artifacts to the project. Will fail on
* missing signatures.
*
* @goal attachsignatures
* @requiresProject true
Expand All @@ -48,13 +49,6 @@ public class AttachSignaturesMojo extends AbstractMojo
*/
private MavenProject project;

/**
* @parameter default-value="${project.attachedArtifacts}
* @required
* @readonly
*/
private List attachedArtifacts;

/**
* Used to create artifact instances.
*
Expand All @@ -64,40 +58,71 @@ public class AttachSignaturesMojo extends AbstractMojo
*/
protected ArtifactFactory artifactFactory;

private final Set<String> attachedIds = new HashSet<String>();

@Override
public void execute() throws MojoExecutionException
{
List<Artifact> artifacts = new ArrayList<Artifact>();
for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
{
Artifact artifact = (Artifact) i.next();
artifacts.add( artifact );
}
// simply attach the signature of all non-signature artifacts
List<Artifact> artifacts = new ArrayList<Artifact>(
project.getAttachedArtifacts() );
project.getAttachedArtifacts()
.clear();
for ( Artifact artifact : artifacts )
{
String type = artifact.getType();
if ( !type.endsWith( ".asc" ) )
if ( artifact.getArtifactId()
.equals( project.getArtifactId() ) && artifact.getGroupId()
.equals( project.getGroupId() ) )
{
File artifactFile = artifact.getFile();
String signaturePath = artifactFile.getAbsolutePath() + ".asc";
File signatureFile = FileUtils.getFile( signaturePath );
if ( signatureFile.exists() )
// only add back the project artifacts we want
if ( type.equals( "pom.asc" ) || type.equals( "pom" ) )
{
String classifier = null;
if ( artifact.hasClassifier() )
attach( artifact );
}
}
else
{
// re-attach and check if there's a signature to attach as well
attach( artifact );
if ( !type.endsWith( ".asc" ) )
{
File artifactFile = artifact.getFile();
String signaturePath = artifactFile.getAbsolutePath()
+ ".asc";
File signatureFile = FileUtils.getFile( signaturePath );
if ( signatureFile.exists() )
{
classifier = artifact.getClassifier();
String classifier = null;
if ( artifact.hasClassifier() )
{
classifier = artifact.getClassifier();
}
Artifact signatureArtifact = artifactFactory.createArtifactWithClassifier(
artifact.getGroupId(),
artifact.getArtifactId(),
artifact.getVersion(), type + ".asc",
classifier );
signatureArtifact.setFile( signatureFile );
attach( signatureArtifact );
}
else
{
throw new MojoExecutionException(
"Missing signature for artifact: " + artifact );
}
Artifact signatureArtifact = artifactFactory.createArtifactWithClassifier(
artifact.getGroupId(), artifact.getArtifactId(),
artifact.getVersion(), type + ".asc", classifier );
signatureArtifact.setFile( signatureFile );
project.addAttachedArtifact( signatureArtifact );
getLog().info(
"Attached signature: " + signatureArtifact.getId() );
}
}
}
}

private void attach( Artifact artifact )
{
String id = artifact.getId();
if ( !attachedIds.contains( id ) )
{
project.addAttachedArtifact( artifact );
attachedIds.add( id );
getLog().info( "Attached: " + id );
}
}
}

0 comments on commit d126395

Please sign in to comment.