Search in sources :

Example 1 with MavenInvocationException

use of org.apache.maven.shared.invoker.MavenInvocationException in project fabric8 by jboss-fuse.

the class CreateProfileZipMojo method generateAggregatedZip.

protected void generateAggregatedZip(MavenProject rootProject, List<MavenProject> reactorProjects, List<MavenProject> pomZipProjects) throws IOException, MojoExecutionException {
    File projectBaseDir = rootProject.getBasedir();
    String rootProjectGroupId = rootProject.getGroupId();
    String rootProjectArtifactId = rootProject.getArtifactId();
    String rootProjectVersion = rootProject.getVersion();
    File projectOutputFile = new File(projectBaseDir, "target/profile.zip");
    getLog().info("Generating " + projectOutputFile.getAbsolutePath() + " from root project " + rootProject.getArtifactId());
    File projectBuildDir = new File(projectBaseDir, reactorProjectOutputPath);
    createAggregatedZip(reactorProjects, projectBaseDir, projectBuildDir, reactorProjectOutputPath, projectOutputFile, includeReadMe, pomZipProjects);
    if (rootProject.getAttachedArtifacts() != null) {
        // need to remove existing as otherwise we get a WARN
        Artifact found = null;
        for (Artifact artifact : rootProject.getAttachedArtifacts()) {
            if (artifactClassifier != null && artifact.hasClassifier() && artifact.getClassifier().equals(artifactClassifier)) {
                found = artifact;
                break;
            }
        }
        if (found != null) {
            rootProject.getAttachedArtifacts().remove(found);
        }
    }
    getLog().info("Attaching aggregated zip " + projectOutputFile + " to root project " + rootProject.getArtifactId());
    projectHelper.attachArtifact(rootProject, artifactType, artifactClassifier, projectOutputFile);
    List<String> activeProfileIds = new ArrayList<>();
    List<org.apache.maven.model.Profile> activeProfiles = rootProject.getActiveProfiles();
    if (activeProfiles != null) {
        for (org.apache.maven.model.Profile profile : activeProfiles) {
            String id = profile.getId();
            if (Strings.isNotBlank(id)) {
                activeProfileIds.add(id);
            }
        }
    }
    // so we need to install manually
    if (rootProject.hasLifecyclePhase("install")) {
        getLog().info("Installing aggregated zip " + projectOutputFile);
        InvocationRequest request = new DefaultInvocationRequest();
        request.setBaseDirectory(rootProject.getBasedir());
        request.setPomFile(new File("./pom.xml"));
        request.setGoals(Collections.singletonList("install:install-file"));
        request.setRecursive(false);
        request.setInteractive(false);
        Properties props = new Properties();
        props.setProperty("file", "target/profile.zip");
        props.setProperty("groupId", rootProjectGroupId);
        props.setProperty("artifactId", rootProjectArtifactId);
        props.setProperty("version", rootProjectVersion);
        props.setProperty("classifier", "profile");
        props.setProperty("packaging", "zip");
        request.setProperties(props);
        getLog().info("Installing aggregated zip using: mvn install:install-file" + serializeMvnProperties(props));
        Invoker invoker = new DefaultInvoker();
        try {
            InvocationResult result = invoker.execute(request);
            if (result.getExitCode() != 0) {
                throw new IllegalStateException("Error invoking Maven goal install:install-file");
            }
        } catch (MavenInvocationException e) {
            throw new MojoExecutionException("Error invoking Maven goal install:install-file", e);
        }
    }
    if (rootProject.hasLifecyclePhase("deploy")) {
        getLog().info("Deploying aggregated zip " + projectOutputFile + " to root project " + rootProject.getArtifactId());
        InvocationRequest request = new DefaultInvocationRequest();
        request.setBaseDirectory(rootProject.getBasedir());
        request.setPomFile(new File("./pom.xml"));
        request.setGoals(Collections.singletonList(deployFileGoal));
        request.setRecursive(false);
        request.setInteractive(false);
        request.setProfiles(activeProfileIds);
        request.setShellEnvironmentInherited(true);
        request.setLocalRepositoryDirectory(session.getRequest().getLocalRepositoryPath());
        request.setUserSettingsFile(session.getRequest().getUserSettingsFile());
        getLog().info("Using deploy goal: " + deployFileGoal + " with active profiles: " + activeProfileIds + " and local repo " + session.getRequest().getLocalRepositoryPath() + " and user settings " + session.getRequest().getUserSettingsFile());
        Properties props = new Properties();
        props.setProperty("file", "target/profile.zip");
        props.setProperty("groupId", rootProjectGroupId);
        props.setProperty("artifactId", rootProjectArtifactId);
        props.setProperty("version", rootProjectVersion);
        props.setProperty("classifier", "profile");
        props.setProperty("packaging", "zip");
        if (altDeploymentRepository == null || altDeploymentRepository.length() <= 0) {
            props.setProperty("url", deploymentRepository.getUrl());
            props.setProperty("repositoryId", deploymentRepository.getId());
        } else {
            Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher(altDeploymentRepository);
            if (!matcher.matches()) {
                throw new MojoExecutionException("Invalid syntax for altDeploymentRepository");
            } else {
                String id = matcher.group(1).trim();
                String url = matcher.group(3).trim();
                props.setProperty("url", url);
                props.setProperty("repositoryId", id);
            }
        }
        props.setProperty("generatePom", "false");
        request.setProperties(props);
        getLog().info("Deploying aggregated zip using: mvn deploy:deploy-file" + serializeMvnProperties(props));
        Invoker invoker = new DefaultInvoker();
        try {
            InvocationResult result = invoker.execute(request);
            if (result.getExitCode() != 0) {
                throw new IllegalStateException("Error invoking Maven goal deploy:deploy-file");
            }
        } catch (MavenInvocationException e) {
            throw new MojoExecutionException("Error invoking Maven goal deploy:deploy-file", e);
        }
    }
}
Also used : DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Matcher(java.util.regex.Matcher) MavenInvocationException(org.apache.maven.shared.invoker.MavenInvocationException) ArrayList(java.util.ArrayList) DefaultInvoker(org.apache.maven.shared.invoker.DefaultInvoker) Properties(java.util.Properties) InvocationResult(org.apache.maven.shared.invoker.InvocationResult) Artifact(org.apache.maven.artifact.Artifact) Invoker(org.apache.maven.shared.invoker.Invoker) DefaultInvoker(org.apache.maven.shared.invoker.DefaultInvoker) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) File(java.io.File)

