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      * Timestamp for reproducible output archive entries, either formatted as ISO 8601
121      * <code>yyyy-MM-dd'T'HH:mm:ssXXX</code> or as an int representing seconds since the epoch (like
122      * <a href="https://reproducible-builds.org/docs/source-date-epoch/">SOURCE_DATE_EPOCH</a>).
123      *
124      * @since 3.9.0
125      */
126     @Parameter(defaultValue = "${project.build.outputTimestamp}")
127     private String outputTimestamp;
128 
129     /**
130      * @see org.apache.maven.plugin.Mojo#execute()
131      */
132     public void execute() throws MojoExecutionException, MojoFailureException {
133         if (skip) {
134             getLog().info("maven.site.skip = true: Skipping jar generation");
135             return;
136         }
137 
138         super.execute();
139 
140         try {
141             File outputFile =
142                     createArchive(outputDirectory, finalName + "-" + getClassifier() + "." + getArtifactType());
143 
144             if (attach) {
145                 projectHelper.attachArtifact(project, getArtifactType(), getClassifier(), outputFile);
146             } else {
147                 getLog().info("NOT adding site jar to the list of attached artifacts.");
148             }
149         } catch (ArchiverException | IOException | ManifestException | DependencyResolutionRequiredException e) {
150             throw new MojoExecutionException("Error while creating archive.", e);
151         }
152     }
153 
154     protected String getArtifactType() {
155         return "jar";
156     }
157 
158     protected String getClassifier() {
159         return "site";
160     }
161 
162     /**
163      * Method that creates the jar file.
164      *
165      * @param siteDirectory the directory where the site files are located
166      * @param jarFilename   the filename of the created jar file
167      * @return a File object that contains the created jar file
168      * @throws ArchiverException
169      * @throws IOException
170      * @throws ManifestException
171      * @throws DependencyResolutionRequiredException
172      */
173     private File createArchive(File siteDirectory, String jarFilename)
174             throws ArchiverException, IOException, ManifestException, DependencyResolutionRequiredException {
175         File siteJar = new File(jarOutputDirectory, jarFilename);
176 
177         MavenArchiver archiver = new MavenArchiver();
178         archiver.setCreatedBy("Maven Site Plugin", "org.apache.maven.plugins", "maven-site-plugin");
179 
180         archiver.setArchiver(this.jarArchiver);
181 
182         archiver.setOutputFile(siteJar);
183 
184         // configure for Reproducible Builds based on outputTimestamp value
185         archiver.configureReproducibleBuild(outputTimestamp);
186 
187         if (!siteDirectory.isDirectory()) {
188             getLog().warn("JAR will be empty - no content was marked for inclusion!");
189         } else {
190             archiver.getArchiver().addDirectory(siteDirectory, getArchiveIncludes(), getArchiveExcludes());
191         }
192 
193         archiver.createArchive(getSession(), getProject(), archive);
194 
195         return siteJar;
196     }
197 
198     private String[] getArchiveIncludes() {
199         if (this.archiveIncludes != null && this.archiveIncludes.length > 0) {
200             return this.archiveIncludes;
201         }
202 
203         return DEFAULT_ARCHIVE_INCLUDES;
204     }
205 
206     private String[] getArchiveExcludes() {
207         if (this.archiveExcludes != null && this.archiveExcludes.length > 0) {
208             return this.archiveExcludes;
209         }
210         return DEFAULT_ARCHIVE_EXCLUDES;
211     }
212 }