org.codehaus.mojo.exec
Class ExecJavaMojo

java.lang.Object
  extended by org.apache.maven.plugin.AbstractMojo
      extended by org.codehaus.mojo.exec.AbstractExecMojo
          extended by org.codehaus.mojo.exec.ExecJavaMojo
All Implemented Interfaces:
org.apache.maven.plugin.ContextEnabled, org.apache.maven.plugin.Mojo

public class ExecJavaMojo
extends AbstractExecMojo

Executes the supplied java class in the current VM with the enclosing project's dependencies as classpath.

Since:
1.0
Author:
Kaare Nilsen, David Smiley

Nested Class Summary
(package private)  class ExecJavaMojo.IsolatedThreadGroup
          a ThreadGroup to isolate execution and collect exceptions.
 
Field Summary
private  String[] arguments
          The class arguments.
private  org.apache.maven.artifact.factory.ArtifactFactory artifactFactory
           
private  org.apache.maven.artifact.resolver.ArtifactResolver artifactResolver
           
private  boolean cleanupDaemonThreads
          Wether to interrupt/join and possibly stop the daemon threads upon quitting.
private  long daemonThreadJoinTimeout
          This defines the number of milliseconds to wait for daemon threads to quit following their interruption.
This is only taken into account if cleanupDaemonThreads is true.
private  ExecutableDependency executableDependency
          If provided the ExecutableDependency identifies which of the plugin dependencies contains the executable class.
private  boolean includePluginDependencies
          Indicates if this plugin's dependencies should be used when executing the main class.
private  boolean includeProjectDependencies
          Indicates if the project dependencies should be used when executing the main class.
private  boolean keepAlive
          Deprecated. since 1.1-alpha-1
private  long killAfter
          Deprecated. since 1.1-alpha-1
private  org.apache.maven.artifact.repository.ArtifactRepository localRepository
           
private  String mainClass
          The main class to execute.
private  org.apache.maven.artifact.metadata.ArtifactMetadataSource metadataSource
           
private  Properties originalSystemProperties
           
private  List pluginDependencies
           
private  org.apache.maven.project.MavenProjectBuilder projectBuilder
           
private  List remoteRepositories
           
private  boolean stopUnresponsiveDaemonThreads
          Wether to call Thread.stop() following a timing out of waiting for an interrupted thread to finish.
private  Property[] systemProperties
          A list of system properties to be passed.
 
Fields inherited from class org.codehaus.mojo.exec.AbstractExecMojo
classpathScope, project
 
Fields inherited from interface org.apache.maven.plugin.Mojo
ROLE
 
Constructor Summary
ExecJavaMojo()
           
 
Method Summary
private  void addRelevantPluginDependenciesToClasspath(List path)
          Add any relevant project dependencies to the classpath.
private  void addRelevantProjectDependenciesToClasspath(List path)
          Add any relevant project dependencies to the classpath.
private  Set determineRelevantPluginDependencies()
          Determine all plugin dependencies relevant to the executable.
 void execute()
          Execute goal.
private  org.apache.maven.artifact.Artifact findExecutableArtifact()
          Examine the plugin dependencies to find the executable artifact.
private  Collection getActiveThreads(ThreadGroup threadGroup)
           
private  ClassLoader getClassLoader()
          Set up a classloader for the execution of the main class.
private  org.apache.maven.artifact.Artifact getExecutablePomArtifact(org.apache.maven.artifact.Artifact executableArtifact)
          Get the artifact which refers to the POM of the executable artifact.
private  void joinNonDaemonThreads(ThreadGroup threadGroup)
           
private  void joinThread(Thread thread, long timeoutMsecs)
           
private  Set resolveExecutableDependencies(org.apache.maven.artifact.Artifact executablePomArtifact)
          Resolve the executable dependencies for the specified project
private  void setSystemProperties()
          Pass any given system properties to the java system properties.
private  void terminateThreads(ThreadGroup threadGroup)
           
private  void waitFor(long millis)
          Stop program execution for nn millis.
 
Methods inherited from class org.codehaus.mojo.exec.AbstractExecMojo
collectProjectArtifactsAndClasspath, hasCommandlineArgs, isSkip, parseCommandlineArgs, registerSourceRoots
 
