19
19
package org .apache .maven .plugins .gpg ;
20
20
21
21
import java .io .File ;
22
- import java .io .IOException ;
23
- import java .nio .file .Path ;
24
- import java .util .ArrayList ;
25
22
import java .util .List ;
26
23
27
- import org .apache .maven .artifact .Artifact ;
28
24
import org .apache .maven .plugin .MojoExecutionException ;
29
25
import org .apache .maven .plugin .MojoFailureException ;
30
26
import org .apache .maven .plugins .annotations .Component ;
33
29
import org .apache .maven .plugins .annotations .Parameter ;
34
30
import org .apache .maven .project .MavenProject ;
35
31
import org .apache .maven .project .MavenProjectHelper ;
36
- import org .codehaus .plexus .util .FileUtils ;
37
- import org .codehaus .plexus .util .SelectorUtils ;
38
32
39
33
/**
40
34
* Sign project artifact, the POM, and attached artifacts with GnuPG for deployment.
46
40
@ Mojo (name = "sign" , defaultPhase = LifecyclePhase .VERIFY , threadSafe = true )
47
41
public class GpgSignAttachedMojo extends AbstractGpgMojo {
48
42
49
- private static final String DEFAULT_EXCLUDES [] =
50
- new String [] {"**/*.md5" , "**/*.sha1" , "**/*.sha256" , "**/*.sha512" , "**/*.asc" };
51
-
52
43
/**
53
44
* Skip doing the gpg signing.
54
45
*/
@@ -57,7 +48,7 @@ public class GpgSignAttachedMojo extends AbstractGpgMojo {
57
48
58
49
/**
59
50
* A list of files to exclude from being signed. Can contain Ant-style wildcards and double wildcards. The default
60
- * excludes are <code>**/*.md5 **/*.sha1 **/*.sha256 **/*.sha512 **/*.asc</code>.
51
+ * excludes are <code>**/*.md5 **/*.sha1 **/*.sha256 **/*.sha512 **/*.asc</code>.
61
52
*
62
53
* @since 1.0-alpha-4
63
54
*/
@@ -91,135 +82,32 @@ public void execute() throws MojoExecutionException, MojoFailureException {
91
82
return ;
92
83
}
93
84
94
- if (excludes == null || excludes .length == 0 ) {
95
- excludes = DEFAULT_EXCLUDES ;
96
- }
97
- String newExcludes [] = new String [excludes .length ];
98
- for (int i = 0 ; i < excludes .length ; i ++) {
99
- String pattern ;
100
- pattern = excludes [i ].trim ().replace ('/' , File .separatorChar ).replace ('\\' , File .separatorChar );
101
- if (pattern .endsWith (File .separator )) {
102
- pattern += "**" ;
103
- }
104
- newExcludes [i ] = pattern ;
105
- }
106
- excludes = newExcludes ;
85
+ // ----------------------------------------------------------------------------
86
+ // Collect files to sign
87
+ // ----------------------------------------------------------------------------
107
88
108
- AbstractGpgSigner signer = newSigner (project );
89
+ FilesCollector collector = new FilesCollector (project , excludes , getLog ());
90
+ List <FilesCollector .Item > items = collector .collect ();
109
91
110
92
// ----------------------------------------------------------------------------
111
- // What we need to generateSignatureForArtifact here
93
+ // Sign collected files and attach all the signatures
112
94
// ----------------------------------------------------------------------------
113
95
96
+ AbstractGpgSigner signer = newSigner (project );
114
97
signer .setOutputDirectory (ascDirectory );
115
98
signer .setBuildDirectory (new File (project .getBuild ().getDirectory ()));
116
99
signer .setBaseDirectory (project .getBasedir ());
117
100
118
- List <SigningBundle > signingBundles = new ArrayList <>();
119
-
120
- if (!"pom" .equals (project .getPackaging ())) {
121
- // ----------------------------------------------------------------------------
122
- // Project artifact
123
- // ----------------------------------------------------------------------------
124
-
125
- Artifact artifact = project .getArtifact ();
126
-
127
- File file = artifact .getFile ();
128
-
129
- if (file != null && file .isFile ()) {
130
- getLog ().debug ("Generating signature for " + file );
101
+ for (FilesCollector .Item item : items ) {
102
+ getLog ().debug ("Generating signature for " + item .getFile ());
131
103
132
- File projectArtifactSignature = signer .generateSignatureForArtifact (file );
104
+ File signature = signer .generateSignatureForArtifact (item . getFile () );
133
105
134
- if (projectArtifactSignature != null ) {
135
- signingBundles .add (
136
- new SigningBundle (artifact .getArtifactHandler ().getExtension (), projectArtifactSignature ));
137
- }
138
- } else if (project .getAttachedArtifacts ().isEmpty ()) {
139
- throw new MojoFailureException ("The project artifact has not been assembled yet. "
140
- + "Please do not invoke this goal before the lifecycle phase \" package\" ." );
141
- } else {
142
- getLog ().debug ("Main artifact not assembled, skipping signature generation" );
143
- }
144
- }
145
-
146
- // ----------------------------------------------------------------------------
147
- // POM
148
- // ----------------------------------------------------------------------------
149
-
150
- File pomToSign =
151
- new File (project .getBuild ().getDirectory (), project .getBuild ().getFinalName () + ".pom" );
152
-
153
- try {
154
- FileUtils .copyFile (project .getFile (), pomToSign );
155
- } catch (IOException e ) {
156
- throw new MojoExecutionException ("Error copying POM for signing." , e );
157
- }
158
-
159
- getLog ().debug ("Generating signature for " + pomToSign );
160
-
161
- File pomSignature = signer .generateSignatureForArtifact (pomToSign );
162
-
163
- if (pomSignature != null ) {
164
- signingBundles .add (new SigningBundle ("pom" , pomSignature ));
165
- }
166
-
167
- // ----------------------------------------------------------------------------
168
- // Attached artifacts
169
- // ----------------------------------------------------------------------------
170
-
171
- for (Object o : project .getAttachedArtifacts ()) {
172
- Artifact artifact = (Artifact ) o ;
173
-
174
- File file = artifact .getFile ();
175
-
176
- if (isExcluded (artifact )) {
177
- getLog ().debug ("Skipping generation of signature for excluded " + file );
178
- continue ;
179
- }
180
-
181
- getLog ().debug ("Generating signature for " + file );
182
-
183
- File signature = signer .generateSignatureForArtifact (file );
184
-
185
- if (signature != null ) {
186
- signingBundles .add (new SigningBundle (
187
- artifact .getArtifactHandler ().getExtension (), artifact .getClassifier (), signature ));
188
- }
189
- }
190
-
191
- // ----------------------------------------------------------------------------
192
- // Attach all the signatures
193
- // ----------------------------------------------------------------------------
194
-
195
- for (SigningBundle bundle : signingBundles ) {
196
106
projectHelper .attachArtifact (
197
107
project ,
198
- bundle .getExtension () + AbstractGpgSigner .SIGNATURE_EXTENSION ,
199
- bundle .getClassifier (),
200
- bundle .getSignature ());
201
- }
202
- }
203
-
204
- /**
205
- * Tests whether or not a name matches against at least one exclude pattern.
206
- *
207
- * @param artifact The artifact to match. Must not be <code>null</code>.
208
- * @return <code>true</code> when the name matches against at least one exclude pattern, or <code>false</code>
209
- * otherwise.
210
- */
211
- protected boolean isExcluded (Artifact artifact ) {
212
- final Path projectBasePath = project .getBasedir ().toPath ();
213
- final Path artifactPath = artifact .getFile ().toPath ();
214
- final String relativeArtifactPath =
215
- projectBasePath .relativize (artifactPath ).toString ();
216
-
217
- for (String exclude : excludes ) {
218
- if (SelectorUtils .matchPath (exclude , relativeArtifactPath )) {
219
- return true ;
220
- }
108
+ item .getExtension () + AbstractGpgSigner .SIGNATURE_EXTENSION ,
109
+ item .getClassifier (),
110
+ signature );
221
111
}
222
-
223
- return false ;
224
112
}
225
113
}
0 commit comments