Skip to content

Commit 7684096

Browse files
committed
exec mojo can run executableDependency instead of executable
1 parent 79ffade commit 7684096

File tree

6 files changed

+170
-45
lines changed

6 files changed

+170
-45
lines changed

src/main/java/org/codehaus/mojo/exec/AbstractExecMojo.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@
2323
import java.util.List;
2424

2525
import org.apache.maven.artifact.Artifact;
26+
import org.apache.maven.artifact.factory.ArtifactFactory;
27+
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
28+
import org.apache.maven.artifact.repository.ArtifactRepository;
29+
import org.apache.maven.artifact.resolver.ArtifactResolver;
2630
import org.apache.maven.plugin.AbstractMojo;
2731
import org.apache.maven.plugin.MojoExecutionException;
32+
import org.apache.maven.plugins.annotations.Component;
2833
import org.apache.maven.plugins.annotations.Parameter;
2934
import org.apache.maven.project.MavenProject;
35+
import org.apache.maven.project.MavenProjectBuilder;
3036
import org.codehaus.plexus.util.cli.CommandLineUtils;
3137

3238
/**
@@ -45,6 +51,40 @@ public abstract class AbstractExecMojo
4551
@Parameter( defaultValue = "${project}", readonly = true )
4652
protected MavenProject project;
4753

54+
@Component
55+
private ArtifactResolver artifactResolver;
56+
57+
@Component
58+
private ArtifactFactory artifactFactory;
59+
60+
@Component
61+
private MavenProjectBuilder projectBuilder;
62+
63+
@Component
64+
private ArtifactMetadataSource metadataSource;
65+
66+
@Parameter( readonly = true, required = true, defaultValue = "${localRepository}" )
67+
private ArtifactRepository localRepository;
68+
69+
@Parameter( readonly = true, required = true, defaultValue = "${project.remoteArtifactRepositories}" )
70+
private List<ArtifactRepository> remoteRepositories;
71+
72+
@Parameter( readonly = true, defaultValue = "${plugin.artifacts}" )
73+
private List<Artifact> pluginDependencies;
74+
75+
/**
76+
* If provided the ExecutableDependency identifies which of the plugin dependencies contains the executable class.
77+
* This will have the affect of only including plugin dependencies required by the identified ExecutableDependency.
78+
* <p/>
79+
* If includeProjectDependencies is set to <code>true</code>, all of the project dependencies will be included on
80+
* the executable's classpath. Whether a particular project dependency is a dependency of the identified
81+
* ExecutableDependency will be irrelevant to its inclusion in the classpath.
82+
*
83+
* @since 1.1-beta-1
84+
*/
85+
@Parameter
86+
protected ExecutableDependency executableDependency;
87+
4888
/**
4989
* This folder is added to the list of those folders containing source to be compiled. Use this if your plugin
5090
* generates source code.
@@ -185,4 +225,34 @@ protected boolean isSkip()
185225
{
186226
return skip;
187227
}
228+
229+
/**
230+
* Examine the plugin dependencies to find the executable artifact.
231+
*
232+
* @return an artifact which refers to the actual executable tool (not a POM)
233+
* @throws MojoExecutionException if no executable artifact was found
234+
*/
235+
protected Artifact findExecutableArtifact()
236+
throws MojoExecutionException
237+
{
238+
// ILimitedArtifactIdentifier execToolAssembly = this.getExecutableToolAssembly();
239+
240+
Artifact executableTool = null;
241+
for ( Artifact pluginDep : this.pluginDependencies )
242+
{
243+
if ( this.executableDependency.matches( pluginDep ) )
244+
{
245+
executableTool = pluginDep;
246+
break;
247+
}
248+
}
249+
250+
if ( executableTool == null )
251+
{
252+
throw new MojoExecutionException( "No dependency of the plugin matches the specified executableDependency."
253+
+ " Specified executableToolAssembly is: " + executableDependency.toString() );
254+
}
255+
256+
return executableTool;
257+
}
188258
}

src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -150,19 +150,6 @@ public class ExecJavaMojo
150150
@Parameter( property = "exec.includePluginsDependencies", defaultValue = "false" )
151151
private boolean includePluginDependencies;
152152

