use of org.apache.maven.shared.invoker.DefaultInvocationRequest in project maven-plugins by apache.
the class JavadocUtil method invokeMaven.
/**
* Invoke Maven for the given project file with a list of goals and properties, the output will be in the
* invokerlog file.
* <br/>
* <b>Note</b>: the Maven Home should be defined in the <code>maven.home</code> Java system property or defined in
* <code>M2_HOME</code> system env variables.
*
* @param log a logger could be null.
* @param localRepositoryDir the localRepository not null.
* @param projectFile a not null project file.
* @param goals a not null goals list.
* @param properties the properties for the goals, could be null.
* @param invokerLog the log file where the invoker will be written, if null using <code>System.out</code>.
* @throws MavenInvocationException if any
* @since 2.6
*/
protected static void invokeMaven(Log log, File localRepositoryDir, File projectFile, List<String> goals, Properties properties, File invokerLog) throws MavenInvocationException {
if (projectFile == null) {
throw new IllegalArgumentException("projectFile should be not null.");
}
if (!projectFile.isFile()) {
throw new IllegalArgumentException(projectFile.getAbsolutePath() + " is not a file.");
}
if (goals == null || goals.size() == 0) {
throw new IllegalArgumentException("goals should be not empty.");
}
if (localRepositoryDir == null || !localRepositoryDir.isDirectory()) {
throw new IllegalArgumentException("localRepositoryDir '" + localRepositoryDir + "' should be a directory.");
}
String mavenHome = getMavenHome(log);
if (StringUtils.isEmpty(mavenHome)) {
String msg = "Could NOT invoke Maven because no Maven Home is defined. You need to have set the M2_HOME " + "system env variable or a maven.home Java system properties.";
if (log != null) {
log.error(msg);
} else {
System.err.println(msg);
}
return;
}
Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(new File(mavenHome));
invoker.setLocalRepositoryDirectory(localRepositoryDir);
InvocationRequest request = new DefaultInvocationRequest();
request.setBaseDirectory(projectFile.getParentFile());
request.setPomFile(projectFile);
if (log != null) {
request.setDebug(log.isDebugEnabled());
} else {
request.setDebug(true);
}
request.setGoals(goals);
if (properties != null) {
request.setProperties(properties);
}
File javaHome = getJavaHome(log);
if (javaHome != null) {
request.setJavaHome(javaHome);
}
if (log != null && log.isDebugEnabled()) {
log.debug("Invoking Maven for the goals: " + goals + " with " + (properties == null ? "no properties" : "properties=" + properties));
}
InvocationResult result = invoke(log, invoker, request, invokerLog, goals, properties, null);
if (result.getExitCode() != 0) {
String invokerLogContent = readFile(invokerLog, "UTF-8");
// see DefaultMaven
if (invokerLogContent != null && (!invokerLogContent.contains("Scanning for projects...") || invokerLogContent.contains(OutOfMemoryError.class.getName()))) {
if (log != null) {
log.error("Error occurred during initialization of VM, trying to use an empty MAVEN_OPTS...");
if (log.isDebugEnabled()) {
log.debug("Reinvoking Maven for the goals: " + goals + " with an empty MAVEN_OPTS...");
}
}
result = invoke(log, invoker, request, invokerLog, goals, properties, "");
}
}
if (result.getExitCode() != 0) {
String invokerLogContent = readFile(invokerLog, "UTF-8");
// see DefaultMaven
if (invokerLogContent != null && (!invokerLogContent.contains("Scanning for projects...") || invokerLogContent.contains(OutOfMemoryError.class.getName()))) {
throw new MavenInvocationException(ERROR_INIT_VM);
}
throw new MavenInvocationException("Error when invoking Maven, consult the invoker log file: " + invokerLog.getAbsolutePath());
}
}
use of org.apache.maven.shared.invoker.DefaultInvocationRequest in project maven-plugins by apache.
the class InvokerPropertiesTest method testConfigureRequestProject.
public void testConfigureRequestProject() throws Exception {
Properties props = new Properties();
InvokerProperties facade = new InvokerProperties(props);
InvocationRequest request = new DefaultInvocationRequest();
File tempPom = File.createTempFile("maven-invoker-plugin-test", ".pom");
File tempDir = tempPom.getParentFile();
request.setBaseDirectory(tempDir);
facade.configureInvocation(request, 0);
assertEquals(tempDir, request.getBaseDirectory());
assertEquals(null, request.getPomFile());
props.setProperty("invoker.project", tempPom.getName());
request.setBaseDirectory(tempDir);
facade.configureInvocation(request, 0);
assertEquals(tempDir, request.getBaseDirectory());
assertEquals(tempPom, request.getPomFile());
props.setProperty("invoker.project", "");
request.setBaseDirectory(tempDir);
facade.configureInvocation(request, 0);
assertEquals(tempDir, request.getBaseDirectory());
assertEquals(null, request.getPomFile());
tempPom.delete();
}
use of org.apache.maven.shared.invoker.DefaultInvocationRequest in project maven-plugins by apache.
the class InvokerPropertiesTest method testConfigureRequestRecursion.
public void testConfigureRequestRecursion() throws Exception {
Properties props = new Properties();
InvokerProperties facade = new InvokerProperties(props);
InvocationRequest request = new DefaultInvocationRequest();
request.setRecursive(true);
facade.configureInvocation(request, 0);
assertTrue(request.isRecursive());
request.setRecursive(false);
facade.configureInvocation(request, 0);
assertFalse(request.isRecursive());
props.setProperty("invoker.nonRecursive", "true");
facade.configureInvocation(request, 0);
assertFalse(request.isRecursive());
props.setProperty("invoker.nonRecursive", "false");
facade.configureInvocation(request, 0);
assertTrue(request.isRecursive());
}
use of org.apache.maven.shared.invoker.DefaultInvocationRequest in project maven-plugins by apache.
the class InvokerPropertiesTest method testConfigureRequestProfiles.
public void testConfigureRequestProfiles() throws Exception {
Properties props = new Properties();
InvokerProperties facade = new InvokerProperties(props);
InvocationRequest request = new DefaultInvocationRequest();
request.setProfiles(Collections.singletonList("test"));
facade.configureInvocation(request, 0);
assertEquals(Collections.singletonList("test"), request.getProfiles());
props.setProperty("invoker.profiles", "verify");
facade.configureInvocation(request, 0);
assertEquals(Collections.singletonList("verify"), request.getProfiles());
props.setProperty("invoker.profiles", " ");
facade.configureInvocation(request, 0);
assertEquals(Arrays.asList(new String[0]), request.getProfiles());
props.setProperty("invoker.profiles", " clean , test verify ,");
facade.configureInvocation(request, 0);
assertEquals(Arrays.asList(new String[] { "clean", "test", "verify" }), request.getProfiles());
props.setProperty("invoker.profiles", "");
facade.configureInvocation(request, 0);
assertEquals(Arrays.asList(new String[0]), request.getProfiles());
}
use of org.apache.maven.shared.invoker.DefaultInvocationRequest in project maven-plugins by apache.
the class InvokerPropertiesTest method testConfigureRequestFailureBehavior.
public void testConfigureRequestFailureBehavior() throws Exception {
Properties props = new Properties();
InvokerProperties facade = new InvokerProperties(props);
InvocationRequest request = new DefaultInvocationRequest();
request.setReactorFailureBehavior(InvocationRequest.ReactorFailureBehavior.FailAtEnd);
facade.configureInvocation(request, 0);
assertEquals(InvocationRequest.ReactorFailureBehavior.FailAtEnd, request.getReactorFailureBehavior());
props.setProperty("invoker.failureBehavior", InvocationRequest.ReactorFailureBehavior.FailNever.getLongOption());
facade.configureInvocation(request, 0);
assertEquals("fail-never", request.getReactorFailureBehavior().getLongOption());
}
Aggregations