Search in sources :

Example 21 with MavenExecutionRequest

use of org.apache.maven.execution.MavenExecutionRequest in project meecrowave by apache.

the class MeecrowaveBundleMojoTest method bundle.

@Test
public void bundle() throws Exception {
    final File moduleBase = jarLocation(MeecrowaveBundleMojoTest.class).getParentFile().getParentFile();
    final File basedir = new File(moduleBase, "src/test/resources/" + getClass().getSimpleName());
    final File pom = new File(basedir, "pom.xml");
    final MavenExecutionRequest request = new DefaultMavenExecutionRequest();
    request.setBaseDirectory(basedir);
    final ProjectBuildingRequest configuration = request.getProjectBuildingRequest();
    final DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory().newInstance(repositorySession, new LocalRepository(new File(moduleBase, "target/fake"), "")));
    configuration.setRepositorySession(repositorySession);
    final MavenProject project = mojo.lookup(ProjectBuilder.class).build(pom, configuration).getProject();
    final Build build = new Build();
    final File buildDir = new File("target/" + getClass().getName() + "/build");
    build.setDirectory(buildDir.getAbsolutePath());
    project.setBuild(build);
    final MavenSession session = mojo.newMavenSession(project);
    final MojoExecution execution = mojo.newMojoExecution("bundle");
    execution.getConfiguration().addChild(new Xpp3Dom("enforceMeecrowave") {

        {
            setValue(Boolean.FALSE.toString());
        }
    });
    execution.getConfiguration().addChild(new Xpp3Dom("enforceCommonsCli") {

        {
            setValue(Boolean.FALSE.toString());
        }
    });
    execution.getConfiguration().addChild(new Xpp3Dom("conf") {

        {
            setValue("src/main/meecrowave/conf");
        }
    });
    execution.getConfiguration().addChild(new Xpp3Dom("webapp") {

        {
            setValue("src/main/webapp");
        }
    });
    mojo.executeMojo(session, project, execution);
    assertTrue(buildDir.exists());
    try (final ZipFile zip = new ZipFile(new File(buildDir, "test-meecrowave-distribution.zip"))) {
        assertTrue(zip.getEntry("test-distribution/docBase/sub/index.html") != null);
    }
}
Also used : Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) LocalRepository(org.eclipse.aether.repository.LocalRepository) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) MavenSession(org.apache.maven.execution.MavenSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) MavenProject(org.apache.maven.project.MavenProject) ZipFile(java.util.zip.ZipFile) MojoExecution(org.apache.maven.plugin.MojoExecution) Build(org.apache.maven.model.Build) SimpleLocalRepositoryManagerFactory(org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory) ZipFile(java.util.zip.ZipFile) File(java.io.File) Test(org.junit.Test)

Example 22 with MavenExecutionRequest

use of org.apache.maven.execution.MavenExecutionRequest in project kie-wb-common by kiegroup.

the class AFMavenCli method loadCoreExtensions.