Methods inherited from class org.apache.maven.plugin.AbstractMojo
getLog, getPluginContext, setLog, setPluginContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

artifactResolver

private org.apache.maven.artifact.resolver.ArtifactResolver artifactResolver

artifactFactory

private org.apache.maven.artifact.factory.ArtifactFactory artifactFactory

metadataSource

private org.apache.maven.artifact.metadata.ArtifactMetadataSource metadataSource

localRepository

private org.apache.maven.artifact.repository.ArtifactRepository localRepository
Since:
1.0

remoteRepositories

private List remoteRepositories
Since:
1.1-beta-1

projectBuilder

private org.apache.maven.project.MavenProjectBuilder projectBuilder
Since:
1.0

pluginDependencies

private List pluginDependencies
Since:
1.1-beta-1

mainClass

private String mainClass
The main class to execute.

Since:
1.0

arguments

private String[] arguments
The class arguments.

Since:
1.0

systemProperties

private Property[] systemProperties
A list of system properties to be passed. Note: as the execution is not forked, some system properties required by the JVM cannot be passed here. Use MAVEN_OPTS or the exec:exec instead. See the user guide for more information.

Since:
1.0

keepAlive

private boolean keepAlive
Deprecated. since 1.1-alpha-1
Indicates if mojo should be kept running after the mainclass terminates. Usefull for serverlike apps with deamonthreads.

Since:
1.0

includeProjectDependencies

private boolean includeProjectDependencies
Indicates if the project dependencies should be used when executing the main class.

Since:
1.1-beta-1

includePluginDependencies

private boolean includePluginDependencies
Indicates if this plugin's dependencies should be used when executing the main class.

This is useful when project dependencies are not appropriate. Using only the plugin dependencies can be particularly useful when the project is not a java project. For example a mvn project using the csharp plugins only expects to see dotnet libraries as dependencies.

Since:
1.1-beta-1

executableDependency

private ExecutableDependency executableDependency
If provided the ExecutableDependency identifies which of the plugin dependencies contains the executable class. This will have the affect of only including plugin dependencies required by the identified ExecutableDependency.

If includeProjectDependencies is set to true, all of the project dependencies will be included on the executable's classpath. Whether a particular project dependency is a dependency of the identified ExecutableDependency will be irrelevant to its inclusion in the classpath.

Since:
1.1-beta-1

cleanupDaemonThreads

private boolean cleanupDaemonThreads
Wether to interrupt/join and possibly stop the daemon threads upon quitting.
If this is false, maven does nothing about the daemon threads. When maven has no more work to do, the VM will normally terminate any remaining daemon threads.

In certain cases (in particular if maven is embedded), you might need to keep this enabled to make sure threads are properly cleaned up to ensure they don't interfere with subsequent activity. In that case, see daemonThreadJoinTimeout and stopUnresponsiveDaemonThreads for further tuning.

Since:
1.1-beta-1

daemonThreadJoinTimeout

private long daemonThreadJoinTimeout
This defines the number of milliseconds to wait for daemon threads to quit following their interruption.
This is only taken into account if cleanupDaemonThreads is true. A value <=0 means to not timeout (i.e. wait indefinitely for threads to finish). Following a timeout, a warning will be logged.

Note: properly coded threads should terminate upon interruption but some threads may prove problematic: as the VM does interrupt daemon threads, some code may not have been written to handle interruption properly. For example java.util.Timer is known to not handle interruptions in JDK <= 1.6. So it is not possible for us to infinitely wait by default otherwise maven could hang. A sensible default value has been chosen, but this default value may change in the future based on user feedback.

Since:
1.1-beta-1

stopUnresponsiveDaemonThreads

private boolean stopUnresponsiveDaemonThreads
Wether to call Thread.stop() following a timing out of waiting for an interrupted thread to finish. This is only taken into account if cleanupDaemonThreads is true and the daemonThreadJoinTimeout threshold has been reached for an uncooperative thread. If this is false, or if Thread.stop() fails to get the thread to stop, then a warning is logged and Maven will continue on while the affected threads (and related objects in memory) linger on. Consider setting this to true if you are invoking problematic code that you can't fix. An example is Timer which doesn't respond to interruption. To have Timer fixed, vote for this bug.

