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 public abstract class AbstractQuliceMojo extends AbstractMojo
26 implements Contextualizable {
27
28
29
30
31 private final DefaultMavenEnvironment environment =
32 new DefaultMavenEnvironment();
33
34
35
36
37 @Parameter(defaultValue = "${project}", readonly = true)
38 private MavenProject project;
39
40
41
42
43 @Parameter(defaultValue = "${session}", readonly = true)
44 private MavenSession sess;
45
46
47
48
49 @Inject
50 private MavenPluginManager manager;
51
52
53
54
55 @Parameter(property = "qulice.skip", defaultValue = "false")
56 private boolean skip;
57
58
59
60
61 @Parameter(property = "qulice.excludes")
62 private final Collection<String> excludes = new LinkedList<>();
63
64
65
66
67
68 @Parameter(
69 property = "qulice.asserts",
70 required = false
71 )
72 private final Collection<String> asserts = new LinkedList<>();
73
74
75
76
77
78 @Parameter(property = "encoding", defaultValue = "${project.build.sourceEncoding}")
79 private String charset;
80
81
82
83
84
85 public final void setProject(final MavenProject proj) {
86 this.project = proj;
87 }
88
89
90
91
92
93 public final void setSkip(final boolean skp) {
94 this.skip = skp;
95 }
96
97
98
99
100
101 public final void setAsserts(final Collection<String> val) {
102 this.asserts.clear();
103 this.asserts.addAll(val);
104 }
105
106
107
108
109
110 public final void setExcludes(final Collection<String> exprs) {
111 this.excludes.clear();
112 this.excludes.addAll(exprs);
113 }
114
115
116
117
118
119 public void setEncoding(final String encoding) {
120 this.charset = encoding;
121 }
122
123 @Override
124 public final void contextualize(final Context ctx) {
125 this.environment.setContext(ctx);
126 }
127
128 @Override
129 public final void execute() throws MojoFailureException {
130 StaticLoggerBinder.getSingleton().setMavenLog(this.getLog());
131 if (this.skip) {
132 this.getLog().info("Execution skipped");
133 return;
134 }
135 this.environment.setProject(this.project);
136 this.environment.setMojoExecutor(
137 new MojoExecutor(this.manager, this.sess)
138 );
139 this.environment.setExcludes(this.excludes);
140 this.environment.setAssertion(this.asserts);
141 this.environment.setEncoding(this.charset);
142 this.doExecute();
143 Logger.info(this, "Qulice quality check completed");
144 }
145
146
147
148
149
150 public final MavenSession session() {
151 return this.sess;
152 }
153
154
155
156
157
158
159 protected abstract void doExecute() throws MojoFailureException;
160
161
162
163
164
165 protected final MavenEnvironment env() {
166 return this.environment;
167 }
168 }