1 package org.apache.maven.plugins.site;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import org.apache.maven.archiver.MavenArchiveConfiguration;
26 import org.apache.maven.archiver.MavenArchiver;
27 import org.apache.maven.artifact.DependencyResolutionRequiredException;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.MojoFailureException;
30 import org.apache.maven.project.MavenProjectHelper;
31
32 import org.codehaus.plexus.archiver.ArchiverException;
33 import org.codehaus.plexus.archiver.jar.JarArchiver;
34 import org.codehaus.plexus.archiver.jar.ManifestException;
35
36 /**
37 * Bundles the site output into a JAR so that it can be deployed to a repository.
38 *
39 * @author <a href="mailto:mbeerman@yahoo.com">Matthew Beermann</a>
40 * @version $Id: SiteJarMojo.html 816573 2012-05-08 12:11:59Z hboutemy $
41 * @goal jar
42 * @phase package
43 * @since 2.0-beta-6
44 */
45 public class SiteJarMojo
46 extends SiteMojo
47 {
48 private static final String[] DEFAULT_ARCHIVE_EXCLUDES = new String[]{};
49
50 private static final String[] DEFAULT_ARCHIVE_INCLUDES = new String[]{"**/**"};
51
52 /**
53 * Specifies the directory where the generated jar file will be put.
54 *
55 * @parameter expression="${project.build.directory}"
56 * @required
57 */
58 private String jarOutputDirectory;
59
60 /**
61 * Specifies the filename that will be used for the generated jar file.
62 * Please note that "-site" will be appended to the file name.
63 *
64 * @parameter expression="${project.build.finalName}"
65 * @required
66 */
67 private String finalName;
68
69 /**
70 * Used for attaching the artifact in the project.
71 *
72 * @component
73 */
74 private MavenProjectHelper projectHelper;
75
76 /**
77 * Specifies whether to attach the generated artifact to the project.
78 *
79 * @parameter expression="${site.attach}" default-value="true"
80 */
81 private boolean attach;
82
83 /**
84 * The Jar archiver.
85 *
86 * @component role="org.codehaus.plexus.archiver.Archiver" roleHint="jar"
87 * @since 3.1
88 */
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 * @parameter
96 * @since 3.1
97 */
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 * @parameter
105 * @since 3.1
106 */
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 * @parameter
114 * @since 3.1
115 */
116 private String[] archiveExcludes;
117
118 /**
119 * @see org.apache.maven.plugin.Mojo#execute()
120 */
121 public void execute()
122 throws MojoExecutionException, MojoFailureException
123 {
124 if ( !outputDirectory.exists() )
125 {
126 super.execute();
127 }
128
129 try
130 {
131 File outputFile = createArchive( outputDirectory,
132 finalName + "-" + getClassifier() + "." + getArtifactType() );
133
134 if ( attach )
135 {
136 projectHelper.attachArtifact( project, getArtifactType(), getClassifier(), outputFile );
137 }
138 else
139 {
140 getLog().info( "NOT adding site jar to the list of attached artifacts." );
141 }
142 }
143 catch ( ArchiverException e )
144 {
145 throw new MojoExecutionException( "Error while creating archive.", e );
146 }
147 catch ( IOException e )
148 {
149 throw new MojoExecutionException( "Error while creating archive.", e );
150 }
151 catch ( ManifestException e )
152 {
153 throw new MojoExecutionException( "Error while creating archive.", e );
154 }
155 catch ( DependencyResolutionRequiredException e )
156 {
157 throw new MojoExecutionException( "Error while creating archive.", e );
158 }
159 }
160
161 protected String getArtifactType()
162 {
163 return "jar";
164 }
165
166 protected String getClassifier()
167 {
168 return "site";
169 }
170
171 /**
172 * Method that creates the jar file.
173 *
174 * @param siteDirectory the directory where the site files are located
175 * @param jarFilename the filename of the created jar file
176 * @return a File object that contains the created jar file
177 * @throws ArchiverException
178 * @throws IOException
179 * @throws ManifestException
180 * @throws DependencyResolutionRequiredException
181 */
182 private File createArchive( File siteDirectory, String jarFilename )
183 throws ArchiverException, IOException, ManifestException, DependencyResolutionRequiredException
184 {
185 File siteJar = new File( jarOutputDirectory, jarFilename );
186
187 MavenArchiver archiver = new MavenArchiver();
188
189 archiver.setArchiver( this.jarArchiver );
190
191 archiver.setOutputFile( siteJar );
192
193 if ( !siteDirectory.isDirectory() )
194 {
195 getLog().warn( "JAR will be empty - no content was marked for inclusion !" );
196 }
197 else
198 {
199 archiver.getArchiver().addDirectory( siteDirectory, getArchiveIncludes(), getArchiveExcludes() );
200 }
201
202 archiver.createArchive( project, archive );
203
204 return siteJar;
205 }
206
207 private String[] getArchiveIncludes()
208 {
209 if ( this.archiveIncludes != null && this.archiveIncludes.length > 0 )
210 {
211 return this.archiveIncludes;
212 }
213
214 return DEFAULT_ARCHIVE_INCLUDES;
215 }
216
217 private String[] getArchiveExcludes()
218 {
219 if ( this.archiveExcludes != null && this.archiveExcludes.length > 0 )
220 {
221 return this.archiveExcludes;
222 }
223 return DEFAULT_ARCHIVE_EXCLUDES;
224 }
225 }