Since:
1.1-beta-1

killAfter

private long killAfter
Deprecated. since 1.1-alpha-1
Deprecated this is not needed anymore.

Since:
1.0

originalSystemProperties

private Properties originalSystemProperties
Constructor Detail

ExecJavaMojo

public ExecJavaMojo()
Method Detail

execute

public void execute()
             throws org.apache.maven.plugin.MojoExecutionException,
                    org.apache.maven.plugin.MojoFailureException
Execute goal.

Throws:
org.apache.maven.plugin.MojoExecutionException - execution of the main class or one of the threads it generated failed.
org.apache.maven.plugin.MojoFailureException - something bad happened...

joinNonDaemonThreads

private void joinNonDaemonThreads(ThreadGroup threadGroup)

joinThread

private void joinThread(Thread thread,
                        long timeoutMsecs)

terminateThreads

private void terminateThreads(ThreadGroup threadGroup)

getActiveThreads

private Collection getActiveThreads(ThreadGroup threadGroup)

setSystemProperties

private void setSystemProperties()
Pass any given system properties to the java system properties.


getClassLoader

private ClassLoader getClassLoader()
                            throws org.apache.maven.plugin.MojoExecutionException
Set up a classloader for the execution of the main class.

Returns:
the classloader
Throws:
org.apache.maven.plugin.MojoExecutionException - if a problem happens

addRelevantPluginDependenciesToClasspath

private void addRelevantPluginDependenciesToClasspath(List path)
                                               throws org.apache.maven.plugin.MojoExecutionException
Add any relevant project dependencies to the classpath. Indirectly takes includePluginDependencies and ExecutableDependency into consideration.

Parameters:
path - classpath of URL objects
Throws:
org.apache.maven.plugin.MojoExecutionException - if a problem happens

addRelevantProjectDependenciesToClasspath

private void addRelevantProjectDependenciesToClasspath(List path)
                                                throws org.apache.maven.plugin.MojoExecutionException
Add any relevant project dependencies to the classpath. Takes includeProjectDependencies into consideration.

Parameters:
path - classpath of URL objects
Throws:
org.apache.maven.plugin.MojoExecutionException - if a problem happens

determineRelevantPluginDependencies

private Set determineRelevantPluginDependencies()
                                         throws org.apache.maven.plugin.MojoExecutionException
Determine all plugin dependencies relevant to the executable. Takes includePlugins, and the executableDependency into consideration.

Returns:
a set of Artifact objects. (Empty set is returned if there are no relevant plugin dependencies.)
Throws:
org.apache.maven.plugin.MojoExecutionException - if a problem happens resolving the plufin dependencies

getExecutablePomArtifact

private org.apache.maven.artifact.Artifact getExecutablePomArtifact(org.apache.maven.artifact.Artifact executableArtifact)
Get the artifact which refers to the POM of the executable artifact.

Parameters:
executableArtifact - this artifact refers to the actual assembly.
Returns:
an artifact which refers to the POM of the executable artifact.

findExecutableArtifact

private org.apache.maven.artifact.Artifact findExecutableArtifact()
                                                           throws org.apache.maven.plugin.MojoExecutionException
Examine the plugin dependencies to find the executable artifact.

Returns:
an artifact which refers to the actual executable tool (not a POM)
Throws:
org.apache.maven.plugin.MojoExecutionException - if no executable artifact was found

resolveExecutableDependencies

private Set resolveExecutableDependencies(org.apache.maven.artifact.Artifact executablePomArtifact)
                                   throws org.apache.maven.plugin.MojoExecutionException
Resolve the executable dependencies for the specified project

Parameters:
executablePomArtifact - the project's POM
Returns:
a set of Artifacts
Throws:
org.apache.maven.plugin.MojoExecutionException - if a failure happens

waitFor

private void waitFor(long millis)
Stop program execution for nn millis.

Parameters:
millis - the number of millis-seconds to wait for, 0 stops program forever.


Copyright © 2004-2011 The Kuali Foundation. All Rights Reserved.