View Javadoc

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