Java Code Examples for org.apache.maven.shared.invoker.InvocationResult#getExecutionException()

The following examples show how to use org.apache.maven.shared.invoker.InvocationResult#getExecutionException() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NativeBuildMojo.java    From client-maven-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void execute() throws MojoExecutionException {
    getLog().debug("Start building");

    // prepare the execution:
    final InvocationRequest invocationRequest = new DefaultInvocationRequest();
    invocationRequest.setProfiles(project.getActiveProfiles().stream()
            .map(Profile::getId)
            .collect(Collectors.toList()));
    invocationRequest.setPomFile(new File(pom));

    invocationRequest.setGoals(Arrays.asList("client:compile", "client:link"));

    final Invoker invoker = new DefaultInvoker();
    // execute:
    try {
        final InvocationResult invocationResult = invoker.execute(invocationRequest);
        if (invocationResult.getExitCode() != 0) {
            throw new MojoExecutionException("Error, client:build failed", invocationResult.getExecutionException());
        }
    } catch (MavenInvocationException e) {
        e.printStackTrace();
        throw new MojoExecutionException("Error", e);
    }

}
 
Example 2
Source File: BuildProject.java    From unleash-maven-plugin with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void execute(ExecutionContext context) throws MojoExecutionException, MojoFailureException {
  this.log.info("Starting release build.");

  try {
    InvocationRequest request = setupInvocationRequest();
    Invoker invoker = setupInvoker();

    InvocationResult result = invoker.execute(request);
    if (result.getExitCode() != 0) {
      CommandLineException executionException = result.getExecutionException();
      if (executionException != null) {
        throw new MojoFailureException("Error during project build: " + executionException.getMessage(),
            executionException);
      } else {
        throw new MojoFailureException("Error during project build: " + result.getExitCode());
      }
    }
  } catch (MavenInvocationException e) {
    throw new MojoFailureException(e.getMessage(), e);
  }
}
 
Example 3
Source File: AbstractSundrioMojo.java    From sundrio with Apache License 2.0 6 votes vote down vote up
void backGroundBuild(MavenProject project) throws MojoExecutionException {
    MavenExecutionRequest executionRequest = session.getRequest();

    InvocationRequest request = new DefaultInvocationRequest();
    request.setBaseDirectory(project.getBasedir());
    request.setPomFile(project.getFile());
    request.setGoals(executionRequest.getGoals());
    request.setRecursive(false);
    request.setInteractive(false);

    request.setProfiles(executionRequest.getActiveProfiles());
    request.setProperties(executionRequest.getUserProperties());
    Invoker invoker = new DefaultInvoker();
    try {
        InvocationResult result = invoker.execute(request);
        if (result.getExitCode() != 0) {
            throw new IllegalStateException("Error invoking Maven goals:[" + StringUtils.join(executionRequest.getGoals(), ", ") + "]", result.getExecutionException());
        }
    } catch (MavenInvocationException e) {
        throw new IllegalStateException("Error invoking Maven goals:[" + StringUtils.join(executionRequest.getGoals(), ", ") + "]", e);
    }
}
 
Example 4
Source File: MavenExecutorImpl.java    From developer-studio with Apache License 2.0 5 votes vote down vote up
public boolean executeMavenGoal(File projectPath, List<String> goals,
		boolean isOffline) {
	InvocationRequest request = new DefaultInvocationRequest();

	if (!projectPath.exists()) {
		projectPath.mkdirs();
	}

	request.setPomFile(new File(projectPath, POM_XML));

	if (goals == null) {
		goals = getDefaultMavenGoals();
	}
	request.setGoals(goals);
	Invoker invoker = new DefaultInvoker();
	request.setOffline(isOffline);

	try {
		InvocationResult result = invoker.execute(request);
		if (result.getExecutionException() == null) {
			if (result.getExitCode() != 0) {
				request.setOffline(!isOffline);
				result = invoker.execute(request);
				if (result.getExitCode() == 0) {
					return true;
				} else {
					final String errorMessage = "No maven Project found at "
							+ projectPath;
					log.error(errorMessage);
					throw new MavenInvocationException(errorMessage);
				}
			}
			return true;
		}
	} catch (MavenInvocationException e) {
		log.error("Maven invocation failed", e);
	}
	return false;
}
 
Example 5
Source File: ArchetypeTest.java    From ipaas-quickstarts with Apache License 2.0 5 votes vote down vote up
protected static int invokeMaven(String[] args, String outDir, File logFile) {
    List<String> goals = Arrays.asList(args);
    String commandLine = Strings.join(goals, " ");

    InvocationResult result = null;
    try {
        File dir = new File(outDir);

        InvocationRequest request = new DefaultInvocationRequest();
        request.setGoals(goals);

        InvocationOutputHandler outputHandler = new SystemOutAndFileHandler(logFile);
        outputHandler.consumeLine("");
        outputHandler.consumeLine("");
        outputHandler.consumeLine(dir.getName() + " : starting: mvn " + commandLine);
        outputHandler.consumeLine("");
        request.setOutputHandler(outputHandler);
        request.setErrorHandler(outputHandler);

        DefaultInvoker invoker = new DefaultInvoker();
        request.setPomFile(new File(dir, "pom.xml"));
        result = invoker.execute(request);
        CommandLineException executionException = result.getExecutionException();
        if (executionException != null) {
            LOG.error("Failed to invoke maven with: mvn " + commandLine + ". " + executionException, executionException);
        }
    } catch (Exception e) {
        LOG.error("Failed to invoke maven with: mvn " + commandLine + ". " + e, e);
    }
    return result == null ? 1 : result.getExitCode();
}
 