protected List<CoreExtensionEntry> loadCoreExtensions(AFCliRequest cliRequest, ClassRealm containerRealm, Set<String> providedArtifacts) {
    if (cliRequest.getMultiModuleProjectDirectory() == null) {
        return Collections.emptyList();
    }
    Path extensionsFile = Paths.get(cliRequest.getMultiModuleProjectDirectory().toString(), EXTENSIONS_FILENAME);
    if (!java.nio.file.Files.isRegularFile(extensionsFile)) {
        return Collections.emptyList();
    }
    try {
        List<CoreExtension> extensions = readCoreExtensionsDescriptor(extensionsFile);
        if (extensions.isEmpty()) {
            return Collections.emptyList();
        }
        ContainerConfiguration cc = // 
        new DefaultContainerConfiguration().setClassWorld(// 
        cliRequest.getClassWorld()).setRealm(// 
        containerRealm).setClassPathScanning(// 
        PlexusConstants.SCANNING_INDEX).setAutoWiring(// 
        true).setName("maven");
        DefaultPlexusContainer container = new DefaultPlexusContainer(cc, new AbstractModule() {

            @Override
            protected void configure() {
                bind(ILoggerFactory.class).toInstance(slf4jLoggerFactory);
            }
        });
        try {
            container.setLookupRealm(null);
            container.setLoggerManager(plexusLoggerManager);
            container.getLoggerManager().setThresholds(cliRequest.getRequest().getLoggingLevel());
            Thread.currentThread().setContextClassLoader(container.getContainerRealm());
            executionRequestPopulator = container.lookup(MavenExecutionRequestPopulator.class);
            configurationProcessors = container.lookupMap(AFConfigurationProcessor.class);
            configure(cliRequest);
            MavenExecutionRequest request = DefaultMavenExecutionRequest.copy(cliRequest.getRequest());
            request = populateRequest(cliRequest, request);
            request = executionRequestPopulator.populateDefaults(request);
            BootstrapCoreExtensionManager resolver = container.lookup(BootstrapCoreExtensionManager.class);
            return resolver.loadCoreExtensions(request, providedArtifacts, extensions);
        } finally {
            executionRequestPopulator = null;
            container.dispose();
        }
    } catch (RuntimeException e) {
        // runtime exceptions are most likely bugs in maven, let them bubble up to the user
        throw e;
    } catch (Exception e) {
        slf4jLogger.warn("Failed to read extensions descriptor " + extensionsFile + ": " + e.getMessage());
    }
    return Collections.emptyList();
}
Also used : Path(java.nio.file.Path) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) ContainerConfiguration(org.codehaus.plexus.ContainerConfiguration) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) LifecycleExecutionException(org.apache.maven.lifecycle.LifecycleExecutionException) InternalErrorException(org.apache.maven.InternalErrorException) UnrecognizedOptionException(org.apache.commons.cli.UnrecognizedOptionException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.commons.cli.ParseException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) IOException(java.io.IOException) MavenExecutionRequestPopulationException(org.apache.maven.execution.MavenExecutionRequestPopulationException) AbstractModule(com.google.inject.AbstractModule) CoreExtension(org.apache.maven.cli.internal.extension.model.CoreExtension) DefaultContainerConfiguration(org.codehaus.plexus.DefaultContainerConfiguration) DefaultPlexusContainer(org.codehaus.plexus.DefaultPlexusContainer) BootstrapCoreExtensionManager(org.apache.maven.cli.internal.BootstrapCoreExtensionManager) MavenExecutionRequestPopulator(org.apache.maven.execution.MavenExecutionRequestPopulator)

Example 23 with MavenExecutionRequest

use of org.apache.maven.execution.MavenExecutionRequest in project kie-wb-common by kiegroup.

the class AFMavenCli method execute.

protected int execute(AFCliRequest cliRequest) throws MavenExecutionRequestPopulationException {
    MavenExecutionRequest request = executionRequestPopulator.populateDefaults(cliRequest.getRequest());
    eventSpyDispatcher.onEvent(request);
    MavenExecutionResult result = maven.execute(request);
    eventSpyDispatcher.onEvent(result);
    eventSpyDispatcher.close();
    if (result.hasExceptions()) {
        ExceptionHandler handler = new DefaultExceptionHandler();
        Map<String, String> references = new LinkedHashMap<String, String>();
        MavenProject project = null;
        for (Throwable exception : result.getExceptions()) {
            ExceptionSummary summary = handler.handleException(exception);
            logSummary(summary, references, "", cliRequest.isShowErrors());
            if (project == null && exception instanceof LifecycleExecutionException) {
                project = ((LifecycleExecutionException) exception).getProject();
            }
        }
        slf4jLogger.error("");
        if (!cliRequest.isShowErrors()) {
            slf4jLogger.error("To see the full stack trace of the errors, re-run Maven with the -e switch.");
        }
        if (!slf4jLogger.isDebugEnabled()) {
            slf4jLogger.error("Re-run Maven using the -X switch to enable full debug logging.");
        }
        if (!references.isEmpty()) {
            slf4jLogger.error("");
            slf4jLogger.error("For more information about the errors and possible solutions" + ", please read the following articles:");
            for (Entry<String, String> entry : references.entrySet()) {
                slf4jLogger.error(entry.getValue() + " " + entry.getKey());
            }
        }
        if (project != null && !project.equals(result.getTopologicallySortedProjects().get(0))) {
            slf4jLogger.error("");
            slf4jLogger.error("After correcting the problems, you can resume the build with the command");
            slf4jLogger.error("  mvn <goals> -rf :" + project.getArtifactId());
        }
        if (MavenExecutionRequest.REACTOR_FAIL_NEVER.equals(cliRequest.getRequest().getReactorFailureBehavior())) {
            slf4jLogger.info("Build failures were ignored.");
            return 0;
        } else {
            return 1;
        }
    } else {
        return 0;
    }
}
Also used : DefaultExceptionHandler(org.apache.maven.exception.DefaultExceptionHandler) ExceptionHandler(org.apache.maven.exception.ExceptionHandler) ExceptionSummary(org.apache.maven.exception.ExceptionSummary) MavenProject(org.apache.maven.project.MavenProject) LifecycleExecutionException(org.apache.maven.lifecycle.LifecycleExecutionException) MavenExecutionResult(org.apache.maven.execution.MavenExecutionResult) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultExceptionHandler(org.apache.maven.exception.DefaultExceptionHandler) LinkedHashMap(java.util.LinkedHashMap)

