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 public class GpgSignAttachedMojo
47 extends AbstractGpgMojo
48 {
49
50 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 if ( skip )
98 {
99
100 return;
101 }
102
103 if ( excludes == null || excludes.length == 0 )
104 {
105 excludes = DEFAULT_EXCLUDES;
106 }
107 String newExcludes[] = new String[excludes.length];
108 for ( int i = 0; i < excludes.length; i++ )
109 {
110 String pattern;
111 pattern = excludes[i].trim().replace( '/', File.separatorChar ).replace( '\\', File.separatorChar );
112 if ( pattern.endsWith( File.separator ) )
113 {
114 pattern += "**";
115 }
116 newExcludes[i] = pattern;
117 }
118 excludes = newExcludes;
119
120 GpgSigner signer = newSigner( project );
121
122
123
124
125
126 signer.setOutputDirectory( ascDirectory );
127 signer.setBuildDirectory( new File( project.getBuild().getDirectory() ) );
128 signer.setBaseDirectory( project.getBasedir() );
129
130 List signingBundles = new ArrayList();
131
132 if ( !"pom".equals( project.getPackaging() ) )
133 {
134
135
136
137
138 Artifact artifact = project.getArtifact();
139
140 File file = artifact.getFile();
141
142 if ( file != null && file.isFile() )
143 {
144 getLog().debug( "Generating signature for " + file );
145
146 File projectArtifactSignature = signer.generateSignatureForArtifact( file );
147
148 if ( projectArtifactSignature != null )
149 {
150 signingBundles.add( new SigningBundle( artifact.getArtifactHandler().getExtension(),
151 projectArtifactSignature ) );
152 }
153 }
154 else if ( project.getAttachedArtifacts().isEmpty() )
155 {
156 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 getLog().debug( "Main artifact not assembled, skipping signature generation" );
162 }
163 }
164
165
166
167
168
169 File pomToSign = new File( project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".pom" );
170
171 try
172 {
173 FileUtils.copyFile( project.getFile(), pomToSign );
174 }
175 catch ( IOException e )
176 {
177 throw new MojoExecutionException( "Error copying POM for signing.", e );
178 }
179
180 getLog().debug( "Generating signature for " + pomToSign );
181
182 File pomSignature = signer.generateSignatureForArtifact( pomToSign );
183
184 if ( pomSignature != null )
185 {
186 signingBundles.add( new SigningBundle( "pom", pomSignature ) );
187 }
188
189
190
191
192
193 for ( Iterator i = project.getAttachedArtifacts().iterator(); i.hasNext(); )
194 {
195 Artifact artifact = (Artifact) i.next();
196
197 File file = artifact.getFile();
198
199 getLog().debug( "Generating signature for " + file );
200
201 File signature = signer.generateSignatureForArtifact( file );
202
203 if ( signature != null )
204 {
205 signingBundles.add( new SigningBundle( artifact.getArtifactHandler().getExtension(),
206 artifact.getClassifier(), signature ) );
207 }
208 }
209
210
211
212
213
214 for ( Iterator i = signingBundles.iterator(); i.hasNext(); )
215 {
216 SigningBundle bundle = (SigningBundle) i.next();
217
218 projectHelper.attachArtifact( project, bundle.getExtension() + GpgSigner.SIGNATURE_EXTENSION,
219 bundle.getClassifier(), bundle.getSignature() );
220 }
221 }
222
223
224
225
226
227
228
229
230 protected boolean isExcluded( String name )
231 {
232 for ( int i = 0; i < excludes.length; i++ )
233 {
234 if ( SelectorUtils.matchPath( excludes[i], name ) )
235 {
236 return true;
237 }
238 }
239 return false;
240 }
241
242 }