Example 2 with MavenInvocationException

use of org.apache.maven.shared.invoker.MavenInvocationException in project syncope by apache.

the class MavenUtils method invoke.

private InvocationResult invoke(final InvocationRequest request, final String path) {
    InvocationResult result = null;
    final Invoker invoker = new DefaultInvoker();
    try {
        invoker.setLogger(new PrintStreamLogger(new PrintStream(InstallLog.getInstance().getFileAbsolutePath()), 1000));
        invoker.setOutputHandler(new PrintStreamHandler(new PrintStream(InstallLog.getInstance().getFileAbsolutePath()), true));
        invoker.setWorkingDirectory(new File(path));
        result = invoker.execute(request);
    } catch (MavenInvocationException | FileNotFoundException ex) {
        final String messageError = "Maven exception: " + ex.getMessage();
        handler.emitError(messageError, messageError);
        InstallLog.getInstance().info(messageError);
    }
    return result;
}
Also used : PrintStream(java.io.PrintStream) Invoker(org.apache.maven.shared.invoker.Invoker) DefaultInvoker(org.apache.maven.shared.invoker.DefaultInvoker) PrintStreamLogger(org.apache.maven.shared.invoker.PrintStreamLogger) MavenInvocationException(org.apache.maven.shared.invoker.MavenInvocationException) DefaultInvoker(org.apache.maven.shared.invoker.DefaultInvoker) FileNotFoundException(java.io.FileNotFoundException) InvocationResult(org.apache.maven.shared.invoker.InvocationResult) File(java.io.File) PrintStreamHandler(org.apache.maven.shared.invoker.PrintStreamHandler)

Example 3 with MavenInvocationException

use of org.apache.maven.shared.invoker.MavenInvocationException in project unleash-maven-plugin by shillner.

the class BuildProject method execute.

@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);
    }
}
Also used : DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) Invoker(org.apache.maven.shared.invoker.Invoker) DefaultInvoker(org.apache.maven.shared.invoker.DefaultInvoker) MavenInvocationException(org.apache.maven.shared.invoker.MavenInvocationException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) InvocationResult(org.apache.maven.shared.invoker.InvocationResult) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException)