Example 24 with MavenExecutionRequest

use of org.apache.maven.execution.MavenExecutionRequest in project kie-wb-common by kiegroup.

the class AFSettingsXmlConfigurationProcessor method process.

@Override
public void process(AFCliRequest cliRequest) throws Exception {
    CommandLine commandLine = cliRequest.getCommandLine();
    String workingDirectory = cliRequest.getWorkingDirectory();
    MavenExecutionRequest request = cliRequest.getRequest();
    Path userSettingsFile;
    if (commandLine.hasOption(CLIManager.ALTERNATE_USER_SETTINGS)) {
        String settingsFromCLi = commandLine.getOptionValue(CLIManager.ALTERNATE_USER_SETTINGS);
        logger.info("userSettings:" + settingsFromCLi);
        if (settingsFromCLi != null) {
            userSettingsFile = Paths.get(settingsFromCLi.trim());
            if (!Files.isRegularFile(userSettingsFile)) {
                throw new FileNotFoundException("The specified user settings file does not exist: " + userSettingsFile);
            }
        } else {
            userSettingsFile = DEFAULT_USER_SETTINGS_FILE;
            logger.info("Using default userSettings:" + userSettingsFile);
        }
    } else {
        userSettingsFile = DEFAULT_USER_SETTINGS_FILE;
    }
    Path globalSettingsFile;
    if (commandLine.hasOption(CLIManager.ALTERNATE_GLOBAL_SETTINGS)) {
        globalSettingsFile = Paths.get(commandLine.getOptionValue(CLIManager.ALTERNATE_GLOBAL_SETTINGS));
        globalSettingsFile = resolvePath(globalSettingsFile, workingDirectory);
        if (!Files.isRegularFile(globalSettingsFile)) {
            throw new FileNotFoundException("The specified global settings file does not exist: " + globalSettingsFile);
        }
    } else {
        globalSettingsFile = DEFAULT_GLOBAL_SETTINGS_FILE;
    }
    request.setGlobalSettingsFile(globalSettingsFile.toFile());
    request.setUserSettingsFile(userSettingsFile.toFile());
    AFSettingsBuildingRequest settingsRequest = new AFSettingsBuildingRequest();
    settingsRequest.setGlobalSettingsFile(globalSettingsFile.toFile());
    settingsRequest.setUserSettingsFile(userSettingsFile.toFile());
    settingsRequest.setSystemProperties(cliRequest.getSystemProperties());
    settingsRequest.setUserProperties(cliRequest.getUserProperties());
    if (request.getEventSpyDispatcher() != null) {
        request.getEventSpyDispatcher().onEvent(settingsRequest);
    }
    logger.debug("Reading global settings from " + getLocation(settingsRequest.getGlobalSettingsSource(), settingsRequest.getGlobalSettingsPath()));
    logger.debug("Reading user settings from " + getLocation(settingsRequest.getUserSettingsSource(), settingsRequest.getUserSettingsPath()));
    SettingsBuildingResult settingsResult = settingsBuilder.build(settingsRequest);
    if (request.getEventSpyDispatcher() != null) {
        request.getEventSpyDispatcher().onEvent(settingsResult);
    }
    populateFromSettings(request, settingsResult.getEffectiveSettings());
    if (!settingsResult.getProblems().isEmpty() && logger.isWarnEnabled()) {
        logger.warn("");
        logger.warn("Some problems were encountered while building the effective settings");
        for (SettingsProblem problem : settingsResult.getProblems()) {
            logger.warn(problem.getMessage() + " @ " + problem.getLocation());
        }
        logger.warn("");
    }
}
Also used : Path(java.nio.file.Path) CommandLine(org.apache.commons.cli.CommandLine) SettingsBuildingResult(org.apache.maven.settings.building.SettingsBuildingResult) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) FileNotFoundException(java.io.FileNotFoundException) SettingsProblem(org.apache.maven.settings.building.SettingsProblem)

