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