001 /*
002 * #%L
003 * License Maven Plugin
004 *
005 * $Id: UpdateProjectLicenseMojo.java 14625 2011-08-26 12:49:21Z tchemit $
006 * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/UpdateProjectLicenseMojo.java $
007 * %%
008 * Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
009 * %%
010 * This program is free software: you can redistribute it and/or modify
011 * it under the terms of the GNU Lesser General Public License as
012 * published by the Free Software Foundation, either version 3 of the
013 * License, or (at your option) any later version.
014 *
015 * This program is distributed in the hope that it will be useful,
016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018 * GNU General Lesser Public License for more details.
019 *
020 * You should have received a copy of the GNU General Lesser Public
021 * License along with this program. If not, see
022 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
023 * #L%
024 */
025
026 package org.codehaus.mojo.license;
027
028 import org.codehaus.mojo.license.model.License;
029
030 import java.io.File;
031
032 /**
033 * Updates (or creates) the main project license file according to the given
034 * license defines as {@link #licenseName}.
035 * <p/>
036 * Can also generate a bundled license file (to avoid collision names in
037 * class-path). This file is by default generated in
038 * {@code META-INF class-path directory}.
039 *
040 * @author tchemit <chemit@codelutin.com>
041 * @goal update-project-license
042 * @phase generate-resources
043 * @requiresProject true
044 * @since 1.0
045 */
046 public class UpdateProjectLicenseMojo
047 extends AbstractLicenseNameMojo
048 {
049
050 /**
051 * Project license file to synchronize with main license defined in
052 * descriptor file.
053 *
054 * @parameter expression="${license.licenceFile}" default-value="${basedir}/LICENSE.txt"
055 * @required
056 * @since 1.0
057 */
058 protected File licenseFile;
059
060 /**
061 * The directory where to generate license resources.
062 * <p/>
063 * <b>Note:</b> This option is not available for {@code pom} module types.
064 *
065 * @parameter expression="${license.outputDirectory}" default-value="target/generated-sources/license"
066 * @since 1.0
067 */
068 protected File outputDirectory;
069
070 /**
071 * A flag to copy the main license file in a bundled place.
072 * <p/>
073 * This is usefull for final application to have a none confusing location
074 * to seek for the application license.
075 * <p/>
076 * If Sets to {@code true}, will copy the license file to the
077 * {@link #bundleLicensePath} to {@link #outputDirectory}.
078 * <p/>
079 * <b>Note:</b> This option is not available for {@code pom} module types.
080 *
081 * @parameter expression="${license.generateBundle}" default-value="false"
082 * @since 1.0
083 */
084 protected boolean generateBundle;
085
086 /**
087 * The path of the bundled license file to produce when
088 * {@link #generateBundle} is on.
089 * <p/>
090 * <b>Note:</b> This option is not available for {@code pom} module types.
091 *
092 * @parameter expression="${license.bundleLicensePath}" default-value="META-INF/${project.artifactId}-LICENSE.txt"
093 * @since 1.0
094 */
095 protected String bundleLicensePath;
096
097 /**
098 * A flag to force to generate project license file even if it is up-to-date.
099 *
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 }