001 /*
002 * #%L
003 * License Maven Plugin
004 *
005 * $Id: LicenseListMojo.java 13616 2011-02-14 11:38:39Z tchemit $
006 * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/LicenseListMojo.java $
007 * %%
008 * Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
009 * %%
010 * This program is free software: you can redistribute it and/or modify
011 * it under the terms of the GNU Lesser General Public License as
012 * published by the Free Software Foundation, either version 3 of the
013 * License, or (at your option) any later version.
014 *
015 * This program is distributed in the hope that it will be useful,
016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018 * GNU General Lesser Public License for more details.
019 *
020 * You should have received a copy of the GNU General Lesser Public
021 * License along with this program. If not, see
022 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
023 * #L%
024 */
025
026 package org.codehaus.mojo.license;
027
028 import org.apache.maven.plugin.MojoExecutionException;
029 import org.apache.maven.plugin.MojoFailureException;
030 import org.codehaus.mojo.license.model.License;
031 import org.codehaus.mojo.license.model.LicenseStore;
032
033 import java.io.IOException;
034 import java.util.Arrays;
035 import java.util.Collections;
036 import java.util.List;
037
038 /**
039 * Display all available licenses.
040 *
041 * @author tchemit <chemit@codelutin.com>
042 * @goal license-list
043 * @requiresProject false
044 * @requiresDirectInvocation
045 * @since 1.0
046 */
047 public class LicenseListMojo
048 extends AbstractLicenseMojo
049 {
050
051 /**
052 * the url of an extra license repository.
053 *
054 * @parameter expression="${extraResolver}"
055 * @since 1.0
056 */
057 private String extraResolver;
058
059 /**
060 * A flag to display also the content of each license.
061 *
062 * @parameter expression="${detail}"
063 * @since 1.0
064 */
065 private boolean detail;
066
067 /**
068 * store of licenses
069 */
070 protected LicenseStore licenseStore;
071
072 @Override
073 protected void init()
074 throws Exception
075 {
076
077 // obtain licenses store
078 licenseStore = LicenseStore.createLicenseStore( getLog(), getExtraResolver() );
079 }
080
081 @Override
082 public void doAction()
083 throws MojoExecutionException, MojoFailureException
084 {
085 StringBuilder buffer = new StringBuilder();
086
087 if ( isVerbose() )
088 {
089 buffer.append( "\n\n-------------------------------------------------------------------------------\n" );
090 buffer.append( " maven-license-plugin\n" );
091 buffer.append( "-------------------------------------------------------------------------------\n\n" );
092 }
093 buffer.append( "Available licenses :\n\n" );
094
095 List<String> names = Arrays.asList( licenseStore.getLicenseNames() );
096
097 int maxLength = 0;
098 for ( String name : names )
099 {
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 }