Example 4 with MavenInvocationException

use of org.apache.maven.shared.invoker.MavenInvocationException in project edx-app-android by edx.

the class MavenRun method main.

public static void main(String[] args) throws IOException {
    InvocationRequest request = new DefaultInvocationRequest();
    request.setPomFile(new File("pom.xml"));
    if (args.length > 0) {
        if (args[0] != null && args[1] != null) {
            Properties projectProperties = new Properties();
            projectProperties.setProperty("deviceOS", args[0]);
            projectProperties.setProperty("appPath", args[1]);
            projectProperties.setProperty("osVersion", args[2]);
            projectProperties.setProperty("deviceName", args[3]);
            projectProperties.setProperty("udid", args[4]);
            request.setProperties(projectProperties);
        }
    }
    request.setGoals(Collections.singletonList("test"));
    Invoker invoker = new DefaultInvoker();
    invoker.setMavenHome(new File(System.getenv("M2_HOME")));
    try {
        invoker.execute(request);
    } catch (MavenInvocationException e) {
        e.printStackTrace();
    }
}
Also used : DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) InvocationRequest(org.apache.maven.shared.invoker.InvocationRequest) Invoker(org.apache.maven.shared.invoker.Invoker) DefaultInvoker(org.apache.maven.shared.invoker.DefaultInvoker) MavenInvocationException(org.apache.maven.shared.invoker.MavenInvocationException) DefaultInvoker(org.apache.maven.shared.invoker.DefaultInvoker) DefaultInvocationRequest(org.apache.maven.shared.invoker.DefaultInvocationRequest) Properties(java.util.Properties) File(java.io.File)

Example 5 with MavenInvocationException

use of org.apache.maven.shared.invoker.MavenInvocationException in project mule by mulesoft.

the class MavenTestUtils method runMavenGoal.

private static void runMavenGoal(List<String> goals, String baseDirectory) {
    Invoker invoker = new DefaultInvoker();
    invoker.setLocalRepositoryDirectory(getMavenLocalRepository());
    invoker.setLogger(LOGGER);
    LOGGER.setThreshold(3);
    InvocationRequest request = new DefaultInvocationRequest();
    request.setGoals(goals);
    request.setBatchMode(true);
    File mavenArtifactsAndBaseDirectory = new File(new File(MAVEN_ARTIFACTS_DIRECTORY), baseDirectory);
    LOGGER.info("Using Maven artifacts base directory: '" + mavenArtifactsAndBaseDirectory.getAbsolutePath() + "'...");
    request.setBaseDirectory(mavenArtifactsAndBaseDirectory);
    request.setPomFile(new File(mavenArtifactsAndBaseDirectory, "pom.xml"));
    request.setShowErrors(true);
    request.setUserSettingsFile(MAVEN_SETTINGS);
    try {
        InvocationResult result = invoker.execute(request);
        if (result.getExitCode() != 0) {
            LOGGER.error(result.getExecutionException().getMessage());
        }
    } catch (MavenInvocationException e) {
        throw new RuntimeException("Error running Maven project: " + e.getMessage());
    }
}
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)

Aggregations

MavenInvocationException (org.apache.maven.shared.invoker.MavenInvocationException)18 File (java.io.File)16 DefaultInvocationRequest (org.apache.maven.shared.invoker.DefaultInvocationRequest)15 InvocationRequest (org.apache.maven.shared.invoker.InvocationRequest)15 InvocationResult (org.apache.maven.shared.invoker.InvocationResult)13 DefaultInvoker (org.apache.maven.shared.invoker.DefaultInvoker)11 Invoker (org.apache.maven.shared.invoker.Invoker)11 Properties (java.util.Properties)5 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Artifact (org.apache.maven.artifact.Artifact)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2 MavenProject (org.apache.maven.project.MavenProject)2 MavenReportException (org.apache.maven.reporting.MavenReportException)2 CommandLineConfigurationException (org.apache.maven.shared.invoker.CommandLineConfigurationException)2 MavenCommandLineBuilder (org.apache.maven.shared.invoker.MavenCommandLineBuilder)2 RunFailureException (org.apache.maven.shared.scriptinterpreter.RunFailureException)2