View Javadoc
1   /*
2    * Copyright 2012 Brian Matthews
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.btmatthews.maven.plugins.crx;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.util.List;
22  
23  import org.apache.maven.execution.MavenSession;
24  import org.apache.maven.plugin.AbstractMojo;
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.apache.maven.plugins.annotations.Component;
28  import org.apache.maven.plugins.annotations.LifecyclePhase;
29  import org.apache.maven.plugins.annotations.Mojo;
30  import org.apache.maven.plugins.annotations.Parameter;
31  import org.apache.maven.project.MavenProject;
32  import org.apache.maven.project.MavenProjectHelper;
33  import org.apache.maven.shared.filtering.MavenFileFilter;
34  import org.apache.maven.shared.filtering.MavenFilteringException;
35  import org.apache.maven.shared.filtering.MavenResourcesExecution;
36  import org.apache.maven.shared.filtering.MavenResourcesFiltering;
37  import org.codehaus.plexus.archiver.ArchiverException;
38  import org.codehaus.plexus.util.FileUtils;
39  import org.codehaus.plexus.util.StringUtils;
40  
41  /**
42   * Implement the crx goal for the plug-in. The crx goal packages and signs a Chrome Browser Extension producing a file
43   * with a .crx extension.
44   *
45   * @author <a href="mailto:brian@btmatthews.com">Brian Matthews</a>
46   * @since 1.0.0
47   */
48  @Mojo(name = "crx", defaultPhase = LifecyclePhase.PACKAGE)
49  public class CRXMojo extends AbstractMojo {
50  
51      /**
52       * The PEM file containing the public/private key.
53       */
54      @Parameter(defaultValue = "${crxPEMFile}", required = true)
55      private File pemFile;
56  
57      /**
58       * The password for the PEM file.
59       */
60      @Parameter(defaultValue = "${crxPEMPassword}")
61      private String pemPassword;
62  
63      /**
64       * The source directory for the Chrome Extension.
65       */
66      @Parameter(defaultValue = "${basedir}/src/main/chrome", required = true)
67      private File crxSourceDirectory;
68  
69      /**
70       * A comma separated list of inclusion rules.
71       */
72      @Parameter(required = false)
73      private String packagingIncludes;
74  
75      /**
76       * A comma separated list of exclusion rules.
77       */
78      @Parameter(required = false)
79      private String packagingExcludes;
80  
81      /**
82       * The final name of the generated artifact.
83       */
84      @Parameter(defaultValue = "${project.build.finalName}", required = true)
85      private String finalName;
86  
87      /**
88       * The build target directory.
89       */
90      @Parameter(defaultValue = "${project.build.directory}", required = true)
91      private File outputDirectory;
92  
93      /**
94       * An optional classifier for the artifact.
95       */
96      @Parameter
97      private String classifier;
98  
99      /**
100      * Specify that the CRX sources should be filtered.
101      *
102      * @since 1.2.0
103      */
104     @Parameter(defaultValue = "false")
105     private boolean filtering;
106 
107     /**
108      * Filters (property files) to include during the interpolation of the pom.xml.
109      *
110      * @since 1.2.0
111      */
112     @Parameter
113     private List filters;
114 
115     /**
116      * A list of file extensions that should not be filtered if filtering is enabled.
117      *
118      * @since 1.2.0
119      */
120     @Parameter
121     private List nonFilteredFileExtensions;
122 
123     /**
124      * The Maven project.
125      */
126     @Parameter(defaultValue = "${project}", readonly = true, required = true)
127     private MavenProject project;
128 
129     /**
130      * The Maven project helper.
131      */
132     @Component
133     private MavenProjectHelper projectHelper;
134 
135     /**
136      * The archiver component that is used to package and sign the Google Chrome Extension.
137      */
138     @Component(role = CRXArchiver.class, hint = "crx")
139     private CRXArchiver crxArchiver;
140 
141     /**
142      * Used to copy the file with resource filtering.
143      *
144      * @since 1.2.0
145      */
146     @Component(role = MavenFileFilter.class, hint = "default")
147     private MavenFileFilter mavenFileFilter;
148 
149     /**
150      * Used to perform the file filtering.
151      *
152      * @since 1.2.0
153      */
154     @Component(role = MavenResourcesFiltering.class, hint = "default")
155     private MavenResourcesFiltering mavenResourcesFiltering;
156 
157     /**
158      * The current Maven session.
159      *
160      * @since 1.2.0
161      */
162     @Parameter(defaultValue = "${session}", readonly = true)
163     private MavenSession session;
164 
165     /**
166      * File filtering wrappers.
167      *
168      * @since 1.2.0
169      */
170     private List filterWrappers;
171 
172     /**
173      * Called when the Maven plug-in is executing. It creates an in-memory ZIP file of all the Chrome Extension
174      * source files, generates as signature using the private key from the PEM file, outputs a CRX file containing
175      * a header, the public key, the signature and the ZIP data.
176      *
177      * @throws MojoExecutionException If there was an error that should stop the build.
178      * @throws MojoFailureException   If there was an error but the build might be allowed to continue.
179      */
180     public final void execute() throws MojoExecutionException, MojoFailureException {
181 
182         // Make sure we have a manifest file for the CRX
183 
184         final File manifestFile = new File(crxSourceDirectory, "manifest.json");
185         if (!manifestFile.exists()) {
186             throw new MojoExecutionException("Missing manifest.json file");
187         }
188 
189         // Generate CRX file name
190 
191         final StringBuilder crxFilename = new StringBuilder();
192         crxFilename.append(finalName);
193         if (StringUtils.isNotEmpty(classifier)) {
194             crxFilename.append('-');
195             crxFilename.append(classifier);
196         }
197         final File crxDirectory = new File(outputDirectory, crxFilename.toString());
198         crxFilename.append(".crx");
199 
200         copyFiles(crxSourceDirectory, crxDirectory);
201 
202         // Generate the CRX file
203 
204         final File crxFile = new File(outputDirectory, crxFilename.toString());
205         final String[] includes = ParameterUtils.splitParameter(packagingIncludes);
206         final String[] excludes = ParameterUtils.splitParameter(packagingExcludes);
207 
208         crxArchiver.setPemFile(pemFile);
209         crxArchiver.setPemPassword(pemPassword);
210         crxArchiver.addDirectory(crxDirectory, includes, excludes);
211         crxArchiver.setDestFile(crxFile);
212 
213         try {
214             crxArchiver.createArchive();
215         } catch (final IOException e) {
216             throw new MojoExecutionException("Failed to package and sign the Google Chrome Extension", e);
217         } catch (final ArchiverException e) {
218             throw new MojoExecutionException(e.getMessage(), e);
219         }
220 
221         // Attach the artifact to the build life-cycle
222 
223         if (StringUtils.isNotEmpty(classifier)) {
224             projectHelper.attachArtifact(project, "crx", classifier, crxFile);
225         } else {
226             project.getArtifact().setFile(crxFile);
227         }
228     }
229 
230     /**
231      * Recursively copy from a source directory to a destination directory applying resource filtering if necessary.
232      *
233      * @param source      The source directory.
234      * @param destination The destination directory.
235      * @throws MojoExecutionException If there was an error during the recursive copying or filtering.
236      * @since 1.2.0
237      */
238     private void copyFiles(final File source, final File destination) throws MojoExecutionException {
239         try {
240             if (!destination.exists() && !destination.mkdirs()) {
241                 throw new MojoExecutionException("Could not create directory: " + destination.getAbsolutePath());
242             }
243             for (final File sourceItem : source.listFiles()) {
244                 final File destinationItem = new File(destination, sourceItem.getName());
245                 if (sourceItem.isDirectory()) {
246                     copyFiles(sourceItem, destinationItem);
247                 } else {
248                     if (filtering && !isNonFilteredExtension(sourceItem.getName())) {
249                         mavenFileFilter.copyFile(sourceItem, destinationItem, true, getFilterWrappers(), null);
250                     } else {
251                         FileUtils.copyFile(sourceItem, destinationItem);
252                     }
253                 }
254             }
255         } catch (final MavenFilteringException e) {
256             throw new MojoExecutionException("Failed to build filtering wrappers", e);
257         } catch (final IOException e) {
258             throw new MojoExecutionException("Error copying file: " + source.getAbsolutePath(), e);
259         }
260     }
261 
262     /**
263      * Determine whether the file name should be filtered or not based on the list of excluded file extensions.
264      *
265      * @param fileName The file name.
266      * @return {@code true} if the file extension is not excluded and the file should be filtered. Otherwise {@code
267      * false}.
268      * @throws MojoExecutionException If there was an error determining whether the file name should be filtered.
269      * @since 1.2.0
270      */
271     private boolean isNonFilteredExtension(final String fileName) throws MojoExecutionException {
272         return !mavenResourcesFiltering.filteredFileExtension(fileName, nonFilteredFileExtensions);
273     }
274 
275     /**
276      * Build a list of filter wrappers.
277      *
278      * @return The list of filter wrappers.
279      * @throws MojoExecutionException If there was a problem building the list of filter wrappers.
280      * @since 1.2.0
281      */
282     private List getFilterWrappers()
283             throws MojoExecutionException {
284         if (filterWrappers == null) {
285             try {
286                 final MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution();
287                 mavenResourcesExecution.setEscapeString("\\");
288                 filterWrappers = mavenFileFilter.getDefaultFilterWrappers(project, filters, true, session,
289                         mavenResourcesExecution);
290             } catch (final MavenFilteringException e) {
291                 throw new MojoExecutionException("Failed to build filtering wrappers: " + e.getMessage(), e);
292             }
293         }
294         return filterWrappers;
295     }
296 }