diff --git a/example-attach/pom.xml b/example-attach/pom.xml index 7b85229..3b911dc 100644 --- a/example-attach/pom.xml +++ b/example-attach/pom.xml @@ -28,6 +28,7 @@ attach-artifact-signatures + none attachsignatures diff --git a/plugin/src/main/java/org/neo4j/build/plugins/ease/AttachSignaturesMojo.java b/plugin/src/main/java/org/neo4j/build/plugins/ease/AttachSignaturesMojo.java index f78187f..0a4cbbf 100644 --- a/plugin/src/main/java/org/neo4j/build/plugins/ease/AttachSignaturesMojo.java +++ b/plugin/src/main/java/org/neo4j/build/plugins/ease/AttachSignaturesMojo.java @@ -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; @@ -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 @@ -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. * @@ -64,40 +58,71 @@ public class AttachSignaturesMojo extends AbstractMojo */ protected ArtifactFactory artifactFactory; + private final Set attachedIds = new HashSet(); + @Override public void execute() throws MojoExecutionException { - List artifacts = new ArrayList(); - 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 artifacts = new ArrayList( + 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 ); + } + } }