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  public class SiteJarMojo extends SiteMojo {
54      private static final String[] DEFAULT_ARCHIVE_EXCLUDES = new String[] {};
55  
56      private static final String[] DEFAULT_ARCHIVE_INCLUDES = new String[] {"**/**"};
57  
58      /**
59       * Specifies the directory where the generated jar file will be put.
60       */
61      @Parameter(property = "project.build.directory", required = true)
62      private String jarOutputDirectory;
63  
64      /**
65       * Specifies the filename that will be used for the generated jar file.
66       * Please note that "-site" will be appended to the file name.
67       */
68      @Parameter(property = "project.build.finalName", required = true)
69      private String finalName;
70  
71      /**
72       * Used for attaching the artifact in the project.
73       */
74      @Component
75      private MavenProjectHelper projectHelper;
76  
77      /**
78       * Specifies whether to attach the generated artifact to the project.
79       */
80      @Parameter(property = "site.attach", defaultValue = "true")
81      private boolean attach;
82  
83      /**
84       * The Jar archiver.
85       *
86       * @since 3.1
87       */
88      @Component(role = Archiver.class, hint = "jar")
89      private JarArchiver jarArchiver;
90  
91      /**
92       * The archive configuration to use.
93       * See <a href="http://maven.apache.org/shared/maven-archiver/index.html">Maven Archiver Reference</a>.
94       *
95       * @since 3.1
96       */
97      @Parameter
98      private MavenArchiveConfiguration archive = new MavenArchiveConfiguration();
99  
100     /**
101      * List of files to include. Specified as file set patterns which are relative to the input directory whose contents
102      * is being packaged into the JAR.
103      *
104      * @since 3.1
105      */
106     @Parameter
107     private String[] archiveIncludes;
108 
109     /**
110      * List of files to exclude. Specified as file set patterns which are relative to the input directory whose contents
111      * is being packaged into the JAR.
112      *
113      * @since 3.1
114      */
115     @Parameter
116     private String[] archiveExcludes;
117 
118     /**
119      * Timestamp for reproducible output archive entries, either formatted as ISO 8601
120      * <code>yyyy-MM-dd'T'HH:mm:ssXXX</code> or as an int representing seconds since the epoch (like
121      * <a href="https://reproducible-builds.org/docs/source-date-epoch/">SOURCE_DATE_EPOCH</a>).
122      *
123      * @since 3.9.0
124      */
125     @Parameter(defaultValue = "${project.build.outputTimestamp}")
126     private String outputTimestamp;
127 
128     /**
129      * @see org.apache.maven.plugin.Mojo#execute()
130      */
131     public void execute() throws MojoExecutionException, MojoFailureException {
132         if (skip) {
133             getLog().info("maven.site.skip = true: Skipping jar generation");
134             return;
135         }
136 
137         super.execute();
138 
139         try {
140             File outputFile =
141                     createArchive(outputDirectory, finalName + "-" + getClassifier() + "." + getArtifactType());
142 
143             if (attach) {
144                 projectHelper.attachArtifact(project, getArtifactType(), getClassifier(), outputFile);
145             } else {
146                 getLog().info("NOT adding site jar to the list of attached artifacts.");
147             }
148         } catch (ArchiverException | IOException | ManifestException | DependencyResolutionRequiredException e) {
149             throw new MojoExecutionException("Error while creating archive.", e);
150         }
151     }
152 
153     protected String getArtifactType() {
154         return "jar";
155     }
156 
157     protected String getClassifier() {
158         return "site";
159     }
160 
161     /**
162      * Method that creates the jar file.
163      *
164      * @param siteDirectory the directory where the site files are located
165      * @param jarFilename   the filename of the created jar file
166      * @return a File object that contains the created jar file
167      * @throws ArchiverException
168      * @throws IOException
169      * @throws ManifestException
170      * @throws DependencyResolutionRequiredException
171      */
172     private File createArchive(File siteDirectory, String jarFilename)
173             throws ArchiverException, IOException, ManifestException, DependencyResolutionRequiredException {
174         File siteJar = new File(jarOutputDirectory, jarFilename);
175 
176         MavenArchiver archiver = new MavenArchiver();
177         archiver.setCreatedBy("Maven Site Plugin", "org.apache.maven.plugins", "maven-site-plugin");
178 
179         archiver.setArchiver(this.jarArchiver);
180 
181         archiver.setOutputFile(siteJar);
182 
183         // configure for Reproducible Builds based on outputTimestamp value
184         archiver.configureReproducibleBuild(outputTimestamp);
185 
186         if (!siteDirectory.isDirectory()) {
187             getLog().warn("JAR will be empty - no content was marked for inclusion!");
188         } else {
189             archiver.getArchiver().addDirectory(siteDirectory, getArchiveIncludes(), getArchiveExcludes());
190         }
191 
192         archiver.createArchive(getSession(), getProject(), archive);
193 
194         return siteJar;
195     }
196 
197     private String[] getArchiveIncludes() {
198         if (this.archiveIncludes != null && this.archiveIncludes.length > 0) {
199             return this.archiveIncludes;
200         }
201 
202         return DEFAULT_ARCHIVE_INCLUDES;
203     }
204 
205     private String[] getArchiveExcludes() {
206         if (this.archiveExcludes != null && this.archiveExcludes.length > 0) {
207             return this.archiveExcludes;
208         }
209         return DEFAULT_ARCHIVE_EXCLUDES;
210     }
211 }