Class ExecMojo
- java.lang.Object
-
- org.apache.maven.plugin.AbstractMojo
-
- org.codehaus.mojo.exec.AbstractExecMojo
-
- org.codehaus.mojo.exec.ExecMojo
-
- All Implemented Interfaces:
org.apache.maven.plugin.ContextEnabled,org.apache.maven.plugin.Mojo
@Mojo(name="exec", threadSafe=true, requiresDependencyResolution=TEST) public class ExecMojo extends AbstractExecMojoA Plugin for executing external programs.- Since:
- 1.0
- Version:
- $Id$
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<?>argumentsA list of arguments passed to theexecutable, which should be of type<argument>or<classpath>.private booleanasyncIf set to true the child process executes asynchronously and build execution continues in parallel.private booleanasyncDestroyOnShutdownIf set to true, the asynchronous child process is destroyed upon JVM shutdown.private java.io.Filebasedirprivate java.io.FilebuildDirectorystatic java.lang.StringCLASSPATH_TOKENprivate java.io.FileenvironmentScriptEnvironment script to be merged with environmentVariables This script is platform specifics, on Unix its must be Bourne shell format.private java.util.Map<java.lang.String,java.lang.String>environmentVariablesEnvironment variables to pass to the executed program.private java.lang.StringexecutableThe executable.private booleanlongClasspathIf set to true the classpath and the main class will be written to a MANIFEST.MF file and wrapped into a jar.private booleanlongModulepathIf set to true the modulepath and the main class will be written as an @arg file Instead of '--module-path/-p MODULEPATH ' the exec plugin executes '@modulepath'.static java.lang.StringMODULEPATH_TOKENprivate java.io.FileoutputFileProgram standard and error output will be redirected to the file specified by this optional field.private org.apache.commons.exec.ProcessDestroyerprocessDestroyerprivate booleanquietLogsWhen combined withexec.useMavenLogger=true, prints all executed program output at debug level instead of the default info level to the Maven logger.private org.apache.maven.execution.MavenSessionsessionThe current build session instance.private int[]successCodesExit codes to be resolved as successful execution for non-compliant applications (applications not returning 0 for success).private inttimeoutTimeout in full milliseconds, default is0.private java.lang.StringtoolchainThe toolchain.private booleanuseMavenLoggerWhen enabled, program standard and error output will be redirected to the Maven logger as Info and Error level logs, respectively.private java.io.FileworkingDirectoryThe current working directory.-
Fields inherited from class org.codehaus.mojo.exec.AbstractExecMojo
classpathScope, executableDependency, project
-
-
Constructor Summary
Constructors Constructor Description ExecMojo()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static voidaddToClasspath(java.lang.StringBuffer theClasspath, java.lang.String toAdd)private java.lang.StringcomputeClasspathString(AbstractPath specifiedClasspath)Compute the classpath from the specified Classpath.private java.util.List<java.lang.String>computePath(AbstractPath specifiedClasspath)Compute the classpath from the specified Classpath.private voidcreateArgFile(java.lang.String filePath, java.util.List<java.lang.String> lines)protected java.util.Map<java.lang.String,java.lang.String>createEnvs(java.io.File envScriptFile)protected java.io.FilecreateEnvWrapperFile(java.io.File envScript)private java.io.FilecreateJar(java.util.List<java.lang.String> classPath, java.lang.String mainClass)Create a jar with just a manifest containing a Main-Class entry for SurefireBooter and a Class-Path entry for all classpath elements.voidexecute()priority in the execute method will be to use System properties arguments over the pom specification.protected intexecuteCommandLine(org.apache.commons.exec.Executor exec, org.apache.commons.exec.CommandLine commandLine, java.util.Map<java.lang.String,java.lang.String> enviro, java.io.FileOutputStream outputFile)protected intexecuteCommandLine(org.apache.commons.exec.Executor exec, org.apache.commons.exec.CommandLine commandLine, java.util.Map<java.lang.String,java.lang.String> enviro, java.io.OutputStream out, java.io.OutputStream err)protected intexecuteCommandLine(org.apache.commons.exec.Executor exec, org.apache.commons.exec.CommandLine commandLine, java.util.Map<java.lang.String,java.lang.String> enviro, org.apache.commons.exec.PumpStreamHandler psh)private voidfillSuccessCodes(org.apache.commons.exec.Executor exec)private java.util.List<org.apache.maven.artifact.Artifact>filterArtifacts(java.util.List<org.apache.maven.artifact.Artifact> artifacts, java.util.Collection<java.lang.String> dependencies)(package private) static java.lang.StringfindExecutable(java.lang.String executable, java.util.List<java.lang.String> paths)(package private) java.lang.StringgetExecutable()private static java.util.List<java.lang.String>getExecutableExtensions()(package private) org.apache.commons.exec.CommandLinegetExecutablePath(java.util.Map<java.lang.String,java.lang.String> enviro, java.io.File dir)private java.util.List<java.lang.String>getExecutablePaths(java.util.Map<java.lang.String,java.lang.String> enviro)protected org.apache.commons.exec.ProcessDestroyergetProcessDestroyer()int[]getSuccessCodes()protected java.lang.StringgetSystemProperty(java.lang.String key)private org.apache.maven.toolchain.ToolchaingetToolchain()private voidhandleArguments(java.util.List<java.lang.String> commandArguments)private voidhandleCommandLineArgs(java.util.List<java.lang.String> commandArguments)private java.util.Map<java.lang.String,java.lang.String>handleSystemEnvVariables()private voidhandleSystemPropertyArguments(java.lang.String argsProp, java.util.List<java.lang.String> commandArguments)private voidhandleWorkingDirectory()This is a convenient method to make the execute method a little bit more readable.private static booleanhasExecutableExtension(java.lang.String exec)private static booleanhasNativeExtension(java.lang.String exec)private booleanisLongClassPathArgument(java.lang.String arg)private booleanisLongModulePathArgument(java.lang.String arg)(package private) booleanisResultCodeAFailure(int result)(package private) voidsetArguments(java.util.List<?> arguments)(package private) voidsetBasedir(java.io.File basedir)(package private) voidsetExecutable(java.lang.String executable)(package private) voidsetProject(org.apache.maven.project.MavenProject project)voidsetSuccessCodes(java.lang.Integer... list)(package private) voidsetWorkingDirectory(java.io.File workingDir)(package private) voidsetWorkingDirectory(java.lang.String workingDir)-
Methods inherited from class org.codehaus.mojo.exec.AbstractExecMojo
collectProjectArtifactsAndClasspath, findExecutableArtifact, getSession, hasCommandlineArgs, isSkip, parseCommandlineArgs, registerSourceRoots
-
-
-
-
Field Detail
-
executable
@Parameter(property="exec.executable") private java.lang.String executable
The executable. Can be a full path or the name of the executable. In the latter case, the executable must be in the PATH for the execution to work. Omit when using
executableDependency.The plugin will search for the executable in the following order:
- relative to the root of the project
- as toolchain executable
- relative to the working directory (Windows only)
- relative to the directories specified in the system property PATH (Windows Only)
- Since:
- 1.0
-
timeout
@Parameter(property="exec.timeout", defaultValue="0") private int timeoutTimeout in full milliseconds, default is
0.When set to a value larger than zero, the executable is forcefully terminated if it did not finish within this time, and the build will fail.
- Since:
- 3.0.0
-
toolchain
@Parameter(property="exec.toolchain", defaultValue="jdk") private java.lang.String toolchainThe toolchain. If omitted,
"jdk"is assumed.
-
workingDirectory
@Parameter(property="exec.workingdir") private java.io.File workingDirectory
The current working directory. Optional. If not specified, basedir will be used.- Since:
- 1.0
-
outputFile
@Parameter(property="exec.outputFile") private java.io.File outputFile
Program standard and error output will be redirected to the file specified by this optional field. If not specified the standard Maven logging is used.
Note: Be aware thatSystem.outandSystem.erruse buffering, so don't rely on the order!- Since:
- 1.1-beta-2
- See Also:
System.err,System.in
-
useMavenLogger
@Parameter(property="exec.useMavenLogger", defaultValue="false") private boolean useMavenLoggerWhen enabled, program standard and error output will be redirected to the Maven logger as Info and Error level logs, respectively. If not enabled the traditional behavior of program output being directed to standard System.out and System.err is used.
NOTE: When enabled, to log the program standard out as Maven Debug level instead of Info level useexec.quietLogs=true.
This option can be extremely helpful when combined with multithreaded builds for two reasons:
- Program output is suffixed with the owning thread name, making it easier to trace execution of a specific projects build thread.
- Program output will not get jumbled with other maven log messages.
exec:execto run a script to echo a count from 1 to 100 as:for i in {1..100} do echo "${project.artifactId} - $i" doneWhen this script is run multi-threaded on two modules,module1andmodule2, you might get output such as:[BuilderThread 1] [INFO] --- exec-maven-plugin:1.6.0:exec (test) @ module1 --- [BuilderThread 2] [INFO] --- exec-maven-plugin:1.6.0:exec (test) @ module2 --- ... module2 - 98 modu module1 - 97 module1 - le2 - 9899 ...
With this flag enabled, the output will instead come something similar to:... [Exec Stream Pumper] [INFO] [BuilderThread 2] module2 - 98 [Exec Stream Pumper] [INFO] [BuilderThread 1] module1 - 97 [Exec Stream Pumper] [INFO] [BuilderThread 1] module1 - 98 [Exec Stream Pumper] [INFO] [BuilderThread 2] module2 - 99 ...
NOTE 1: To show the thread in the Maven log, configure the Maven installations conf/logging/simplelogger.properties option:org.slf4j.simpleLogger.showThreadName=true
NOTE 2: This option is ignored whenexec.outputFileis specified.- Since:
- 3.0.0
- See Also:
System.err,System.in
-
quietLogs
@Parameter(property="exec.quietLogs", defaultValue="false") private boolean quietLogsWhen combined withexec.useMavenLogger=true, prints all executed program output at debug level instead of the default info level to the Maven logger.- Since:
- 3.0.0
-
arguments
@Parameter private java.util.List<?> arguments
A list of arguments passed to the
executable, which should be of type<argument>or<classpath>. Can be overridden by using theexec.argsenvironment variable.- Since:
- 1.0
-
basedir
@Parameter(readonly=true, required=true, defaultValue="${basedir}") private java.io.File basedir- Since:
- 1.0
-
buildDirectory
@Parameter(readonly=true, required=true, defaultValue="${project.build.directory}") private java.io.File buildDirectory- Since:
- 3.0.0
-
environmentVariables
@Parameter private java.util.Map<java.lang.String,java.lang.String> environmentVariables
Environment variables to pass to the executed program. For example if you want to set the LANG var:
<environmentVariables> <LANG>en_US</LANG> </environmentVariables>- Since:
- 1.1-beta-2
-
environmentScript
@Parameter private java.io.File environmentScript
Environment script to be merged with environmentVariables This script is platform specifics, on Unix its must be Bourne shell format. Use this feature if you have a need to create environment variable dynamically such as invoking Visual Studio environment script file- Since:
- 1.4.0
-
session
@Parameter(defaultValue="${session}", readonly=true) private org.apache.maven.execution.MavenSession sessionThe current build session instance. This is used for toolchain manager API calls.
-
successCodes
@Parameter private int[] successCodes
Exit codes to be resolved as successful execution for non-compliant applications (applications not returning 0 for success).- Since:
- 1.1.1
-
longClasspath
@Parameter(property="exec.longClasspath", defaultValue="false") private boolean longClasspathIf set to true the classpath and the main class will be written to a MANIFEST.MF file and wrapped into a jar. Instead of '-classpath/-cp CLASSPATH mainClass' the exec plugin executes '-jar maven-exec.jar'.- Since:
- 1.1.2
-
longModulepath
@Parameter(property="exec.longModulepath", defaultValue="true") private boolean longModulepathIf set to true the modulepath and the main class will be written as an @arg file Instead of '--module-path/-p MODULEPATH ' the exec plugin executes '@modulepath'.- Since:
- 1.1.2
-
async
@Parameter(property="exec.async", defaultValue="false") private boolean asyncIf set to true the child process executes asynchronously and build execution continues in parallel.
-
asyncDestroyOnShutdown
@Parameter(property="exec.asyncDestroyOnShutdown", defaultValue="true") private boolean asyncDestroyOnShutdownIf set to true, the asynchronous child process is destroyed upon JVM shutdown. If set to false, asynchronous child process continues execution after JVM shutdown. Applies only to asynchronous processes; ignored for synchronous processes.
-
CLASSPATH_TOKEN
public static final java.lang.String CLASSPATH_TOKEN
- See Also:
- Constant Field Values
-
MODULEPATH_TOKEN
public static final java.lang.String MODULEPATH_TOKEN
- See Also:
- Constant Field Values
-
processDestroyer
private org.apache.commons.exec.ProcessDestroyer processDestroyer
-
-
Method Detail
-
execute
public void execute() throws org.apache.maven.plugin.MojoExecutionExceptionpriority in the execute method will be to use System properties arguments over the pom specification.- Throws:
org.apache.maven.plugin.MojoExecutionException- if a failure happens
-
handleSystemEnvVariables
private java.util.Map<java.lang.String,java.lang.String> handleSystemEnvVariables() throws org.apache.maven.plugin.MojoExecutionException- Throws:
org.apache.maven.plugin.MojoExecutionException
-
handleWorkingDirectory
private void handleWorkingDirectory() throws org.apache.maven.plugin.MojoExecutionExceptionThis is a convenient method to make the execute method a little bit more readable. It will define the workingDirectory to be the baseDir in case of workingDirectory is null. If the workingDirectory does not exist it will created.- Throws:
org.apache.maven.plugin.MojoExecutionException
-
handleSystemPropertyArguments
private void handleSystemPropertyArguments(java.lang.String argsProp, java.util.List<java.lang.String> commandArguments) throws org.apache.maven.plugin.MojoExecutionException- Throws:
org.apache.maven.plugin.MojoExecutionException
-
handleCommandLineArgs
private void handleCommandLineArgs(java.util.List<java.lang.String> commandArguments) throws org.apache.maven.plugin.MojoExecutionException, java.io.IOException- Throws:
org.apache.maven.plugin.MojoExecutionExceptionjava.io.IOException
-
handleArguments
private void handleArguments(java.util.List<java.lang.String> commandArguments) throws org.apache.maven.plugin.MojoExecutionException, java.io.IOException- Throws:
org.apache.maven.plugin.MojoExecutionExceptionjava.io.IOException
-
fillSuccessCodes
private void fillSuccessCodes(org.apache.commons.exec.Executor exec)
-
isResultCodeAFailure
boolean isResultCodeAFailure(int result)
-
isLongClassPathArgument
private boolean isLongClassPathArgument(java.lang.String arg)
-
isLongModulePathArgument
private boolean isLongModulePathArgument(java.lang.String arg)
-
computeClasspathString
private java.lang.String computeClasspathString(AbstractPath specifiedClasspath)
Compute the classpath from the specified Classpath. The computed classpath is based on the classpathScope. The plugin cannot know from maven the phase it is executed in. So we have to depend on the user to tell us he wants the scope in which the plugin is expected to be executed.- Parameters:
specifiedClasspath- Non null when the user restricted the dependencies,nullotherwise (the default classpath will be used)- Returns:
- a platform specific String representation of the classpath
-
computePath
private java.util.List<java.lang.String> computePath(AbstractPath specifiedClasspath)
Compute the classpath from the specified Classpath. The computed classpath is based on the classpathScope. The plugin cannot know from maven the phase it is executed in. So we have to depend on the user to tell us he wants the scope in which the plugin is expected to be executed.- Parameters:
specifiedClasspath- Non null when the user restricted the dependencies,nullotherwise (the default classpath will be used)- Returns:
- a list of class path elements
-
addToClasspath
private static void addToClasspath(java.lang.StringBuffer theClasspath, java.lang.String toAdd)
-
filterArtifacts
private java.util.List<org.apache.maven.artifact.Artifact> filterArtifacts(java.util.List<org.apache.maven.artifact.Artifact> artifacts, java.util.Collection<java.lang.String> dependencies)
-
getExecutablePath
org.apache.commons.exec.CommandLine getExecutablePath(java.util.Map<java.lang.String,java.lang.String> enviro, java.io.File dir)
-
findExecutable
static java.lang.String findExecutable(java.lang.String executable, java.util.List<java.lang.String> paths)
-
hasNativeExtension
private static boolean hasNativeExtension(java.lang.String exec)
-
hasExecutableExtension
private static boolean hasExecutableExtension(java.lang.String exec)
-
getExecutableExtensions
private static java.util.List<java.lang.String> getExecutableExtensions()
-
getExecutablePaths
private java.util.List<java.lang.String> getExecutablePaths(java.util.Map<java.lang.String,java.lang.String> enviro)
-
executeCommandLine
protected int executeCommandLine(org.apache.commons.exec.Executor exec, org.apache.commons.exec.CommandLine commandLine, java.util.Map<java.lang.String,java.lang.String> enviro, java.io.OutputStream out, java.io.OutputStream err) throws org.apache.commons.exec.ExecuteException, java.io.IOException- Throws:
org.apache.commons.exec.ExecuteExceptionjava.io.IOException
-
executeCommandLine
protected int executeCommandLine(org.apache.commons.exec.Executor exec, org.apache.commons.exec.CommandLine commandLine, java.util.Map<java.lang.String,java.lang.String> enviro, java.io.FileOutputStream outputFile) throws org.apache.commons.exec.ExecuteException, java.io.IOException- Throws:
org.apache.commons.exec.ExecuteExceptionjava.io.IOException
-
executeCommandLine
protected int executeCommandLine(org.apache.commons.exec.Executor exec, org.apache.commons.exec.CommandLine commandLine, java.util.Map<java.lang.String,java.lang.String> enviro, org.apache.commons.exec.PumpStreamHandler psh) throws org.apache.commons.exec.ExecuteException, java.io.IOException- Throws:
org.apache.commons.exec.ExecuteExceptionjava.io.IOException
-
setExecutable
void setExecutable(java.lang.String executable)
-
getExecutable
java.lang.String getExecutable()
-
setWorkingDirectory
void setWorkingDirectory(java.lang.String workingDir)
-
setWorkingDirectory
void setWorkingDirectory(java.io.File workingDir)
-
setArguments
void setArguments(java.util.List<?> arguments)
-
setBasedir
void setBasedir(java.io.File basedir)
-
setProject
void setProject(org.apache.maven.project.MavenProject project)
-
getSystemProperty
protected java.lang.String getSystemProperty(java.lang.String key)
-
setSuccessCodes
public void setSuccessCodes(java.lang.Integer... list)
-
getSuccessCodes
public int[] getSuccessCodes()
-
getToolchain
private org.apache.maven.toolchain.Toolchain getToolchain()
-
createJar
private java.io.File createJar(java.util.List<java.lang.String> classPath, java.lang.String mainClass) throws java.io.IOExceptionCreate a jar with just a manifest containing a Main-Class entry for SurefireBooter and a Class-Path entry for all classpath elements. Copied from surefire (ForkConfiguration#createJar())- Parameters:
classPath- List<String> of all classpath elements.- Returns:
- Throws:
java.io.IOException
-
createArgFile
private void createArgFile(java.lang.String filePath, java.util.List<java.lang.String> lines) throws java.io.IOException- Throws:
java.io.IOException
-
createEnvs
protected java.util.Map<java.lang.String,java.lang.String> createEnvs(java.io.File envScriptFile) throws org.apache.maven.plugin.MojoExecutionException- Throws:
org.apache.maven.plugin.MojoExecutionException
-
createEnvWrapperFile
protected java.io.File createEnvWrapperFile(java.io.File envScript) throws java.io.IOException- Throws:
java.io.IOException
-
getProcessDestroyer
protected org.apache.commons.exec.ProcessDestroyer getProcessDestroyer()
-
-