View Javadoc
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", "**/*.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              // 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 }