
scala_maven.ScalaRunMojo Maven / Gradle / Ivy
/*
* This is free and unencumbered software released into the public domain.
* See UNLICENSE.
*/
package scala_maven;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.toolchain.Toolchain;
import org.codehaus.plexus.util.StringUtils;
import scala_maven_executions.JavaMainCaller;
import scala_maven_executions.JavaMainCallerByFork;
import util.FileUtils;
/** Run a Scala class using the Scala runtime */
@Mojo(name = "run", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true)
@Execute(phase = LifecyclePhase.TEST_COMPILE)
public class ScalaRunMojo extends ScalaMojoSupport {
/** The class to use when launching a scala program */
@Parameter(property = "launcher")
private String launcher;
/**
* Additional parameter to use to call the main class Using this parameter only from command line
* ("-DaddArgs=arg1|arg2|arg3|..."), not from pom.xml.
*/
@Parameter(property = "addArgs")
private String addArgs;
/**
* A list of launcher definition (to avoid rewriting long command line or share way to call an
* application) launchers could be define by :
*
*
* <launchers>
* <launcher>
* <id>myLauncher</id>
* <mainClass>my.project.Main</mainClass>
* <args>
* <arg>arg1</arg>
* </args>
* <jvmArgs>
* <jvmArg>-Xmx64m</jvmArg>
* </jvmArgs>
* </launcher>
* <launcher>
* <id>myLauncher2</id>
* ...
* <><>
* </launcher>
* </launchers>
*
*/
@Parameter private Launcher[] launchers;
/**
* Main class to call, the call use the jvmArgs and args define in the pom.xml, and the addArgs
* define in the command line if define.
*
* Higher priority to launcher parameter) Using this parameter only from command line
* (-DmainClass=...), not from pom.xml.
*/
@Parameter(property = "mainClass")
private String mainClass;
@Override
protected void doExecute() throws Exception {
JavaMainCaller jcmd = null;
Toolchain toolchain = toolchainManager.getToolchainFromBuildContext("jdk", session);
if (StringUtils.isNotEmpty(mainClass)) {
jcmd =
new JavaMainCallerByFork(
this,
mainClass,
FileUtils.toMultiPath(FileUtils.fromStrings(project.getTestClasspathElements())),
jvmArgs,
args,
forceUseArgFile,
toolchain);
} else if ((launchers != null) && (launchers.length > 0)) {
if (StringUtils.isNotEmpty(launcher)) {
for (int i = 0; (i < launchers.length) && (jcmd == null); i++) {
if (launcher.equals(launchers[i].id)) {
getLog()
.info("launcher '" + launchers[i].id + "' selected => " + launchers[i].mainClass);
jcmd =
new JavaMainCallerByFork(
this,
launchers[i].mainClass,
FileUtils.toMultiPath(
FileUtils.fromStrings(project.getTestClasspathElements())),
launchers[i].jvmArgs,
launchers[i].args,
forceUseArgFile,
toolchain);
}
}
} else {
getLog().info("launcher '" + launchers[0].id + "' selected => " + launchers[0].mainClass);
jcmd =
new JavaMainCallerByFork(
this,
launchers[0].mainClass,
FileUtils.toMultiPath(FileUtils.fromStrings(project.getTestClasspathElements())),
launchers[0].jvmArgs,
launchers[0].args,
forceUseArgFile,
toolchain);
}
}
if (jcmd != null) {
if (StringUtils.isNotEmpty(addArgs)) {
jcmd.addArgs(StringUtils.split(addArgs, "|"));
}
jcmd.run(displayCmd);
} else {
getLog().warn("Not mainClass or valid launcher found/define");
}
}
}