1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31
32
33
34
35
36
37 public class LicenseListMojo
38 extends AbstractLicenseMojo
39 {
40
41
42
43
44
45
46
47 private String extraResolver;
48
49
50
51
52
53
54
55 private boolean detail;
56
57
58
59
60 protected LicenseStore licenseStore;
61
62 @Override
63 protected void init()
64 throws Exception
65 {
66
67
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 }