1 package org.codehaus.mojo.exec;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.nio.file.Path;
24 import java.nio.file.Paths;
25 import java.util.List;
26
27 import org.apache.maven.artifact.Artifact;
28 import org.apache.maven.execution.MavenSession;
29 import org.apache.maven.model.Resource;
30 import org.apache.maven.plugin.AbstractMojo;
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.plugins.annotations.Parameter;
33 import org.apache.maven.project.MavenProject;
34 import org.codehaus.plexus.util.cli.CommandLineUtils;
35
36
37
38
39
40
41
42
43 public abstract class AbstractExecMojo
44 extends AbstractMojo
45 {
46
47
48
49 @Parameter( defaultValue = "${project}", readonly = true )
50 protected MavenProject project;
51
52 @Parameter( defaultValue = "${session}", readonly = true, required = true )
53 private MavenSession session;
54
55 @Parameter( readonly = true, defaultValue = "${plugin.artifacts}" )
56 private List<Artifact> pluginDependencies;
57
58
59
60
61
62
63
64
65
66
67
68
69
70 @Parameter
71 protected ExecutableDependency executableDependency;
72
73
74
75
76
77 @Parameter( property = "sourceRoot" )
78 private File sourceRoot;
79
80
81
82
83
84 @Parameter( property = "testSourceRoot" )
85 private File testSourceRoot;
86
87
88
89
90 @Parameter( property = "exec.args" )
91 private String commandlineArgs;
92
93
94
95
96
97 @Parameter( property = "exec.classpathScope", defaultValue = "runtime" )
98 protected String classpathScope;
99
100
101
102
103
104
105
106
107 @Parameter( property = "exec.skip", defaultValue = "false", alias = "skip" )
108 private boolean skip;
109
110
111
112
113
114
115
116 @Parameter( property = "addResourcesToClasspath", defaultValue = "false" )
117 private boolean addResourcesToClasspath;
118
119
120
121
122
123
124
125 @Parameter( property = "addOutputToClasspath", defaultValue = "true" )
126 private boolean addOutputToClasspath;
127
128
129
130
131
132
133
134
135 protected void collectProjectArtifactsAndClasspath( List<Artifact> artifacts, List<Path> theClasspathFiles )
136 {
137 if ( addResourcesToClasspath )
138 {
139 for ( Resource r : project.getBuild().getResources() )
140 {
141 theClasspathFiles.add( Paths.get( r.getDirectory() ) );
142 }
143 }
144
145 if ( "compile".equals( classpathScope ) )
146 {
147 artifacts.addAll( project.getCompileArtifacts() );
148 if ( addOutputToClasspath )
149 {
150 theClasspathFiles.add( Paths.get( project.getBuild().getOutputDirectory() ) );
151 }
152 }
153 else if ( "test".equals( classpathScope ) )
154 {
155 artifacts.addAll( project.getTestArtifacts() );
156 if ( addOutputToClasspath )
157 {
158 theClasspathFiles.add( Paths.get( project.getBuild().getTestOutputDirectory() ) );
159 theClasspathFiles.add( Paths.get( project.getBuild().getOutputDirectory() ) );
160 }
161 }
162 else if ( "runtime".equals( classpathScope ) )
163 {
164 artifacts.addAll( project.getRuntimeArtifacts() );
165 if ( addOutputToClasspath )
166 {
167 theClasspathFiles.add( Paths.get( project.getBuild().getOutputDirectory() ) );
168 }
169 }
170 else if ( "system".equals( classpathScope ) )
171 {
172 artifacts.addAll( project.getSystemArtifacts() );
173 }
174 else
175 {
176 throw new IllegalStateException( "Invalid classpath scope: " + classpathScope );
177 }
178
179 getLog().debug( "Collected project artifacts " + artifacts );
180 getLog().debug( "Collected project classpath " + theClasspathFiles );
181 }
182
183
184
185
186
187
188
189
190
191 protected String[] parseCommandlineArgs()
192 throws MojoExecutionException
193 {
194 if ( commandlineArgs == null )
195 {
196 return null;
197 }
198 else
199 {
200 try
201 {
202 return CommandLineUtils.translateCommandline( commandlineArgs );
203 }
204 catch ( Exception e )
205 {
206 throw new MojoExecutionException( e.getMessage() );
207 }
208 }
209 }
210
211
212
213
214 protected boolean hasCommandlineArgs()
215 {
216 return ( commandlineArgs != null );
217 }
218
219
220
221
222 protected void registerSourceRoots()
223 {
224 if ( sourceRoot != null )
225 {
226 getLog().info( "Registering compile source root " + sourceRoot );
227 project.addCompileSourceRoot( sourceRoot.toString() );
228 }
229
230 if ( testSourceRoot != null )
231 {
232 getLog().info( "Registering compile test source root " + testSourceRoot );
233 project.addTestCompileSourceRoot( testSourceRoot.toString() );
234 }
235 }
236
237
238
239
240
241
242 protected boolean isSkip()
243 {
244 return skip;
245 }
246
247 protected final MavenSession getSession()
248 {
249 return session;
250 }
251
252 protected final List<Artifact> getPluginDependencies()
253 {
254 return pluginDependencies;
255 }
256
257
258
259
260
261
262
263 protected Artifact findExecutableArtifact()
264 throws MojoExecutionException
265 {
266
267
268 Artifact executableTool = null;
269 for ( Artifact pluginDep : this.pluginDependencies )
270 {
271 if ( this.executableDependency.matches( pluginDep ) )
272 {
273 executableTool = pluginDep;
274 break;
275 }
276 }
277
278 if ( executableTool == null )
279 {
280 throw new MojoExecutionException( "No dependency of the plugin matches the specified executableDependency."
281 + " Specified executableToolAssembly is: " + executableDependency.toString() );
282 }
283
284 return executableTool;
285 }
286 }