| 1 | |
package org.apache.maven.plugin.gpg; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
import java.io.File; |
| 23 | |
import java.io.IOException; |
| 24 | |
import java.util.ArrayList; |
| 25 | |
import java.util.Iterator; |
| 26 | |
import java.util.List; |
| 27 | |
|
| 28 | |
import org.apache.maven.artifact.Artifact; |
| 29 | |
import org.apache.maven.plugin.MojoExecutionException; |
| 30 | |
import org.apache.maven.plugin.MojoFailureException; |
| 31 | |
import org.apache.maven.project.MavenProject; |
| 32 | |
import org.apache.maven.project.MavenProjectHelper; |
| 33 | |
import org.codehaus.plexus.util.FileUtils; |
| 34 | |
import org.codehaus.plexus.util.SelectorUtils; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | 0 | public class GpgSignAttachedMojo |
| 47 | |
extends AbstractGpgMojo |
| 48 | |
{ |
| 49 | |
|
| 50 | 0 | private static final String DEFAULT_EXCLUDES[] = new String[] { "**/*.md5", "**/*.sha1", "**/*.asc" }; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
private boolean skip; |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
private String[] excludes; |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
private File ascDirectory; |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
protected MavenProject project; |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
private MavenProjectHelper projectHelper; |
| 93 | |
|
| 94 | |
public void execute() |
| 95 | |
throws MojoExecutionException, MojoFailureException |
| 96 | |
{ |
| 97 | 0 | if ( skip ) |
| 98 | |
{ |
| 99 | |
|
| 100 | 0 | return; |
| 101 | |
} |
| 102 | |
|
| 103 | 0 | if ( excludes == null || excludes.length == 0 ) |
| 104 | |
{ |
| 105 | 0 | excludes = DEFAULT_EXCLUDES; |
| 106 | |
} |
| 107 | 0 | String newExcludes[] = new String[excludes.length]; |
| 108 | 0 | for ( int i = 0; i < excludes.length; i++ ) |
| 109 | |
{ |
| 110 | |
String pattern; |
| 111 | 0 | pattern = excludes[i].trim().replace( '/', File.separatorChar ).replace( '\\', File.separatorChar ); |
| 112 | 0 | if ( pattern.endsWith( File.separator ) ) |
| 113 | |
{ |
| 114 | 0 | pattern += "**"; |
| 115 | |
} |
| 116 | 0 | newExcludes[i] = pattern; |
| 117 | |
} |
| 118 | 0 | excludes = newExcludes; |
| 119 | |
|
| 120 | 0 | GpgSigner signer = newSigner( project ); |
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | 0 | signer.setOutputDirectory( ascDirectory ); |
| 127 | 0 | signer.setBuildDirectory( new File( project.getBuild().getDirectory() ) ); |
| 128 | 0 | signer.setBaseDirectory( project.getBasedir() ); |
| 129 | |
|
| 130 | 0 | List signingBundles = new ArrayList(); |
| 131 | |
|
| 132 | 0 | if ( !"pom".equals( project.getPackaging() ) ) |
| 133 | |
{ |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | 0 | Artifact artifact = project.getArtifact(); |
| 139 | |
|
| 140 | 0 | File file = artifact.getFile(); |
| 141 | |
|
| 142 | 0 | if ( file != null && file.isFile() ) |
| 143 | |
{ |
| 144 | 0 | getLog().debug( "Generating signature for " + file ); |
| 145 | |
|
| 146 | 0 | File projectArtifactSignature = signer.generateSignatureForArtifact( file ); |
| 147 | |
|
| 148 | 0 | if ( projectArtifactSignature != null ) |
| 149 | |
{ |
| 150 | 0 | signingBundles.add( new SigningBundle( artifact.getArtifactHandler().getExtension(), |
| 151 | |
projectArtifactSignature ) ); |
| 152 | |
} |
| 153 | 0 | } |
| 154 | 0 | else if ( project.getAttachedArtifacts().isEmpty() ) |
| 155 | |
{ |
| 156 | 0 | throw new MojoFailureException( "The project artifact has not been assembled yet. " |
| 157 | |
+ "Please do not invoke this goal before the lifecycle phase \"package\"." ); |
| 158 | |
} |
| 159 | |
else |
| 160 | |
{ |
| 161 | 0 | getLog().debug( "Main artifact not assembled, skipping signature generation" ); |
| 162 | |
} |
| 163 | |
} |
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | 0 | File pomToSign = new File( project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".pom" ); |
| 170 | |
|
| 171 | |
try |
| 172 | |
{ |
| 173 | 0 | FileUtils.copyFile( project.getFile(), pomToSign ); |
| 174 | |
} |
| 175 | 0 | catch ( IOException e ) |
| 176 | |
{ |
| 177 | 0 | throw new MojoExecutionException( "Error copying POM for signing.", e ); |
| 178 | 0 | } |
| 179 | |
|
| 180 | 0 | getLog().debug( "Generating signature for " + pomToSign ); |
| 181 | |
|
| 182 | 0 | File pomSignature = signer.generateSignatureForArtifact( pomToSign ); |
| 183 | |
|
| 184 | 0 | if ( pomSignature != null ) |
| 185 | |
{ |
| 186 | 0 | signingBundles.add( new SigningBundle( "pom", pomSignature ) ); |
| 187 | |
} |
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | 0 | for ( Iterator i = project.getAttachedArtifacts().iterator(); i.hasNext(); ) |
| 194 | |
{ |
| 195 | 0 | Artifact artifact = (Artifact) i.next(); |
| 196 | |
|
| 197 | 0 | File file = artifact.getFile(); |
| 198 | |
|
| 199 | 0 | getLog().debug( "Generating signature for " + file ); |
| 200 | |
|
| 201 | 0 | File signature = signer.generateSignatureForArtifact( file ); |
| 202 | |
|
| 203 | 0 | if ( signature != null ) |
| 204 | |
{ |
| 205 | 0 | signingBundles.add( new SigningBundle( artifact.getArtifactHandler().getExtension(), |
| 206 | |
artifact.getClassifier(), signature ) ); |
| 207 | |
} |
| 208 | 0 | } |
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | 0 | for ( Iterator i = signingBundles.iterator(); i.hasNext(); ) |
| 215 | |
{ |
| 216 | 0 | SigningBundle bundle = (SigningBundle) i.next(); |
| 217 | |
|
| 218 | 0 | projectHelper.attachArtifact( project, bundle.getExtension() + GpgSigner.SIGNATURE_EXTENSION, |
| 219 | |
bundle.getClassifier(), bundle.getSignature() ); |
| 220 | 0 | } |
| 221 | 0 | } |
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
protected boolean isExcluded( String name ) |
| 231 | |
{ |
| 232 | 0 | for ( int i = 0; i < excludes.length; i++ ) |
| 233 | |
{ |
| 234 | 0 | if ( SelectorUtils.matchPath( excludes[i], name ) ) |
| 235 | |
{ |
| 236 | 0 | return true; |
| 237 | |
} |
| 238 | |
} |
| 239 | 0 | return false; |
| 240 | |
} |
| 241 | |
|
| 242 | |
} |