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