Example 6
Source File: MavenJarResolver.java    From beakerx with Apache License 2.0 5 votes vote down vote up
private AddMvnCommandResult getResult(InvocationResult invocationResult, String dependencies) {
  if (invocationResult.getExitCode() != 0) {
    if (invocationResult.getExecutionException() != null) {
      return AddMvnCommandResult.error(invocationResult.getExecutionException().getMessage());
    }
    StringBuilder errorMsgBuilder = new StringBuilder("Could not resolve dependencies for:");
    errorMsgBuilder
            .append("\n")
            .append(dependencies);
    return AddMvnCommandResult.error(errorMsgBuilder.toString());
  }

  return AddMvnCommandResult.success(transformFromMavenRepoToKernelRepo(mavenBuildClasspath(), jarsFromRepo()));
}
 
Example 7
Source File: IntegrationTestMojo.java    From helidon-build-tools with Apache License 2.0 4 votes vote down vote up
private void invokePostArchetypeGenerationGoals(List<String> goals, File basedir)
        throws IOException, MojoExecutionException {

    FileLogger logger = setupLogger(basedir);

    if (!goals.isEmpty()) {
        getLog().info("Invoking post-archetype-generation goals: " + goals);
        InvocationRequest request = new DefaultInvocationRequest()
                .setBaseDirectory(basedir)
                .setGoals(goals)
                .setBatchMode(true)
                .setShowErrors(true)
                .setDebug(debug)
                .setShowVersion(showVersion);

        if (logger != null) {
            request.setErrorHandler(logger);
            request.setOutputHandler(logger);
        }

        if (!properties.isEmpty()) {
            Properties props = new Properties();
            for (Map.Entry<String, String> entry : properties.entrySet()) {
                if (entry.getValue() != null) {
                    props.setProperty(entry.getKey(), entry.getValue());
                }
            }
            request.setProperties(props);
        }

        try {
            InvocationResult result = invoker.execute(request);
            getLog().info("Post-archetype-generation invoker exit code: " + result.getExitCode());
            if (result.getExitCode() != 0) {
                throw new MojoExecutionException("Execution failure: exit code = " + result.getExitCode(),
                        result.getExecutionException());
            }
        } catch (MavenInvocationException ex) {
            throw new MojoExecutionException(ex.getMessage(), ex);
        }
    } else {
        getLog().info("No post-archetype-generation goals to invoke.");
    }
}
 
Example 8
Source File: InvokerMojo.java    From iterator-maven-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * Possibly to multithreading..
 * 
 * @throws MojoFailureException in case of failure.
 */
// private int numberOfThreads;

public void execute()
    throws MojoExecutionException, MojoFailureException
{
    if ( isSkip() )
    {
        getLog().info( "Skip by user request." );
        return;
    }

    if ( isNoneSet() )
    {
        throw new MojoExecutionException( "You have to use at least one. " + "Either items, "
            + "itemsWithProperties, content or folder element!" );
    }

    if ( isMoreThanOneSet() )
    {
        throw new MojoExecutionException( "You can use only one element. "
            + "Either items, itemsWithProperties, content or folder element but not more than one of them." );
    }

    File localRepository = new File( getMavenSession().getSettings().getLocalRepository() );

    invoker.setLocalRepositoryDirectory( localRepository );
    // invoker.setOutputHandler(outputHandler);
    // TODO: Check how it looks if we will use the invokerLogger?
    // invoker.setLogger();

    // getLog().info("local repository: " + localRepository);
    // // getLog().isDebugEnabled()
    // getLog().info("Invoker:" + invoker);

    List<Exception> exceptions = new ArrayList<>();

    for ( ItemWithProperties item : getItemsConverted() )
    {
        try
        {
            createLogOutput( item );
            InvocationResult result = mavenCall( item );

            if ( result.getExitCode() == 0 )
            {
                getLog().info( "------ Maven call was Ok." );
                continue;
            }

            getLog().error( "------ Maven call was NOT Ok. for iteration " + item.getName() + " ( return code: " + result.getExitCode() + " )" );
            if ( result.getExecutionException() != null )
            {
                getLog().error( result.getExecutionException().getMessage(),
                                result.getExecutionException().getCause() );
            }

            String failureMessage =
                "Maven call failed with return code " + result.getExitCode() + " for iteration: " + item.getName();

            if ( isFailAtEnd() )
            {
                exceptions.add( new RuntimeException( failureMessage ) );
            }
            else
            {
                throw new MojoFailureException( failureMessage );
            }

        }
        catch ( MavenInvocationException e )
        {
            // This will stop any iteration.
            getLog().error( "------ ***** Command line options are wrong:", e );
            throw new MojoExecutionException( "Command line options are wrong:", e );
        }
    }

    if ( !exceptions.isEmpty() )
    {
        for ( Exception exception : exceptions )
        {
            getLog().error( exception );
        }
        throw new MojoExecutionException( "Failures during iterations." );
    }
}