153-
/**
154-
* If provided the ExecutableDependency identifies which of the plugin dependencies contains the executable class.
155-
* This will have the affect of only including plugin dependencies required by the identified ExecutableDependency.
156-
* <p/>
157-
* If includeProjectDependencies is set to <code>true</code>, all of the project dependencies will be included on
158-
* the executable's classpath. Whether a particular project dependency is a dependency of the identified
159-
* ExecutableDependency will be irrelevant to its inclusion in the classpath.
160-
*
161-
* @since 1.1-beta-1
162-
*/
163-
@Parameter
164-
private ExecutableDependency executableDependency;
165-
166153
/**
167154
* Whether to interrupt/join and possibly stop the daemon threads upon quitting. <br/>
168155
* If this is <code>false</code>, maven does nothing about the daemon threads. When maven has no more work to do,
@@ -680,36 +667,6 @@ private Artifact getExecutablePomArtifact( Artifact executableArtifact )
680667
executableArtifact.getVersion(), "pom" );
681668
}
682669

683-
/**
684-
* Examine the plugin dependencies to find the executable artifact.
685-
*
686-
* @return an artifact which refers to the actual executable tool (not a POM)
687-
* @throws MojoExecutionException if no executable artifact was found
688-
*/
689-
private Artifact findExecutableArtifact()
690-
throws MojoExecutionException
691-
{
692-
// ILimitedArtifactIdentifier execToolAssembly = this.getExecutableToolAssembly();
693-
694-
Artifact executableTool = null;
695-
for ( Artifact pluginDep : this.pluginDependencies )
696-
{
697-
if ( this.executableDependency.matches( pluginDep ) )
698-
{
699-
executableTool = pluginDep;
700-
break;
701-
}
702-
}
703-
704-
if ( executableTool == null )
705-
{
706-
throw new MojoExecutionException( "No dependency of the plugin matches the specified executableDependency."
707-
+ " Specified executableToolAssembly is: " + executableDependency.toString() );
708-
}
709-
710-
return executableTool;
711-
}
712-
713670
/**
714671
* Resolve the executable dependencies for the specified project
715672
*

src/main/java/org/codehaus/mojo/exec/ExecMojo.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.ArrayList;
3030
import java.util.Arrays;
3131
import java.util.Collection;
32+
import java.util.Collections;
3233
import java.util.HashMap;
3334
import java.util.List;
3435
import java.util.Map;
@@ -49,14 +50,23 @@
4950
import org.apache.commons.exec.PumpStreamHandler;
5051
import org.apache.commons.exec.ShutdownHookProcessDestroyer;
5152
import org.apache.maven.artifact.Artifact;
53+
import org.apache.maven.artifact.factory.ArtifactFactory;
54+
import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
55+
import org.apache.maven.artifact.repository.ArtifactRepository;
56+
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
57+
import org.apache.maven.artifact.resolver.ArtifactResolver;
5258
import org.apache.maven.artifact.resolver.filter.AndArtifactFilter;
5359
import org.apache.maven.artifact.resolver.filter.IncludesArtifactFilter;
5460
import org.apache.maven.execution.MavenSession;
61+
import org.apache.maven.model.Dependency;
5562
import org.apache.maven.plugin.MojoExecutionException;
63+
import org.apache.maven.plugins.annotations.Component;
5664
import org.apache.maven.plugins.annotations.Mojo;
5765
import org.apache.maven.plugins.annotations.Parameter;
5866
import org.apache.maven.plugins.annotations.ResolutionScope;
5967
import org.apache.maven.project.MavenProject;
68+
import org.apache.maven.project.MavenProjectBuilder;
69+
import org.apache.maven.project.artifact.MavenMetadataSource;
6070
import org.apache.maven.toolchain.Toolchain;
6171
import org.apache.maven.toolchain.ToolchainManager;
6272
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
@@ -82,7 +92,7 @@ public class ExecMojo
8292
/**
8393
* <p>
8494
* The executable. Can be a full path or the name of the executable. In the latter case, the executable must be in
85-
* the PATH for the execution to work.
95+
* the PATH for the execution to work. Omit when using <code>executableDependency</code>.
8696
* </p>
8797
* <p>
8898
* The plugin will search for the executable in the following order:
@@ -97,7 +107,7 @@ public class ExecMojo
97107
*
98108
* @since 1.0
99109
*/
100-
@Parameter( property = "exec.executable", required = true )
110+
@Parameter( property = "exec.executable" )
101111
private String executable;
102112

