Skip to content

Commit 1e0472f

Browse files
committed
extract FilesCollector
1 parent af9ccfd commit 1e0472f

File tree

3 files changed

+192
-181
lines changed

3 files changed

+192
-181
lines changed
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.plugins.gpg;
20+
21+
import java.io.File;
22+
import java.io.IOException;
23+
import java.nio.file.Path;
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.plugin.logging.Log;
31+
import org.apache.maven.project.MavenProject;
32+
import org.codehaus.plexus.util.FileUtils;
33+
import org.codehaus.plexus.util.SelectorUtils;
34+
35+
/**
36+
* Collects project artifact, the POM, and attached artifacts to be signed.
37+
*
38+
* @since 3.1.0
39+
*/
40+
public class FilesCollector {
41+
private final MavenProject project;
42+
43+
private static final String DEFAULT_EXCLUDES[] =
44+
new String[] {"**/*.md5", "**/*.sha1", "**/*.sha256", "**/*.sha512", "**/*.asc"};
45+
46+
private final String[] excludes;
47+
48+
private final Log log;
49+
50+
public FilesCollector(MavenProject project, String[] excludes, Log log) {
51+
this.project = project;
52+
this.log = log;
53+
if (excludes == null || excludes.length == 0) {
54+
this.excludes = DEFAULT_EXCLUDES;
55+
return;
56+
}
57+
String newExcludes[] = new String[excludes.length];
58+
for (int i = 0; i < excludes.length; i++) {
59+
String pattern;
60+
pattern = excludes[i].trim().replace('/', File.separatorChar).replace('\\', File.separatorChar);
61+
if (pattern.endsWith(File.separator)) {
62+
pattern += "**";
63+
}
64+
newExcludes[i] = pattern;
65+
}
66+
this.excludes = newExcludes;
67+
}
68+
69+
public List<Item> collect() throws MojoExecutionException, MojoFailureException {
70+
List<Item> items = new ArrayList<>();
71+
72+
if (!"pom".equals(project.getPackaging())) {
73+
// ----------------------------------------------------------------------------
74+
// Project artifact
75+
// ----------------------------------------------------------------------------
76+
77+
Artifact artifact = project.getArtifact();
78+
79+
File file = artifact.getFile();
80+
81+
if (file != null && file.isFile()) {
82+
items.add(new Item(file, artifact.getArtifactHandler().getExtension()));
83+
} else if (project.getAttachedArtifacts().isEmpty()) {
84+
throw new MojoFailureException("The project artifact has not been assembled yet. "
85+
+ "Please do not invoke this goal before the lifecycle phase \"package\".");
86+
} else {
87+
log.debug("Main artifact not assembled, skipping signature generation");
88+
}
89+
}
90+
91+
// ----------------------------------------------------------------------------
92+
// POM
93+
// ----------------------------------------------------------------------------
94+
95+
File pomToSign =
96+
new File(project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".pom");
97+
98+
try {
99+
FileUtils.copyFile(project.getFile(), pomToSign);
100+
} catch (IOException e) {
101+
throw new MojoExecutionException("Error copying POM for signing.", e);
102+
}
103+
104+
items.add(new Item(pomToSign, "pom"));
105+
106+
// ----------------------------------------------------------------------------
107+
// Attached artifacts
108+
// ----------------------------------------------------------------------------
109+
110+
for (Artifact artifact : project.getAttachedArtifacts()) {
111+
File file = artifact.getFile();
112+
113+
if (isExcluded(artifact)) {
114+
log.debug("Skipping generation of signature for excluded " + file);
115+
continue;
116+
}
117+
118+
items.add(new Item(
119+
file,
120+
artifact.getClassifier(),
121+
artifact.getArtifactHandler().getExtension()));
122+
}
123+
124+
return items;
125+
}
126+
127+
/**
128+
* Tests whether or not a name matches against at least one exclude pattern.
129+
*
130+
* @param artifact The artifact to match. Must not be <code>null</code>.
131+
* @return <code>true</code> when the name matches against at least one exclude pattern, or <code>false</code>
132+
* otherwise.
133+
*/
134+
protected boolean isExcluded(Artifact artifact) {
135+
final Path projectBasePath = project.getBasedir().toPath();
136+
final Path artifactPath = artifact.getFile().toPath();
137+
final String relativeArtifactPath =
138+
projectBasePath.relativize(artifactPath).toString();
139+
140+
for (String exclude : excludes) {
141+
if (SelectorUtils.matchPath(exclude, relativeArtifactPath)) {
142+
return true;
143+
}
144+
}
145+
146+
return false;
147+
}
148+
149+
public static class Item {
150+
private final File file;
151+
152+
private final String classifier;
153+
154+
private final String extension;
155+
156+
public Item(File file, String classifier, String extension) {
157+
this.file = file;
158+
this.classifier = classifier;
159+
this.extension = extension;
160+
}
161+
162+
public Item(File file, String extension) {
163+
this(file, null, extension);
164+
}
165+
166+
public File getFile() {
167+
return file;
168+
}
169+
170+
public String getClassifier() {
171+
return classifier;
172+
}
173+
174+
public String getExtension() {
175+
return extension;
176+
}
177+
}
178+
}

src/main/java/org/apache/maven/plugins/gpg/GpgSignAttachedMojo.java

Lines changed: 14 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,8 @@
1919
package org.apache.maven.plugins.gpg;
2020

2121
import java.io.File;
22-
import java.io.IOException;
23-
import java.nio.file.Path;
24-
import java.util.ArrayList;
2522
import java.util.List;
2623

27-
import org.apache.maven.artifact.Artifact;
2824
import org.apache.maven.plugin.MojoExecutionException;
2925
import org.apache.maven.plugin.MojoFailureException;
3026
import org.apache.maven.plugins.annotations.Component;
@@ -33,8 +29,6 @@
3329
import org.apache.maven.plugins.annotations.Parameter;
3430
import org.apache.maven.project.MavenProject;
3531
import 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.
@@ -46,9 +40,6 @@
4640
@Mojo(name = "sign", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
4741
public 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>**&#47;*.md5 **&#47;*.sha1 **&#47;*.sha256 **&#47;*.sha512 **&#47;*.asc</code>.
51+
* excludes are <code>**&#47;*.md5 **&#47;*.sha1 **&#47;*.sha256 **&#47;*.sha512 **&#47;*.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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy