1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.codehaus.gmavenplus.mojo;
18
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.execution.MavenSession;
21 import org.apache.maven.plugin.AbstractMojo;
22 import org.apache.maven.plugin.MojoExecution;
23 import org.apache.maven.plugins.annotations.Parameter;
24 import org.apache.maven.project.MavenProject;
25 import org.codehaus.gmavenplus.model.IncludeClasspath;
26 import org.codehaus.gmavenplus.model.internal.Version;
27 import org.codehaus.gmavenplus.util.ClassWrangler;
28
29 import java.io.File;
30 import java.net.MalformedURLException;
31 import java.util.List;
32
33 import static java.util.Collections.emptyList;
34
35
36
37
38
39
40
41
42 public abstract class AbstractGroovyMojo extends AbstractMojo {
43
44
45
46
47 protected static final String GROOVY_SOURCES_PATTERN = "**" + File.separator + "*.groovy";
48
49
50
51
52 protected static final String JAVA_SOURCES_PATTERN = "**" + File.separator + "*.java";
53
54
55
56
57 protected static final Version JAVA_1_7 = new Version(1, 7);
58
59
60
61
62 protected static final Version JAVA_1_8 = new Version(1, 8);
63
64
65
66
67 protected static final Version JAVA_12 = new Version(12);
68
69
70
71
72 protected static final Version GROOVY_1_5_0 = new Version(1, 5, 0);
73
74
75
76
77 protected ClassWrangler classWrangler;
78
79
80
81
82
83
84 @Parameter(property = "project", required = true, readonly = true)
85 protected MavenProject project;
86
87
88
89
90 @Parameter(property = "session", required = true, readonly = true)
91 protected MavenSession session;
92
93
94
95
96 @Parameter(property = "plugin.artifacts", required = true, readonly = true)
97 protected List<Artifact> pluginArtifacts;
98
99
100
101
102 @Parameter(property = "mojoExecution", required = true, readonly = true)
103 protected MojoExecution mojoExecution;
104
105
106
107
108
109 protected Version minGroovyVersion = GROOVY_1_5_0;
110
111
112
113
114 protected void logPluginClasspath() {
115 if (getLog().isDebugEnabled()) {
116 StringBuilder sb = new StringBuilder();
117 for (int i = 0; i < pluginArtifacts.size(); i++) {
118 sb.append(pluginArtifacts.get(i).getFile());
119 if (i < pluginArtifacts.size() - 1) {
120 sb.append(", ");
121 }
122 }
123 getLog().debug("Plugin classpath:\n" + sb);
124 }
125 }
126
127
128
129
130
131
132 protected boolean isJavaSupportIndy() {
133 return getJavaVersion().compareTo(JAVA_1_7, false) >= 0;
134 }
135
136
137
138
139
140
141 protected boolean isJavaSupportPreviewFeatures() {
142 return getJavaVersion().compareTo(JAVA_12, false) >= 0;
143 }
144
145
146
147
148
149
150 protected boolean isJavaSupportParameters() {
151 return getJavaVersion().compareTo(JAVA_1_8, false) >= 0;
152 }
153
154
155
156
157
158
159 protected Version getJavaVersion() {
160 return Version.parseFromString(getJavaVersionString());
161 }
162
163
164
165
166
167
168 protected String getJavaVersionString() {
169 return System.getProperty("java.version");
170 }
171
172
173
174
175
176
177 protected boolean groovyVersionSupportsAction() {
178 return classWrangler.getGroovyVersion() != null && groovyAtLeast(minGroovyVersion);
179 }
180
181
182
183
184
185
186
187 protected boolean groovyAtLeast(Version version) {
188 return ClassWrangler.groovyAtLeast(classWrangler.getGroovyVersion(), version);
189 }
190
191
192
193
194
195
196
197 protected boolean groovyIs(Version version) {
198 return ClassWrangler.groovyIs(classWrangler.getGroovyVersion(), version);
199 }
200
201
202
203
204
205
206
207 protected boolean groovyNewerThan(Version version) {
208 return ClassWrangler.groovyNewerThan(classWrangler.getGroovyVersion(), version);
209 }
210
211
212
213
214
215
216
217 protected boolean groovyOlderThan(Version version) {
218 return ClassWrangler.groovyOlderThan(classWrangler.getGroovyVersion(), version);
219 }
220
221
222
223
224
225
226
227 protected boolean isGroovyIndy() {
228 return classWrangler.isGroovyIndy();
229 }
230
231
232
233
234
235
236
237
238 protected void setupClassWrangler(List<?> classpath, IncludeClasspath includeClasspath) throws MalformedURLException {
239 if (IncludeClasspath.PROJECT_ONLY.equals(includeClasspath)) {
240 getLog().info("Using isolated classloader, without GMavenPlus classpath.");
241 classWrangler = new ClassWrangler(classpath, ClassLoader.getSystemClassLoader(), getLog());
242 } else if (IncludeClasspath.PROJECT_AND_PLUGIN.equals(includeClasspath)) {
243 getLog().info("Using plugin classloader, includes GMavenPlus and project classpath.");
244 classWrangler = new ClassWrangler(classpath, getClass().getClassLoader(), getLog());
245 } else {
246 getLog().info("Using plugin classloader, includes GMavenPlus classpath, but not project classpath.");
247 classWrangler = new ClassWrangler(emptyList(), getClass().getClassLoader(), getLog());
248 }
249 }
250 }