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.HashMap; |
26 | |
import java.util.Iterator; |
27 | |
import java.util.List; |
28 | |
import java.util.Map; |
29 | |
|
30 | |
import org.apache.maven.artifact.Artifact; |
31 | |
import org.apache.maven.artifact.handler.ArtifactHandler; |
32 | |
import org.apache.maven.artifact.handler.DefaultArtifactHandler; |
33 | |
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager; |
34 | |
import org.apache.maven.plugin.MojoExecutionException; |
35 | |
import org.apache.maven.plugin.MojoFailureException; |
36 | |
import org.apache.maven.project.MavenProject; |
37 | |
import org.apache.maven.project.MavenProjectHelper; |
38 | |
import org.codehaus.plexus.util.FileUtils; |
39 | |
import org.codehaus.plexus.util.SelectorUtils; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | 0 | public class GpgSignAttachedMojo |
51 | |
extends AbstractGpgMojo |
52 | |
{ |
53 | |
|
54 | 0 | private static final String DEFAULT_EXCLUDES[] = new String[] { "**/*.md5", "**/*.sha1", "**/*.asc" }; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
private boolean skip; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
private String[] excludes; |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
private File ascDirectory; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
protected MavenProject project; |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
private MavenProjectHelper projectHelper; |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
private ArtifactHandlerManager artifactHandlerManager; |
106 | |
|
107 | |
public void execute() |
108 | |
throws MojoExecutionException, MojoFailureException |
109 | |
{ |
110 | 0 | if ( skip ) |
111 | |
{ |
112 | |
|
113 | 0 | return; |
114 | |
} |
115 | |
|
116 | 0 | if ( excludes == null || excludes.length == 0 ) |
117 | |
{ |
118 | 0 | excludes = DEFAULT_EXCLUDES; |
119 | |
} |
120 | 0 | String newExcludes[] = new String[excludes.length]; |
121 | 0 | for ( int i = 0; i < excludes.length; i++ ) |
122 | |
{ |
123 | |
String pattern; |
124 | 0 | pattern = excludes[i].trim().replace( '/', File.separatorChar ).replace( '\\', File.separatorChar ); |
125 | 0 | if ( pattern.endsWith( File.separator ) ) |
126 | |
{ |
127 | 0 | pattern += "**"; |
128 | |
} |
129 | 0 | newExcludes[i] = pattern; |
130 | |
} |
131 | 0 | excludes = newExcludes; |
132 | |
|
133 | 0 | GpgSigner signer = newSigner( project ); |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | 0 | signer.setOutputDirectory( ascDirectory ); |
140 | 0 | signer.setBuildDirectory( new File( project.getBuild().getDirectory() ) ); |
141 | 0 | signer.setBaseDirectory( project.getBasedir() ); |
142 | |
|
143 | 0 | List signingBundles = new ArrayList(); |
144 | |
|
145 | 0 | if ( !"pom".equals( project.getPackaging() ) ) |
146 | |
{ |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | 0 | File projectArtifact = project.getArtifact().getFile(); |
152 | |
|
153 | 0 | if ( projectArtifact == null ) |
154 | |
{ |
155 | 0 | throw new MojoFailureException( "The project artifact has not been assembled yet. " |
156 | |
+ "Please do not invoke this goal before the lifecycle phase \"package\"." ); |
157 | |
} |
158 | |
|
159 | 0 | File projectArtifactSignature = signer.generateSignatureForArtifact( projectArtifact ); |
160 | |
|
161 | 0 | if ( projectArtifactSignature != null ) |
162 | |
{ |
163 | 0 | signingBundles.add( new SigningBundle( project.getArtifact().getType(), projectArtifactSignature ) ); |
164 | |
} |
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | 0 | File pomToSign = new File( project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".pom" ); |
172 | |
|
173 | |
try |
174 | |
{ |
175 | 0 | FileUtils.copyFile( project.getFile(), pomToSign ); |
176 | |
} |
177 | 0 | catch ( IOException e ) |
178 | |
{ |
179 | 0 | throw new MojoExecutionException( "Error copying POM for signing.", e ); |
180 | 0 | } |
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 | File signature = signer.generateSignatureForArtifact( file ); |
200 | |
|
201 | 0 | if ( signature != null ) |
202 | |
{ |
203 | 0 | signingBundles.add( new SigningBundle( artifact.getType(), artifact.getClassifier(), signature ) ); |
204 | |
} |
205 | 0 | } |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | 0 | ArtifactHandler handler = new DefaultArtifactHandler( "asc" ); |
212 | |
|
213 | 0 | Map map = new HashMap(); |
214 | |
|
215 | 0 | map.put( "asc", handler ); |
216 | |
|
217 | 0 | artifactHandlerManager.addHandlers( map ); |
218 | |
|
219 | 0 | for ( Iterator i = signingBundles.iterator(); i.hasNext(); ) |
220 | |
{ |
221 | 0 | SigningBundle bundle = (SigningBundle) i.next(); |
222 | |
|
223 | 0 | ArtifactHandler ah = artifactHandlerManager.getArtifactHandler( bundle.getArtifactType() ); |
224 | |
|
225 | 0 | if ( bundle.getClassifier() != null && !"".equals( bundle.getClassifier() ) ) |
226 | |
{ |
227 | 0 | projectHelper.attachArtifact( project, "asc", bundle.getClassifier() + "." + ah.getExtension(), |
228 | |
bundle.getSignature() ); |
229 | |
} |
230 | |
else |
231 | |
{ |
232 | 0 | projectHelper.attachArtifact( project, ah.getExtension() + ".asc", null, bundle.getSignature() ); |
233 | |
} |
234 | 0 | } |
235 | 0 | } |
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
protected boolean isExcluded( String name ) |
245 | |
{ |
246 | 0 | for ( int i = 0; i < excludes.length; i++ ) |
247 | |
{ |
248 | 0 | if ( SelectorUtils.matchPath( excludes[i], name ) ) |
249 | |
{ |
250 | 0 | return true; |
251 | |
} |
252 | |
} |
253 | 0 | return false; |
254 | |
} |
255 | |
|
256 | |
} |