1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
40
41
42
43
44
45
46
47 public class LicenseListMojo
48 extends AbstractLicenseMojo
49 {
50
51
52
53
54
55
56
57 private String extraResolver;
58
59
60
61
62
63
64
65 private boolean detail;
66
67
68
69
70 protected LicenseStore licenseStore;
71
72 @Override
73 protected void init()
74 throws Exception
75 {
76
77
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 }