1919package org .apache .maven .plugins .gpg ;
2020
2121import java .io .File ;
22- import java .io .IOException ;
23- import java .nio .file .Path ;
24- import java .util .ArrayList ;
2522import java .util .List ;
2623
27- import org .apache .maven .artifact .Artifact ;
2824import org .apache .maven .plugin .MojoExecutionException ;
2925import org .apache .maven .plugin .MojoFailureException ;
3026import org .apache .maven .plugins .annotations .Component ;
3329import org .apache .maven .plugins .annotations .Parameter ;
3430import org .apache .maven .project .MavenProject ;
3531import org .apache .maven .project .MavenProjectHelper ;
36- import org .codehaus .plexus .util .FileUtils ;
37- import org .codehaus .plexus .util .SelectorUtils ;
3832
3933/**
4034 * Sign project artifact, the POM, and attached artifacts with GnuPG for deployment.
4640@ Mojo (name = "sign" , defaultPhase = LifecyclePhase .VERIFY , threadSafe = true )
4741public class GpgSignAttachedMojo extends AbstractGpgMojo {
4842
49- private static final String DEFAULT_EXCLUDES [] =
50- new String [] {"**/*.md5" , "**/*.sha1" , "**/*.sha256" , "**/*.sha512" , "**/*.asc" };
51-
5243 /**
5344 * Skip doing the gpg signing.
5445 */
@@ -57,7 +48,7 @@ public class GpgSignAttachedMojo extends AbstractGpgMojo {
5748
5849 /**
5950 * 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>.
6152 *
6253 * @since 1.0-alpha-4
6354 */
@@ -91,135 +82,32 @@ public void execute() throws MojoExecutionException, MojoFailureException {
9182 return ;
9283 }
9384
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+ // ----------------------------------------------------------------------------
10788
108- AbstractGpgSigner signer = newSigner (project );
89+ FilesCollector collector = new FilesCollector (project , excludes , getLog ());
90+ List <FilesCollector .Item > items = collector .collect ();
10991
11092 // ----------------------------------------------------------------------------
111- // What we need to generateSignatureForArtifact here
93+ // Sign collected files and attach all the signatures
11294 // ----------------------------------------------------------------------------
11395
96+ AbstractGpgSigner signer = newSigner (project );
11497 signer .setOutputDirectory (ascDirectory );
11598 signer .setBuildDirectory (new File (project .getBuild ().getDirectory ()));
11699 signer .setBaseDirectory (project .getBasedir ());
117100
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 ());
131103
132- File projectArtifactSignature = signer .generateSignatureForArtifact (file );
104+ File signature = signer .generateSignatureForArtifact (item . getFile () );
133105
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 ) {
196106 projectHelper .attachArtifact (
197107 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 );
221111 }
222-
223- return false ;
224112 }
225113}
0 commit comments