Example 25 with MavenExecutionRequest

use of org.apache.maven.execution.MavenExecutionRequest in project kie-wb-common by kiegroup.

the class ReusableAFMavenCli method execute.

protected int execute(AFCliRequest cliRequest) throws MavenExecutionRequestPopulationException {
    MavenExecutionRequest request = reusableExecutionRequestPopulator.populateDefaults(cliRequest.getRequest());
    reusableEventSpyDispatcher.onEvent(request);
    MavenExecutionResult result = reusableMaven.execute(request);
    reusableEventSpyDispatcher.onEvent(result);
    reusableEventSpyDispatcher.close();
    if (result.hasExceptions()) {
        ExceptionHandler handler = new DefaultExceptionHandler();
        Map<String, String> references = new LinkedHashMap<String, String>();
        MavenProject project = null;
        for (Throwable exception : result.getExceptions()) {
            ExceptionSummary summary = handler.handleException(exception);
            logSummary(summary, references, "", cliRequest.isShowErrors());
            if (project == null && exception instanceof LifecycleExecutionException) {
                project = ((LifecycleExecutionException) exception).getProject();
            }
        }
        reusableSlf4jLogger.error("");
        if (!cliRequest.isShowErrors()) {
            reusableSlf4jLogger.error("To see the full stack trace of the errors, re-run Maven with the -e switch.");
        }
        if (!reusableSlf4jLogger.isDebugEnabled()) {
            reusableSlf4jLogger.error("Re-run Maven using the -X switch to enable full debug logging.");
        }
        if (!references.isEmpty()) {
            reusableSlf4jLogger.error("");
            reusableSlf4jLogger.error("For more information about the errors and possible solutions" + ", please read the following articles:");
            for (Entry<String, String> entry : references.entrySet()) {
                reusableSlf4jLogger.error(entry.getValue() + " " + entry.getKey());
            }
        }
        if (project != null && !project.equals(result.getTopologicallySortedProjects().get(0))) {
            reusableSlf4jLogger.error("");
            reusableSlf4jLogger.error("After correcting the problems, you can resume the build with the command");
            reusableSlf4jLogger.error("  mvn <goals> -rf :" + project.getArtifactId());
        }
        if (MavenExecutionRequest.REACTOR_FAIL_NEVER.equals(cliRequest.getRequest().getReactorFailureBehavior())) {
            reusableSlf4jLogger.info("Build failures were ignored.");
            return 0;
        } else {
            return 1;
        }
    } else {
        return 0;
    }
}
Also used : DefaultExceptionHandler(org.apache.maven.exception.DefaultExceptionHandler) ExceptionHandler(org.apache.maven.exception.ExceptionHandler) ExceptionSummary(org.apache.maven.exception.ExceptionSummary) MavenProject(org.apache.maven.project.MavenProject) LifecycleExecutionException(org.apache.maven.lifecycle.LifecycleExecutionException) MavenExecutionResult(org.apache.maven.execution.MavenExecutionResult) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultExceptionHandler(org.apache.maven.exception.DefaultExceptionHandler) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)56 DefaultMavenExecutionRequest (org.apache.maven.execution.DefaultMavenExecutionRequest)40 File (java.io.File)24 MavenSession (org.apache.maven.execution.MavenSession)20 MavenExecutionResult (org.apache.maven.execution.MavenExecutionResult)14 MavenProject (org.apache.maven.project.MavenProject)14 DefaultMavenExecutionResult (org.apache.maven.execution.DefaultMavenExecutionResult)10 IOException (java.io.IOException)9 MavenExecutionRequestPopulationException (org.apache.maven.execution.MavenExecutionRequestPopulationException)9 MavenExecutionRequestPopulator (org.apache.maven.execution.MavenExecutionRequestPopulator)9 DefaultPlexusContainer (org.codehaus.plexus.DefaultPlexusContainer)8 ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)7 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)7 FileNotFoundException (java.io.FileNotFoundException)6 Path (java.nio.file.Path)6 Properties (java.util.Properties)6 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)6 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)6 LifecycleExecutionException (org.apache.maven.lifecycle.LifecycleExecutionException)5 ProjectBuilder (org.apache.maven.project.ProjectBuilder)5