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.site.render;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.maven.archiver.MavenArchiveConfiguration;
25  import org.apache.maven.archiver.MavenArchiver;
26  import org.apache.maven.artifact.DependencyResolutionRequiredException;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
29  import org.apache.maven.plugins.annotations.Component;
30  import org.apache.maven.plugins.annotations.LifecyclePhase;
31  import org.apache.maven.plugins.annotations.Mojo;
32  import org.apache.maven.plugins.annotations.Parameter;
33  import org.apache.maven.plugins.annotations.ResolutionScope;
34  import org.apache.maven.project.MavenProjectHelper;
35  import org.codehaus.plexus.archiver.Archiver;
36  import org.codehaus.plexus.archiver.ArchiverException;
37  import org.codehaus.plexus.archiver.jar.JarArchiver;
38  import org.codehaus.plexus.archiver.jar.ManifestException;
39  
40  /**
41   * Bundles the site output into a JAR so that it can be deployed to a repository.
42   *
43   * @author <a href="mailto:mbeerman@yahoo.com">Matthew Beermann</a>
44   *
45   * @since 2.0-beta-6
46   */
47  // MSITE-665: requiresDependencyResolution workaround for MPLUGIN-253
48  @Mojo(
49          name = "jar",
50          defaultPhase = LifecyclePhase.PACKAGE,
51          requiresDependencyResolution = ResolutionScope.TEST,
52          requiresReports = true,
53          threadSafe = true)
54  public class SiteJarMojo extends SiteMojo {
55      private static final String[] DEFAULT_ARCHIVE_EXCLUDES = new String[] {};
56  
57      private static final String[] DEFAULT_ARCHIVE_INCLUDES = new String[] {"**/**"};
58  
59      /**
60       * Specifies the directory where the generated jar file will be put.
61       */
62      @Parameter(property = "project.build.directory", required = true)
63      private String jarOutputDirectory;
64  
65      /**
66       * Specifies the filename that will be used for the generated jar file.
67       * Please note that "-site" will be appended to the file name.
68       */
69      @Parameter(property = "project.build.finalName", required = true)
70      private String finalName;
71  
72      /**
73       * Used for attaching the artifact in the project.
74       */
75      @Component
76      private MavenProjectHelper projectHelper;
77  
78      /**
79       * Specifies whether to attach the generated artifact to the project.
80       */
81      @Parameter(property = "site.attach", defaultValue = "true")
82      private boolean attach;
83  
84      /**
85       * The Jar archiver.
86       *
87       * @since 3.1
88       */
89      @Component(role = Archiver.class, hint = "jar")
90      private JarArchiver jarArchiver;
91  
92      /**
93       * The archive configuration to use.
94       * See <a href="http://maven.apache.org/shared/maven-archiver/index.html">Maven Archiver Reference</a>.
95       *
96       * @since 3.1
97       */
98      @Parameter
99      private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
100 
101     /**
102      * List of files to include. Specified as file set patterns which are relative to the input directory whose contents
103      * is being packaged into the JAR.
104      *
105      * @since 3.1
106      */
107     @Parameter
108     private String[] archiveIncludes;
109 
110     /**
111      * List of files to exclude. Specified as file set patterns which are relative to the input directory whose contents
112      * is being packaged into the JAR.
113      *
114      * @since 3.1
115      */
116     @Parameter
117     private String[] archiveExcludes;
118 
119     /**
120      * @see org.apache.maven.plugin.Mojo#execute()
121      */
122     public void execute() throws MojoExecutionException, MojoFailureException {
123         if (skip) {
124             getLog().info("maven.site.skip = true: Skipping jar generation");
125             return;
126         }
127 
128         super.execute();
129 
130         try {
131             File outputFile =
132                     createArchive(outputDirectory, finalName + "-" + getClassifier() + "." + getArtifactType());
133 
134             if (attach) {
135                 projectHelper.attachArtifact(project, getArtifactType(), getClassifier(), outputFile);
136             } else {
137                 getLog().info("NOT adding site jar to the list of attached artifacts.");
138             }
139         } catch (ArchiverException | IOException | ManifestException | DependencyResolutionRequiredException e) {
140             throw new MojoExecutionException("Error while creating archive", e);
141         }
142     }
143 
144     protected String getArtifactType() {
145         return "jar";
146     }
147 
148     protected String getClassifier() {
149         return "site";
150     }
151 
152     /**
153      * Method that creates the jar file.
154      *
155      * @param siteDirectory the directory where the site files are located
156      * @param jarFilename   the filename of the created jar file
157      * @return a File object that contains the created jar file
158      * @throws ArchiverException
159      * @throws IOException
160      * @throws ManifestException
161      * @throws DependencyResolutionRequiredException
162      */
163     private File createArchive(File siteDirectory, String jarFilename)
164             throws ArchiverException, IOException, ManifestException, DependencyResolutionRequiredException {
165         File siteJar = new File(jarOutputDirectory, jarFilename);
166 
167         MavenArchiver archiver = new MavenArchiver();
168         archiver.setCreatedBy("Maven Site Plugin", "org.apache.maven.plugins", "maven-site-plugin");
169 
170         archiver.setArchiver(this.jarArchiver);
171 
172         archiver.setOutputFile(siteJar);
173 
174         // configure for Reproducible Builds based on outputTimestamp value
175         archiver.configureReproducibleBuild(outputTimestamp);
176 
177         if (!siteDirectory.isDirectory()) {
178             getLog().warn("JAR will be empty - no content was marked for inclusion!");
179         } else {
180             archiver.getArchiver().addDirectory(siteDirectory, getArchiveIncludes(), getArchiveExcludes());
181         }
182 
183         archiver.createArchive(getSession(), getProject(), archive);
184 
185         return siteJar;
186     }
187 
188     private String[] getArchiveIncludes() {
189         if (this.archiveIncludes != null && this.archiveIncludes.length > 0) {
190             return this.archiveIncludes;
191         }
192 
193         return DEFAULT_ARCHIVE_INCLUDES;
194     }
195 
196     private String[] getArchiveExcludes() {
197         if (this.archiveExcludes != null && this.archiveExcludes.length > 0) {
198             return this.archiveExcludes;
199         }
200         return DEFAULT_ARCHIVE_EXCLUDES;
201     }
202 }