Search in sources :

Example 6 with InvocationRequest

use of org.apache.maven.shared.invoker.InvocationRequest 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());
    }
}
Also used : Invoker(org.apache.maven.shared.invoker.Invoker) DefaultInvoker(org.apache.maven.shared.invoker.DefaultInvoker) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) MavenInvocationException(org.apache.maven.shared.invoker.MavenInvocationException) DefaultInvoker(org.apache.maven.shared.invoker.DefaultInvoker) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) File(java.io.File) InvocationResult(org.apache.maven.shared.invoker.InvocationResult)

Example 7 with InvocationRequest

use of org.apache.maven.shared.invoker.InvocationRequest 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();
}
Also used : InvokerProperties(org.apache.maven.plugins.invoker.InvokerProperties) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvokerProperties(org.apache.maven.plugins.invoker.InvokerProperties) Properties(java.util.Properties) File(java.io.File)

Example 8 with InvocationRequest

use of org.apache.maven.shared.invoker.InvocationRequest 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());
}
Also used : InvokerProperties(org.apache.maven.plugins.invoker.InvokerProperties) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvokerProperties(org.apache.maven.plugins.invoker.InvokerProperties) Properties(java.util.Properties)

Example 9 with InvocationRequest

use of org.apache.maven.shared.invoker.InvocationRequest 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());
}
Also used : InvokerProperties(org.apache.maven.plugins.invoker.InvokerProperties) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvokerProperties(org.apache.maven.plugins.invoker.InvokerProperties) Properties(java.util.Properties)

Example 10 with InvocationRequest

use of org.apache.maven.shared.invoker.InvocationRequest 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());
}
Also used : InvokerProperties(org.apache.maven.plugins.invoker.InvokerProperties) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvokerProperties(org.apache.maven.plugins.invoker.InvokerProperties) Properties(java.util.Properties)

Aggregations

DefaultInvocationRequest (org.apache.maven.shared.invoker.DefaultInvocationRequest)12 InvocationRequest (org.apache.maven.shared.invoker.InvocationRequest)12 Properties (java.util.Properties)8 InvokerProperties (org.apache.maven.plugins.invoker.InvokerProperties)7 File (java.io.File)5 InvocationResult (org.apache.maven.shared.invoker.InvocationResult)5 MavenInvocationException (org.apache.maven.shared.invoker.MavenInvocationException)4 DefaultInvoker (org.apache.maven.shared.invoker.DefaultInvoker)3 IOException (java.io.IOException)2 Invoker (org.apache.maven.shared.invoker.Invoker)2 Reader (java.io.Reader)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 CommandLineConfigurationException (org.apache.maven.shared.invoker.CommandLineConfigurationException)1 MavenCommandLineBuilder (org.apache.maven.shared.invoker.MavenCommandLineBuilder)1 RunErrorException (org.apache.maven.shared.scriptinterpreter.RunErrorException)1 RunFailureException (org.apache.maven.shared.scriptinterpreter.RunFailureException)1