View Javadoc

1   /*
2    * #%L
3    * License Maven Plugin
4    * 
5    * $Id: LicenseListMojo.java 13616 2011-02-14 11:38:39Z tchemit $
6    * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/LicenseListMojo.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.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugin.MojoFailureException;
30  import org.codehaus.mojo.license.model.License;
31  import org.codehaus.mojo.license.model.LicenseStore;
32  
33  import java.io.IOException;
34  import java.util.Arrays;
35  import java.util.Collections;
36  import java.util.List;
37  
38  /**
39   * Display all available licenses.
40   *
41   * @author tchemit <chemit@codelutin.com>
42   * @goal license-list
43   * @requiresProject false
44   * @requiresDirectInvocation
45   * @since 1.0
46   */
47  public class LicenseListMojo
48      extends AbstractLicenseMojo
49  {
50  
51      /**
52       * the url of an extra license repository.
53       *
54       * @parameter expression="${extraResolver}"
55       * @since 1.0
56       */
57      private String extraResolver;
58  
59      /**
60       * A flag to display also the content of each license.
61       *
62       * @parameter expression="${detail}"
63       * @since 1.0
64       */
65      private boolean detail;
66  
67      /**
68       * store of licenses
69       */
70      protected LicenseStore licenseStore;
71  
72      @Override
73      protected void init()
74          throws Exception
75      {
76  
77          // obtain licenses store
78          licenseStore = LicenseStore.createLicenseStore( getLog(), getExtraResolver() );
79      }
80  
81      @Override
82      public void doAction()
83          throws MojoExecutionException, MojoFailureException
84      {
85          StringBuilder buffer = new StringBuilder();
86  
87          if ( isVerbose() )
88          {
89              buffer.append( "\n\n-------------------------------------------------------------------------------\n" );
90              buffer.append( "                           maven-license-plugin\n" );
91              buffer.append( "-------------------------------------------------------------------------------\n\n" );
92          }
93          buffer.append( "Available licenses :\n\n" );
94  
95          List<String> names = Arrays.asList( licenseStore.getLicenseNames() );
96  
97          int maxLength = 0;
98          for ( String name : names )
99          {
100             if ( name.length() > maxLength )
101             {
102                 maxLength = name.length();
103             }
104         }
105         Collections.sort( names );
106 
107         String pattern = " * %1$-" + maxLength + "s : %2$s\n";
108         for ( String licenseName : names )
109         {
110             License license = licenseStore.getLicense( licenseName );
111             buffer.append( String.format( pattern, licenseName, license.getDescription() ) );
112             if ( isDetail() )
113             {
114                 try
115                 {
116                     buffer.append( "\n" );
117                     buffer.append( license.getHeaderContent( getEncoding() ) );
118                     buffer.append( "\n\n" );
119                 }
120                 catch ( IOException ex )
121                 {
122                     throw new MojoExecutionException(
123                         "could not instanciate license with name " + licenseName + " for reason " + ex.getMessage(),
124                         ex );
125                 }
126             }
127         }
128         getLog().info( buffer.toString() );
129     }
130 
131     public String getExtraResolver()
132     {
133         return extraResolver;
134     }
135 
136     public boolean isDetail()
137     {
138         return detail;
139     }
140 
141     public void setExtraResolver( String extraResolver )
142     {
143         this.extraResolver = extraResolver;
144     }
145 
146     public void setDetail( boolean detail )
147     {
148         this.detail = detail;
149     }
150 }