1 package org.apache.rat.mp;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.FileNotFoundException;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.io.InputStream;
28
29 import org.apache.maven.plugin.MojoExecutionException;
30 import org.apache.maven.plugin.MojoFailureException;
31 import org.apache.rat.Defaults;
32 import org.apache.rat.ReportConfiguration;
33 import org.apache.rat.report.claim.ClaimStatistic;
34
35
36
37
38
39
40
41 public class RatCheckMojo extends AbstractRatMojo
42 {
43
44
45
46
47
48 private File reportFile;
49
50
51
52
53
54
55
56
57
58 private String reportStyle;
59
60
61
62
63
64 private int numUnapprovedLicenses;
65
66
67
68
69
70
71
72 private String addLicenseHeaders;
73
74
75
76
77
78
79
80
81 private String copyrightMessage;
82
83 private ClaimStatistic getRawReport()
84 throws MojoExecutionException, MojoFailureException
85 {
86 FileWriter fw = null;
87 try
88 {
89 fw = new FileWriter( reportFile );
90 final ClaimStatistic statistic = createReport( fw, getStyleSheet() );
91 fw.close();
92 fw = null;
93 return statistic;
94 }
95 catch ( IOException e )
96 {
97 throw new MojoExecutionException( e.getMessage(), e );
98 }
99 finally
100 {
101 if ( fw != null )
102 {
103 try
104 {
105 fw.close();
106 }
107 catch ( Throwable t )
108 {
109
110 }
111 }
112 }
113 }
114
115
116
117
118
119
120
121
122 private InputStream getStyleSheet() throws MojoExecutionException {
123 if ( reportStyle == null || reportStyle.equals( "plain" ) )
124 {
125 return Defaults.getPlainStyleSheet();
126 }
127 else if ( reportStyle.equals( "xml" ) )
128 {
129 return null;
130 }
131 else
132 {
133 try
134 {
135 return new FileInputStream( reportStyle );
136 }
137 catch ( FileNotFoundException e )
138 {
139 throw new MojoExecutionException(
140 "Unable to find report stylesheet: " + reportStyle, e );
141 }
142 }
143 }
144
145
146
147
148
149
150
151
152
153 public void execute() throws MojoExecutionException, MojoFailureException
154 {
155 File parent = reportFile.getParentFile();
156 parent.mkdirs();
157
158 final ClaimStatistic report = getRawReport();
159 check( report );
160 }
161
162 protected void check( ClaimStatistic statistics )
163 throws MojoFailureException
164 {
165 if ( numUnapprovedLicenses < statistics.getNumUnApproved() )
166 {
167 throw new RatCheckException( "Too many unapproved licenses: " + statistics.getNumUnApproved() );
168 }
169 }
170
171 protected ReportConfiguration getConfiguration()
172 throws MojoFailureException, MojoExecutionException {
173 final ReportConfiguration configuration = super.getConfiguration();
174 if ("forced".equals(addLicenseHeaders)) {
175 configuration.setAddingLicenses(true);
176 configuration.setAddingLicensesForced(true);
177 configuration.setCopyrightMessage(copyrightMessage);
178 } else if ("true".equals(addLicenseHeaders)) {
179 configuration.setAddingLicenses(true);
180 configuration.setCopyrightMessage(copyrightMessage);
181 } else if ("false".equals(addLicenseHeaders)) {
182
183 } else {
184 throw new MojoFailureException("Invalid value for addLicenseHeaders: Expected forced|true|false, got "
185 + addLicenseHeaders);
186 }
187 return configuration;
188 }
189 }