use of org.apache.maven.shared.invoker.MavenCommandLineBuilder in project maven-plugins by apache.
the class AbstractInvokerMojo method runBuild.
/**
* Runs the specified project.
*
* @param basedir The base directory of the project, must not be <code>null</code>.
* @param pomFile The (already interpolated) POM file, may be <code>null</code> for a POM-less Maven invocation.
* @param settingsFile The (already interpolated) user settings file for the build, may be <code>null</code>. Will
* be merged with the settings file of the invoking Maven process.
* @param invokerProperties The properties to use.
* @return <code>true</code> if the project was launched or <code>false</code> if the selector script indicated that
* the project should be skipped.
* @throws org.apache.maven.plugin.MojoExecutionException If the project could not be launched.
* @throws org.apache.maven.shared.scriptinterpreter.RunFailureException If either a hook script or the build itself
* failed.
*/
private boolean runBuild(File basedir, File pomFile, File settingsFile, File actualJavaHome, InvokerProperties invokerProperties) throws MojoExecutionException, RunFailureException {
if (getLog().isDebugEnabled() && !invokerProperties.getProperties().isEmpty()) {
Properties props = invokerProperties.getProperties();
getLog().debug("Using invoker properties:");
for (String key : new TreeSet<String>(props.stringPropertyNames())) {
String value = props.getProperty(key);
getLog().debug(" " + key + " = " + value);
}
}
List<String> goals = getGoals(basedir);
List<String> profiles = getProfiles(basedir);
Map<String, Object> context = new LinkedHashMap<String, Object>();
FileLogger logger = setupBuildLogFile(basedir);
boolean selectorResult = true;
try {
try {
scriptRunner.run("selector script", basedir, selectorScript, context, logger, BuildJob.Result.SKIPPED, false);
} catch (RunErrorException e) {
selectorResult = false;
throw e;
} catch (RunFailureException e) {
selectorResult = false;
return false;
}
scriptRunner.run("pre-build script", basedir, preBuildHookScript, context, logger, BuildJob.Result.FAILURE_PRE_HOOK, false);
final InvocationRequest request = new DefaultInvocationRequest();
request.setLocalRepositoryDirectory(localRepositoryPath);
request.setBatchMode(true);
request.setShowErrors(showErrors);
request.setDebug(debug);
request.setShowVersion(showVersion);
setupLoggerForBuildJob(logger, request);
if (mavenHome != null) {
invoker.setMavenHome(mavenHome);
// FIXME: Should we really take care of M2_HOME?
request.addShellEnvironment("M2_HOME", mavenHome.getAbsolutePath());
}
if (mavenExecutable != null) {
invoker.setMavenExecutable(new File(mavenExecutable));
}
if (actualJavaHome != null) {
request.setJavaHome(actualJavaHome);
}
if (environmentVariables != null) {
for (Map.Entry<String, String> variable : environmentVariables.entrySet()) {
request.addShellEnvironment(variable.getKey(), variable.getValue());
}
}
for (int invocationIndex = 1; ; invocationIndex++) {
if (invocationIndex > 1 && !invokerProperties.isInvocationDefined(invocationIndex)) {
break;
}
request.setBaseDirectory(basedir);
request.setPomFile(pomFile);
request.setGoals(goals);
request.setProfiles(profiles);
request.setMavenOpts(mavenOpts);
request.setOffline(false);
String customSettingsFile = invokerProperties.getSettingsFile(invocationIndex);
if (customSettingsFile != null) {
File interpolateSettingsFile = interpolateSettings(new File(customSettingsFile));
File mergeSettingsFile = mergeSettings(interpolateSettingsFile);
request.setUserSettingsFile(mergeSettingsFile);
} else {
request.setUserSettingsFile(settingsFile);
}
Properties systemProperties = getSystemProperties(basedir, invokerProperties.getSystemPropertiesFile(invocationIndex));
request.setProperties(systemProperties);
invokerProperties.configureInvocation(request, invocationIndex);
if (getLog().isDebugEnabled()) {
try {
getLog().debug("Using MAVEN_OPTS: " + request.getMavenOpts());
getLog().debug("Executing: " + new MavenCommandLineBuilder().build(request));
} catch (CommandLineConfigurationException e) {
getLog().debug("Failed to display command line: " + e.getMessage());
}
}
InvocationResult result;
try {
result = invoker.execute(request);
} catch (final MavenInvocationException e) {
getLog().debug("Error invoking Maven: " + e.getMessage(), e);
throw new RunFailureException("Maven invocation failed. " + e.getMessage(), BuildJob.Result.FAILURE_BUILD);
}
verify(result, invocationIndex, invokerProperties, logger);
}
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
} finally {
if (selectorResult) {
runPostBuildHook(basedir, context, logger);
}
if (logger != null) {
logger.close();
}
}
return true;
}
use of org.apache.maven.shared.invoker.MavenCommandLineBuilder in project scala-maven-plugin by davidB.
the class ScalaContinuousTestMojo method postCompileActions.
@Override
protected void postCompileActions() throws Exception {
if (test == null) {
getLog().info("Now running all the unit tests. Use -Dtest=FooTest to run a single test by name");
} else {
getLog().info("Now running tests matching: " + test);
}
final InvocationRequest request = new DefaultInvocationRequest();
request.setLocalRepositoryDirectory(localRepositoryPath);
request.setInteractive(false);
request.setErrorHandler(new SystemOutHandler(true));
request.setOutputHandler(new SystemOutHandler(true));
request.setBaseDirectory(project.getBasedir());
request.setPomFile(new File(project.getBasedir(), "pom.xml"));
request.setGoals(getMavenGoals());
request.setOffline(false);
if (test != null) {
Properties properties = new Properties();
properties.put("test", test);
request.setProperties(properties);
}
if (getLog().isDebugEnabled()) {
try {
getLog().debug("Executing: " + new MavenCommandLineBuilder().build(request));
} catch (CommandLineConfigurationException e) {
getLog().debug("Failed to display command line: " + e.getMessage());
}
}
try {
invoker.execute(request);
} catch (final MavenInvocationException e) {
getLog().debug("Error invoking Maven: " + e.getMessage(), e);
throw new BuildFailureException("Maven invocation failed. " + e.getMessage(), e);
}
}
Aggregations