Index: src/main/java/org/apache/maven/plugin/gpg/GpgSignAttachedMojo.java =================================================================== --- src/main/java/org/apache/maven/plugin/gpg/GpgSignAttachedMojo.java (revision 771738) +++ src/main/java/org/apache/maven/plugin/gpg/GpgSignAttachedMojo.java (working copy) @@ -21,6 +21,7 @@ import java.io.File; import java.io.IOException; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -31,10 +32,15 @@ import org.apache.maven.artifact.handler.ArtifactHandler; import org.apache.maven.artifact.handler.DefaultArtifactHandler; import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; +import org.apache.maven.artifact.installer.ArtifactInstallationException; +import org.apache.maven.artifact.metadata.ArtifactMetadata; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.transform.ArtifactTransformation; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProjectHelper; +import org.apache.maven.project.artifact.ProjectArtifactMetadata; import org.apache.maven.settings.Settings; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.SelectorUtils; @@ -136,6 +142,24 @@ private ArtifactHandlerManager artifactHandlerManager; /** + * Maven ArtifactTransformation + * + * @component + * @required + * @readonly + */ + private ArtifactTransformation artifactTransformer; + /** + * Local maven repository. + * + * @parameter expression="${localRepository}" + * @required + * @readonly + */ + protected ArtifactRepository localRepository; + + + /** * @parameter expression="${settings}" * @required * @readonly @@ -205,7 +229,8 @@ signer.setBaseDirectory( project.getBasedir() ); List signingBundles = new ArrayList(); - + + if ( !"pom".equals( project.getPackaging() ) ) { // ---------------------------------------------------------------------------- @@ -221,16 +246,65 @@ signingBundles.add( new SigningBundle( project.getArtifact().getType(), projectArtifactSignature ) ); } } - + + // ---------------------------------------------------------------------------- // POM // ---------------------------------------------------------------------------- + File pomFile; + try { + // With Maven 2.1 and newer, the installer will transform the pom. Thus, we need to + // also transform the pom in order to sign the version that will be installed/deployed. + if ( "pom".equals( project.getPackaging() ) ) + { + boolean remove = project.getArtifact().getFile() == null; + project.getArtifact().setFile( project.getFile() ); + artifactTransformer.transformForInstall( project.getArtifact(), localRepository ); + pomFile = project.getArtifact().getFile(); + if (remove) + { + //put back to how it was + project.getArtifact().setFile( null ); + } + } + else + { + ProjectArtifactMetadata metadata = findMetadata( project.getArtifact() ); + boolean remove = false; + if ( metadata == null ) + { + //The transform for "non" pom projects requires the metadata to be there + //for the artifact so the pom can be found. + remove = true; + metadata = new ProjectArtifactMetadata( project.getArtifact(), project.getFile() ); + project.getArtifact().addMetadata( metadata ); + } + artifactTransformer.transformForInstall( project.getArtifact(), localRepository ); + pomFile = getMetaDataFile( metadata , project.getFile() ); + if ( remove ) + { + // The install plugin will barf if metadata already exists for the artifact. + // Thus, we need to remove the metadata that we added. + Iterator i = project.getArtifact().getMetadataList().iterator(); + while (i.hasNext()) + { + if (i.next() == metadata) + { + i.remove(); + } + } + } + } + } + catch ( ArtifactInstallationException ex ) + { + throw new MojoExecutionException( "Could not transform pom", ex ); + } File pomToSign = new File( project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".pom" ); - try { - FileUtils.copyFile( project.getFile(), pomToSign ); + FileUtils.copyFile( pomFile, pomToSign ); } catch ( IOException e ) { @@ -293,6 +367,35 @@ } } + private File getMetaDataFile(ProjectArtifactMetadata metadata, File defFile) { + // In Maven 2.1, the "tranformed" pom will be found in the file of the metadata. + // However, the getFile method doesn't exist in 2.0.x. Thus, we'll use a tiny + // bit of reflection to get the file. If that fails, we'll just use the default + // project file which is what we want for Maven 2.0.x. + try + { + Method m = metadata.getClass().getMethod( "getFile", new Class[0] ); + return (File)m.invoke( metadata, new Object[0] ); + } + catch (Exception e) + { + return defFile; + } + } + + private ProjectArtifactMetadata findMetadata(Artifact artifact) { + Iterator i = project.getArtifact().getMetadataList().iterator(); + while ( i.hasNext() ) + { + ArtifactMetadata met = (ArtifactMetadata) i.next(); + if ( met instanceof ProjectArtifactMetadata ) + { + return (ProjectArtifactMetadata) met; + } + } + return null; + } + /** * Tests whether or not a name matches against at least one exclude * pattern.