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
27
28
29
30
31 package com.qulice.maven;
32
33 import com.jcabi.log.Logger;
34 import java.util.Collection;
35 import java.util.LinkedList;
36 import org.apache.maven.execution.MavenSession;
37 import org.apache.maven.plugin.AbstractMojo;
38 import org.apache.maven.plugin.MavenPluginManager;
39 import org.apache.maven.plugin.MojoFailureException;
40 import org.apache.maven.plugins.annotations.Component;
41 import org.apache.maven.plugins.annotations.Parameter;
42 import org.apache.maven.project.MavenProject;
43 import org.codehaus.plexus.context.Context;
44 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
45 import org.slf4j.impl.StaticLoggerBinder;
46
47
48
49
50
51
52 public abstract class AbstractQuliceMojo extends AbstractMojo
53 implements Contextualizable {
54
55
56
57
58 private final DefaultMavenEnvironment environment =
59 new DefaultMavenEnvironment();
60
61
62
63
64 @Parameter(defaultValue = "${project}", readonly = true)
65 private MavenProject project;
66
67
68
69
70 @Parameter(defaultValue = "${session}", readonly = true)
71 private MavenSession sess;
72
73
74
75
76 @Component
77 private MavenPluginManager manager;
78
79
80
81
82 @Parameter(property = "qulice.skip", defaultValue = "false")
83 private boolean skip;
84
85
86
87
88
89
90 @Parameter(property = "qulice.license", defaultValue = "LICENSE.txt")
91 private String license = "LICENSE.txt";
92
93
94
95
96 @Parameter(property = "qulice.excludes")
97 private final Collection<String> excludes = new LinkedList<>();
98
99
100
101
102
103 @Parameter(
104 property = "qulice.asserts",
105 required = false
106 )
107 private final Collection<String> asserts = new LinkedList<>();
108
109
110
111
112
113
114 @Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}")
115 private String charset;
116
117
118
119
120
121 public final void setProject(final MavenProject proj) {
122 this.project = proj;
123 }
124
125
126
127
128
129 public final void setSkip(final boolean skp) {
130 this.skip = skp;
131 }
132
133
134
135
136
137 public final void setAsserts(final Collection<String> asser) {
138 this.asserts.clear();
139 this.asserts.addAll(asser);
140 }
141
142
143
144
145
146 public final void setLicense(final String lcs) {
147 this.license = lcs;
148 }
149
150
151
152
153
154 public final void setExcludes(final Collection<String> exprs) {
155 this.excludes.clear();
156 this.excludes.addAll(exprs);
157 }
158
159
160
161
162
163 public void setEncoding(final String encoding) {
164 this.charset = encoding;
165 }
166
167 @Override
168 public final void contextualize(final Context ctx) {
169 this.environment.setContext(ctx);
170 }
171
172 @Override
173 public final void execute() throws MojoFailureException {
174 StaticLoggerBinder.getSingleton().setMavenLog(this.getLog());
175 if (this.skip) {
176 this.getLog().info("Execution skipped");
177 return;
178 }
179 this.environment.setProperty("license", this.license);
180 this.environment.setProject(this.project);
181 this.environment.setMojoExecutor(
182 new MojoExecutor(this.manager, this.sess)
183 );
184 this.environment.setExcludes(this.excludes);
185 this.environment.setAsser(this.asserts);
186 this.environment.setEncoding(this.charset);
187 final long start = System.nanoTime();
188 this.doExecute();
189 Logger.info(
190 this,
191 "Qulice quality check completed in %[nano]s",
192 System.nanoTime() - start
193 );
194 }
195
196
197
198
199
200 public final MavenSession session() {
201 return this.sess;
202 }
203
204
205
206
207
208
209 protected abstract void doExecute() throws MojoFailureException;
210
211
212
213
214
215 protected final MavenEnvironment env() {
216 return this.environment;
217 }
218
219 }