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 @Parameter(property = "qulice.excludes")
89 private final Collection<String> excludes = new LinkedList<>();
90
91
92
93
94
95 @Parameter(
96 property = "qulice.asserts",
97 required = false
98 )
99 private final Collection<String> asserts = new LinkedList<>();
100
101
102
103
104
105
106 @Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}")
107 private String charset;
108
109
110
111
112
113 public final void setProject(final MavenProject proj) {
114 this.project = proj;
115 }
116
117
118
119
120
121 public final void setSkip(final boolean skp) {
122 this.skip = skp;
123 }
124
125
126
127
128
129 public final void setAsserts(final Collection<String> asser) {
130 this.asserts.clear();
131 this.asserts.addAll(asser);
132 }
133
134
135
136
137
138 public final void setExcludes(final Collection<String> exprs) {
139 this.excludes.clear();
140 this.excludes.addAll(exprs);
141 }
142
143
144
145
146
147 public void setEncoding(final String encoding) {
148 this.charset = encoding;
149 }
150
151 @Override
152 public final void contextualize(final Context ctx) {
153 this.environment.setContext(ctx);
154 }
155
156 @Override
157 public final void execute() throws MojoFailureException {
158 StaticLoggerBinder.getSingleton().setMavenLog(this.getLog());
159 if (this.skip) {
160 this.getLog().info("Execution skipped");
161 return;
162 }
163 this.environment.setProject(this.project);
164 this.environment.setMojoExecutor(
165 new MojoExecutor(this.manager, this.sess)
166 );
167 this.environment.setExcludes(this.excludes);
168 this.environment.setAsser(this.asserts);
169 this.environment.setEncoding(this.charset);
170 final long start = System.nanoTime();
171 this.doExecute();
172 Logger.info(
173 this,
174 "Qulice quality check completed in %[nano]s",
175 System.nanoTime() - start
176 );
177 }
178
179
180
181
182
183 public final MavenSession session() {
184 return this.sess;
185 }
186
187
188
189
190
191
192 protected abstract void doExecute() throws MojoFailureException;
193
194
195
196
197
198 protected final MavenEnvironment env() {
199 return this.environment;
200 }
201
202 }