001    /**
002     * Copyright 2010-2013 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.apache.maven.plugin.MojoExecutionException;
019    import org.apache.maven.plugin.MojoFailureException;
020    import org.codehaus.mojo.license.model.License;
021    import org.codehaus.mojo.license.model.LicenseStore;
022    
023    import java.io.IOException;
024    import java.util.Arrays;
025    import java.util.Collections;
026    import java.util.List;
027    
028    /**
029     * Display all available licenses.
030     *
031     * @author tchemit <chemit@codelutin.com>
032     * @goal license-list
033     * @requiresProject false
034     * @requiresDirectInvocation
035     * @since 1.0
036     */
037    public class LicenseListMojo
038        extends AbstractLicenseMojo
039    {
040    
041        /**
042         * the url of an extra license repository.
043         *
044         * @parameter expression="${extraResolver}"
045         * @since 1.0
046         */
047        private String extraResolver;
048    
049        /**
050         * A flag to display also the content of each license.
051         *
052         * @parameter expression="${detail}"
053         * @since 1.0
054         */
055        private boolean detail;
056    
057        /**
058         * store of licenses
059         */
060        protected LicenseStore licenseStore;
061    
062        @Override
063        protected void init()
064            throws Exception
065        {
066    
067            // obtain licenses store
068            licenseStore = LicenseStore.createLicenseStore( getLog(), getExtraResolver() );
069        }
070    
071        @Override
072        public void doAction()
073            throws MojoExecutionException, MojoFailureException
074        {
075            StringBuilder buffer = new StringBuilder();
076    
077            if ( isVerbose() )
078            {
079                buffer.append( "\n\n-------------------------------------------------------------------------------\n" );
080                buffer.append( "                           maven-license-plugin\n" );
081                buffer.append( "-------------------------------------------------------------------------------\n\n" );
082            }
083            buffer.append( "Available licenses :\n\n" );
084    
085            List<String> names = Arrays.asList( licenseStore.getLicenseNames() );
086    
087            int maxLength = 0;
088            for ( String name : names )
089            {
090                if ( name.length() > maxLength )
091                {
092                    maxLength = name.length();
093                }
094            }
095            Collections.sort( names );
096    
097            String pattern = " * %1$-" + maxLength + "s : %2$s\n";
098            for ( String licenseName : names )
099            {
100                License license = licenseStore.getLicense( licenseName );
101                buffer.append( String.format( pattern, licenseName, license.getDescription() ) );
102                if ( isDetail() )
103                {
104                    try
105                    {
106                        buffer.append( "\n" );
107                        buffer.append( license.getHeaderContent( getEncoding() ) );
108                        buffer.append( "\n\n" );
109                    }
110                    catch ( IOException ex )
111                    {
112                        throw new MojoExecutionException(
113                            "could not instanciate license with name " + licenseName + " for reason " + ex.getMessage(),
114                            ex );
115                    }
116                }
117            }
118            getLog().info( buffer.toString() );
119        }
120    
121        public String getExtraResolver()
122        {
123            return extraResolver;
124        }
125    
126        public boolean isDetail()
127        {
128            return detail;
129        }
130    
131        public void setExtraResolver( String extraResolver )
132        {
133            this.extraResolver = extraResolver;
134        }
135    
136        public void setDetail( boolean detail )
137        {
138            this.detail = detail;
139        }
140    }