103113
/**
@@ -212,6 +222,17 @@ public class ExecMojo
212222
public void execute()
213223
throws MojoExecutionException
214224
{
225+
if ( executable == null )
226+
{
227+
if (executableDependency == null)
228+
{
229+
throw new MojoExecutionException( "The parameter 'executable' is missing or invalid" );
230+
}
231+
232+
executable = findExecutableArtifact().getFile().getAbsolutePath();
233+
getLog().debug( "using executable dependency " + executable);
234+
}
235+
215236
if ( isSkip() )
216237
{
217238
getLog().info( "skipping execute as per configuration" );
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
------
2+
Using executable binary dependencies with the exec:exec goal
3+
------
4+
Markus KARG
5+
------
6+
2016-05-23
7+
------
8+
9+
~~ Copyright 2016 The Codehaus
10+
~~
11+
~~ Licensed under the Apache License, Version 2.0 (the "License");
12+
~~ you may not use this file except in compliance with the License.
13+
~~ You may obtain a copy of the License at
14+
~~
15+
~~ http://www.apache.org/licenses/LICENSE-2.0
16+
~~
17+
~~ Unless required by applicable law or agreed to in writing, software
18+
~~ distributed under the License is distributed on an "AS IS" BASIS,
19+
~~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
~~ See the License for the specific language governing permissions and
21+
~~ limitations under the License.
22+
23+
~~ NOTE: For help with the syntax of this file, see:
24+
~~ http://maven.apache.org/doxia/references/apt-format.html
25+
26+
27+
Using Executable Binary Dependencies Instead of Local Executables
28+
29+
Instead of invoking a locally installed binary executable (identified by <<<executable>>>), a dependency (identified by <<<executableDependency>>>) can directly get executed instead.
30+
This particularly is useful when the dependency is an executable binary like <<<.exe>>> or <<<.bat>>> file produced by a different Maven project which got deployed into a repository.
31+
The binary gets pulled to the local repository and is getting executed <<right there>> without the need to know its actual file name or location on disk: Just the GA coordinates (<<<groupId>>>, <<<artifactId>>>) are needed.
32+
Hence the <<<executable>>> parameter has to be omitted in favor of the <<<executableDependency>>> parameter.
33+
34+
There are two ways of using executable binary dependencies: Project dependencies and plugin dependencies. Currently the <<<exec>>> goal only supports plugin dependencies.
35+
36+
* Plugin Dependencies
37+
38+
Plugin Dependencies are referenced from within the plugin configuration.
39+
40+
** pom.xml
41+
42+
-------------------
43+
<project>
44+
...
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.codehaus.mojo</groupId>
49+
<artifactId>exec-maven-plugin</artifactId>
50+
<version>${project.version}</version>
51+
...
52+
<configuration>
53+
<executableDependency>
54+
<!-- REFERENCES plugin dependency, see declaration below -->
55+
<groupId>your-group</groupId>
56+
<artifactId>your-artifact</artifactId>
57+
</executableDependency>
58+
</configuration>
59+
<dependencies>
60+
<dependency>
61+
<!-- DECLARES plugin dependency, see reference above -->
62+
<groupId>your-group</groupId>
63+
<artifactId>your-artifact</artifactId>
64+
<type>exe</type>
65+
<version>your-version</version>
66+
</dependency>
67+
</dependencies>
68+
...
69+
</plugin>
70+
</plugins>
71+
</build>
72+
...
73+
</project>
74+
-------------------

src/site/apt/index.apt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,5 @@ Exec Maven Plugin
6464
* {{{./examples/example-exec-using-plugin-dependencies.html} Using plugin dependencies with exec:exec}}
6565

6666
* {{{./examples/example-exec-using-toolchains.html} Using toolchains instead of explicit paths}}
67+
68+
* {{{./examples/example-exec-using-executabledependency.html} Using executable binary dependencies instead of local executables}}

src/site/site.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<item name="Changing the classpath scope when running Java programs" href="examples/example-exec-or-java-change-classpath-scope.html"/>
1414
<item name="Using plugin dependencies with exec:exec" href="examples/example-exec-using-plugin-dependencies.html"/>
1515
<item name="Using toolchains with exec:exec" href="examples/example-exec-using-toolchains.html"/>
16+
<item name="Using executable binary dependencies instead of local executables" href="examples/example-exec-using-executabledependency.html"/>
1617
</menu>
1718
</body>
1819
</project>

0 commit comments

Comments
 (0)