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 java.io.File;
29 import java.util.Arrays;
30
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.maven.execution.MavenSession;
33 import org.apache.maven.plugin.AbstractMojo;
34 import org.apache.maven.plugin.MojoExecutionException;
35 import org.apache.maven.plugin.MojoFailureException;
36 import org.apache.maven.project.MavenProject;
37 import org.codehaus.plexus.util.ReaderFactory;
38
39
40
41
42
43
44
45 public abstract class AbstractLicenseMojo extends AbstractMojo {
46
47
48
49
50
51
52
53
54
55 private MavenSession session;
56
57
58
59
60
61
62
63
64 private MavenProject project;
65
66
67
68
69
70
71
72
73
74 private boolean verbose;
75
76
77
78
79
80
81
82
83
84 private String encoding;
85
86 public final String getEncoding() {
87 return encoding;
88 }
89
90 public final void setEncoding(String encoding) {
91 this.encoding = encoding;
92 }
93
94
95
96
97
98
99
100
101
102 protected abstract void init() throws Exception;
103
104
105
106
107
108
109
110
111
112
113
114
115
116 protected abstract void doAction() throws Exception;
117
118 @Override
119 public final void execute() throws MojoExecutionException, MojoFailureException {
120 try {
121 if (getLog().isDebugEnabled()) {
122
123
124 setVerbose(true);
125 }
126
127
128
129 boolean canContinue = checkPackaging();
130 if (!canContinue) {
131 getLog().debug("Skip for packaging '" + getProject().getPackaging() + "'");
132 return;
133 }
134
135
136
137 try {
138
139 checkEncoding();
140
141 init();
142
143 } catch (MojoFailureException e) {
144 throw e;
145 } catch (MojoExecutionException e) {
146 throw e;
147 } catch (Exception e) {
148 throw new MojoExecutionException("could not init goal " + getClass().getSimpleName() + " for reason : "
149 + e.getMessage(), e);
150 }
151
152
153
154 canContinue = checkSkip();
155 if (!canContinue) {
156 if (isVerbose()) {
157 getLog().info("Goal will not be executed.");
158 }
159 return;
160 }
161
162
163
164 try {
165
166 doAction();
167
168 } catch (MojoFailureException e) {
169 throw e;
170 } catch (MojoExecutionException e) {
171 throw e;
172 } catch (Exception e) {
173 throw new MojoExecutionException("could not execute goal " + getClass().getSimpleName()
174 + " for reason : " + e.getMessage(), e);
175 }
176 } finally {
177 afterExecute();
178 }
179 }
180
181
182
183
184 protected void afterExecute() {
185
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205 protected boolean checkPackaging() {
206
207 return true;
208 }
209
210
211
212
213
214
215 protected boolean checkSkip() {
216
217 return true;
218 }
219
220
221
222
223
224
225
226
227 protected boolean acceptPackaging(String... packages) {
228 String projectPackaging = getProject().getPackaging();
229
230 for (String p : packages) {
231 if (p.equals(projectPackaging)) {
232
233 return true;
234 }
235 }
236
237 return false;
238 }
239
240
241
242
243
244
245
246
247 protected boolean rejectPackaging(String... packages) {
248 String projectPackaging = getProject().getPackaging();
249
250 for (String p : packages) {
251 if (p.equals(projectPackaging)) {
252
253 return false;
254 }
255 }
256
257 return true;
258 }
259
260
261
262
263
264
265 protected void checkEncoding() {
266
267 if (isVerbose()) {
268 getLog().info("Will check encoding : " + getEncoding());
269 }
270 if (StringUtils.isEmpty(getEncoding())) {
271 getLog().warn(
272 "File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING
273 + ", i.e. build is platform dependent!");
274 setEncoding(ReaderFactory.FILE_ENCODING);
275 }
276 }
277
278 public final MavenProject getProject() {
279 return project;
280 }
281
282 public final void setProject(MavenProject project) {
283 this.project = project;
284 }
285
286 public final boolean isVerbose() {
287 return verbose;
288 }
289
290 public final void setVerbose(boolean verbose) {
291 this.verbose = verbose;
292 }
293
294 public final MavenSession getSession() {
295 return session;
296 }
297
298 public final void setSession(MavenSession session) {
299 this.session = session;
300 }
301
302 public final long getBuildTimestamp() {
303 return session.getStartTime().getTime();
304 }
305
306
307
308
309
310
311
312
313
314 protected void addResourceDir(File dir, String... includes) {
315 boolean added = MojoHelper.addResourceDir(dir, getProject(), includes);
316 if (added && isVerbose()) {
317 getLog().info("add resource " + dir + " with includes " + Arrays.toString(includes));
318 }
319 }
320
321
322
323
324 protected boolean hasClassPath() {
325 return rejectPackaging("pom");
326 }
327
328 }