001    /**
002     * Copyright 2010-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.codehaus.mojo.license;
017    
018    import org.codehaus.mojo.license.model.License;
019    
020    import java.io.File;
021    
022    /**
023     * Updates (or creates) the main project license file according to the given
024     * license defines as {@link #licenseName}.
025     * <p/>
026     * Can also generate a bundled license file (to avoid collision names in
027     * class-path). This file is by default generated in
028     * {@code META-INF class-path directory}.
029     *
030     * @author tchemit <chemit@codelutin.com>
031     * @goal update-project-license
032     * @phase generate-resources
033     * @requiresProject true
034     * @since 1.0
035     */
036    public class UpdateProjectLicenseMojo
037        extends AbstractLicenseNameMojo
038    {
039    
040        /**
041         * Project license file to synchronize with main license defined in
042         * descriptor file.
043         *
044         * @parameter expression="${license.licenceFile}" default-value="${basedir}/LICENSE.txt"
045         * @required
046         * @since 1.0
047         */
048        protected File licenseFile;
049    
050        /**
051         * The directory where to generate license resources.
052         * <p/>
053         * <b>Note:</b> This option is not available for {@code pom} module types.
054         *
055         * @parameter expression="${license.outputDirectory}"  default-value="target/generated-sources/license"
056         * @since 1.0
057         */
058        protected File outputDirectory;
059    
060        /**
061         * A flag to copy the main license file in a bundled place.
062         * <p/>
063         * This is usefull for final application to have a none confusing location
064         * to seek for the application license.
065         * <p/>
066         * If Sets to {@code true}, will copy the license file to the
067         * {@link #bundleLicensePath} to {@link #outputDirectory}.
068         * <p/>
069         * <b>Note:</b> This option is not available for {@code pom} module types.
070         *
071         * @parameter expression="${license.generateBundle}"  default-value="false"
072         * @since 1.0
073         */
074        protected boolean generateBundle;
075    
076        /**
077         * The path of the bundled license file to produce when
078         * {@link #generateBundle} is on.
079         * <p/>
080         * <b>Note:</b> This option is not available for {@code pom} module types.
081         *
082         * @parameter expression="${license.bundleLicensePath}"  default-value="META-INF/${project.artifactId}-LICENSE.txt"
083         * @since 1.0
084         */
085        protected String bundleLicensePath;
086    
087        /**
088         * A flag to force to generate project license file even if it is up-to-date.
089         *
090         * @parameter expression="${license.force}"  default-value="false"
091         * @since 1.0.0
092         */
093        protected boolean force;
094    
095        /**
096         * A flag to skip the goal.
097         *
098         * @parameter expression="${license.skipUpdateProjectLicense}" default-value="false"
099         * @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    }