1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
37
38
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", "**/*.sigstore"};
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
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
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
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
129
130
131
132
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 }