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.apache.maven.plugin.MojoExecutionException;
19  import org.apache.maven.plugin.MojoFailureException;
20  import org.codehaus.mojo.license.model.License;
21  import org.codehaus.mojo.license.model.LicenseStore;
22  
23  import java.io.IOException;
24  import java.util.Arrays;
25  import java.util.Collections;
26  import java.util.List;
27  
28  /**
29   * Display all available licenses.
30   *
31   * @author tchemit <chemit@codelutin.com>
32   * @goal license-list
33   * @requiresProject false
34   * @requiresDirectInvocation
35   * @since 1.0
36   */
37  public class LicenseListMojo
38      extends AbstractLicenseMojo
39  {
40  
41      /**
42       * the url of an extra license repository.
43       *
44       * @parameter expression="${extraResolver}"
45       * @since 1.0
46       */
47      private String extraResolver;
48  
49      /**
50       * A flag to display also the content of each license.
51       *
52       * @parameter expression="${detail}"
53       * @since 1.0
54       */
55      private boolean detail;
56  
57      /**
58       * store of licenses
59       */
60      protected LicenseStore licenseStore;
61  
62      @Override
63      protected void init()
64          throws Exception
65      {
66  
67          // obtain licenses store
68          licenseStore = LicenseStore.createLicenseStore( getLog(), getExtraResolver() );
69      }
70  
71      @Override
72      public void doAction()
73          throws MojoExecutionException, MojoFailureException
74      {
75          StringBuilder buffer = new StringBuilder();
76  
77          if ( isVerbose() )
78          {
79              buffer.append( "\n\n-------------------------------------------------------------------------------\n" );
80              buffer.append( "                           maven-license-plugin\n" );
81              buffer.append( "-------------------------------------------------------------------------------\n\n" );
82          }
83          buffer.append( "Available licenses :\n\n" );
84  
85          List<String> names = Arrays.asList( licenseStore.getLicenseNames() );
86  
87          int maxLength = 0;
88          for ( String name : names )
89          {
90              if ( name.length() > maxLength )
91              {
92                  maxLength = name.length();
93              }
94          }
95          Collections.sort( names );
96  
97          String pattern = " * %1$-" + maxLength + "s : %2$s\n";
98          for ( String licenseName : names )
99          {
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 }