1 /*
2 * #%L
3 * License Maven Plugin
4 *
5 * $Id: UpdateProjectLicenseMojo.java 14625 2011-08-26 12:49:21Z tchemit $
6 * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/UpdateProjectLicenseMojo.java $
7 * %%
8 * Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
9 * %%
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation, either version 3 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Lesser Public License for more details.
19 *
20 * You should have received a copy of the GNU General Lesser Public
21 * License along with this program. If not, see
22 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
23 * #L%
24 */
25
26 package org.codehaus.mojo.license;
27
28 import org.codehaus.mojo.license.model.License;
29
30 import java.io.File;
31
32 /**
33 * Updates (or creates) the main project license file according to the given
34 * license defines as {@link #licenseName}.
35 * <p/>
36 * Can also generate a bundled license file (to avoid collision names in
37 * class-path). This file is by default generated in
38 * {@code META-INF class-path directory}.
39 *
40 * @author tchemit <chemit@codelutin.com>
41 * @goal update-project-license
42 * @phase generate-resources
43 * @requiresProject true
44 * @since 1.0
45 */
46 public class UpdateProjectLicenseMojo
47 extends AbstractLicenseNameMojo
48 {
49
50 /**
51 * Project license file to synchronize with main license defined in
52 * descriptor file.
53 *
54 * @parameter expression="${license.licenceFile}" default-value="${basedir}/LICENSE.txt"
55 * @required
56 * @since 1.0
57 */
58 protected File licenseFile;
59
60 /**
61 * The directory where to generate license resources.
62 * <p/>
63 * <b>Note:</b> This option is not available for {@code pom} module types.
64 *
65 * @parameter expression="${license.outputDirectory}" default-value="target/generated-sources/license"
66 * @since 1.0
67 */
68 protected File outputDirectory;
69
70 /**
71 * A flag to copy the main license file in a bundled place.
72 * <p/>
73 * This is usefull for final application to have a none confusing location
74 * to seek for the application license.
75 * <p/>
76 * If Sets to {@code true}, will copy the license file to the
77 * {@link #bundleLicensePath} to {@link #outputDirectory}.
78 * <p/>
79 * <b>Note:</b> This option is not available for {@code pom} module types.
80 *
81 * @parameter expression="${license.generateBundle}" default-value="false"
82 * @since 1.0
83 */
84 protected boolean generateBundle;
85
86 /**
87 * The path of the bundled license file to produce when
88 * {@link #generateBundle} is on.
89 * <p/>
90 * <b>Note:</b> This option is not available for {@code pom} module types.
91 *
92 * @parameter expression="${license.bundleLicensePath}" default-value="META-INF/${project.artifactId}-LICENSE.txt"
93 * @since 1.0
94 */
95 protected String bundleLicensePath;
96
97 /**
98 * A flag to force to generate project license file even if it is up-to-date.
99 *
100 * @parameter expression="${license.force}" default-value="false"
101 * @since 1.0.0
102 */
103 protected boolean force;
104
105 /**
106 * A flag to skip the goal.
107 *
108 * @parameter expression="${license.skipUpdateProjectLicense}" default-value="false"
109 * @since 1.0
110 */
111 protected boolean skipUpdateProjectLicense;
112
113 /**
114 * Flag to known if generate is needed.
115 */
116 private boolean doGenerate;
117
118 @Override
119 protected void init()
120 throws Exception
121 {
122
123 if ( isSkip() )
124 {
125 return;
126 }
127
128 super.init();
129
130 // must generate if file does not exist or pom never thant license file
131 File licenseFile = getLicenseFile();
132 if ( licenseFile != null )
133 {
134 File pomFile = getProject().getFile();
135
136 setDoGenerate( isForce() || !licenseFile.exists() || licenseFile.lastModified() <= pomFile.lastModified() );
137 }
138 }
139
140 @Override
141 protected void doAction()
142 throws Exception
143 {
144
145 License license = getLicense();
146
147 File target = getLicenseFile();
148
149 if ( isDoGenerate() )
150 {
151
152 getLog().info( "Will create or update license file [" + license.getName() + "] to " + target );
153 if ( isVerbose() )
154 {
155 getLog().info( "detail of license :\n" + license );
156 }
157
158 if ( target.exists() && isKeepBackup() )
159 {
160 if ( isVerbose() )
161 {
162 getLog().info( "backup " + target );
163 }
164 // copy it to backup file
165 FileUtil.backupFile( target );
166 }
167 }
168
169 // obtain license content
170 String licenseContent = license.getLicenseContent( getEncoding() );
171
172 if ( isDoGenerate() )
173 {
174
175 // writes it root main license file
176 FileUtil.writeString( target, licenseContent, getEncoding() );
177 }
178
179 if ( hasClassPath() )
180 {
181
182 // copy the license file to the resources directory
183 File resourceTarget = new File( getOutputDirectory(), target.getName() );
184 FileUtil.copyFile( getLicenseFile(), resourceTarget );
185
186 addResourceDir( getOutputDirectory(), "**/" + resourceTarget.getName() );
187
188 if ( isGenerateBundle() )
189 {
190
191 // creates the bundled license file
192 File bundleTarget = FileUtil.getFile( getOutputDirectory(), getBundleLicensePath() );
193 FileUtil.copyFile( target, bundleTarget );
194
195 if ( !resourceTarget.getName().equals( bundleTarget.getName() ) )
196 {
197
198 addResourceDir( getOutputDirectory(), "**/" + bundleTarget.getName() );
199 }
200 }
201
202
203 }
204 }
205
206 public File getLicenseFile()
207 {
208 return licenseFile;
209 }
210
211 public boolean isGenerateBundle()
212 {
213 return generateBundle;
214 }
215
216 public File getOutputDirectory()
217 {
218 return outputDirectory;
219 }
220
221 public String getBundleLicensePath()
222 {
223 return bundleLicensePath;
224 }
225
226 public boolean isDoGenerate()
227 {
228 return doGenerate;
229 }
230
231 public boolean isForce()
232 {
233 return force;
234 }
235
236 @Override
237 public boolean isSkip()
238 {
239 return skipUpdateProjectLicense;
240 }
241
242 public void setLicenseFile( File licenseFile )
243 {
244 this.licenseFile = licenseFile;
245 }
246
247 public void setGenerateBundle( boolean generateBundle )
248 {
249 this.generateBundle = generateBundle;
250 }
251
252 public void setOutputDirectory( File outputDirectory )
253 {
254 this.outputDirectory = outputDirectory;
255 }
256
257 public void setBundleLicensePath( String bundleLicensePath )
258 {
259 this.bundleLicensePath = bundleLicensePath;
260 }
261
262 public void setDoGenerate( boolean doGenerate )
263 {
264 this.doGenerate = doGenerate;
265 }
266
267 @Override
268 public void setSkip( boolean skipUpdateProjectLicense )
269 {
270 this.skipUpdateProjectLicense = skipUpdateProjectLicense;
271 }
272
273 public void setForce( boolean force )
274 {
275 this.force = force;
276 }
277 }