1
2
3
4
5 package com.qulice.maven;
6
7 import com.jcabi.log.Logger;
8 import java.util.Collection;
9 import java.util.LinkedList;
10 import javax.inject.Inject;
11 import org.apache.maven.execution.MavenSession;
12 import org.apache.maven.plugin.AbstractMojo;
13 import org.apache.maven.plugin.MavenPluginManager;
14 import org.apache.maven.plugin.MojoFailureException;
15 import org.apache.maven.plugins.annotations.Parameter;
16 import org.apache.maven.project.MavenProject;
17 import org.codehaus.plexus.context.Context;
18 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
19 import org.slf4j.impl.StaticLoggerBinder;
20
21
22
23
24
25
26 public abstract class AbstractQuliceMojo extends AbstractMojo
27 implements Contextualizable {
28
29
30
31
32 private final DefaultMavenEnvironment environment =
33 new DefaultMavenEnvironment();
34
35
36
37
38 @Parameter(defaultValue = "${project}", readonly = true)
39 private MavenProject project;
40
41
42
43
44 @Parameter(defaultValue = "${session}", readonly = true)
45 private MavenSession sess;
46
47
48
49
50 @Inject
51 private MavenPluginManager manager;
52
53
54
55
56 @Parameter(property = "qulice.skip", defaultValue = "false")
57 private boolean skip;
58
59
60
61
62 @Parameter(property = "qulice.excludes")
63 private final Collection<String> excludes = new LinkedList<>();
64
65
66
67
68
69 @Parameter(
70 property = "qulice.asserts",
71 required = false
72 )
73 private final Collection<String> asserts = new LinkedList<>();
74
75
76
77
78
79
80 @Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}")
81 private String charset;
82
83
84
85
86
87 public final void setProject(final MavenProject proj) {
88 this.project = proj;
89 }
90
91
92
93
94
95 public final void setSkip(final boolean skp) {
96 this.skip = skp;
97 }
98
99
100
101
102
103 public final void setAsserts(final Collection<String> val) {
104 this.asserts.clear();
105 this.asserts.addAll(val);
106 }
107
108
109
110
111
112 public final void setExcludes(final Collection<String> exprs) {
113 this.excludes.clear();
114 this.excludes.addAll(exprs);
115 }
116
117
118
119
120
121 public void setEncoding(final String encoding) {
122 this.charset = encoding;
123 }
124
125 @Override
126 public final void contextualize(final Context ctx) {
127 this.environment.setContext(ctx);
128 }
129
130 @Override
131 public final void execute() throws MojoFailureException {
132 StaticLoggerBinder.getSingleton().setMavenLog(this.getLog());
133 if (this.skip) {
134 this.getLog().info("Execution skipped");
135 return;
136 }
137 this.environment.setProject(this.project);
138 this.environment.setMojoExecutor(
139 new MojoExecutor(this.manager, this.sess)
140 );
141 this.environment.setExcludes(this.excludes);
142 this.environment.setAssertion(this.asserts);
143 this.environment.setEncoding(this.charset);
144 final long start = System.nanoTime();
145 this.doExecute();
146 Logger.info(
147 this,
148 "Qulice quality check completed in %[nano]s",
149 System.nanoTime() - start
150 );
151 }
152
153
154
155
156
157 public final MavenSession session() {
158 return this.sess;
159 }
160
161
162
163
164
165
166 protected abstract void doExecute() throws MojoFailureException;
167
168
169
170
171
172 protected final MavenEnvironment env() {
173 return this.environment;
174 }
175
176 }