Search in sources :

Example 1 with ExecutionEvent

use of org.apache.maven.execution.ExecutionEvent in project plugin-compat-tester by jenkinsci.

the class InternalMavenRunner method run.

@Override
public void run(Config config, File baseDirectory, File buildLogFile, String... goals) throws PomExecutionException {
    try {
        init(config);
    } catch (Exception x) {
        throw new PomExecutionException(x);
    }
    final List<String> succeededPlugins = new ArrayList<String>();
    try {
        MavenRequest mavenRequest = buildMavenRequest(config, baseDirectory.getAbsolutePath(), config.userSettingsFile == null ? null : config.userSettingsFile.getAbsolutePath());
        mavenRequest.setGoals(Arrays.asList(goals));
        mavenRequest.setPom(new File(baseDirectory, "pom.xml").getAbsolutePath());
        AbstractExecutionListener mavenListener = new AbstractExecutionListener() {

            @Override
            public void mojoSucceeded(ExecutionEvent event) {
                succeededPlugins.add(event.getMojoExecution().getArtifactId());
            }
        };
        mavenRequest.setExecutionListener(mavenListener);
        mavenRequest.setLoggingLevel(Logger.LEVEL_INFO);
        final PrintStream originalOut = System.out;
        final PrintStream originalErr = System.err;
        SystemIOLoggerFilter loggerFilter = new SystemIOLoggerFilter(buildLogFile);
        // Since here, we are replacing System.out & System.err by
        // wrappers logging things in the build log file
        // We can't do this by using maven embedder's logger (or plexus logger)
        // since :
        // - It would imply to Instantiate a new MavenEmbedder for every test (which have a performance/memory cost !)
        // - Plus it looks like there are lots of System.out/err.println() in maven
        // plugin (instead of using maven logger)
        System.setOut(new SystemIOLoggerFilter.SystemIOWrapper(loggerFilter, originalOut));
        System.setErr(new SystemIOLoggerFilter.SystemIOWrapper(loggerFilter, originalErr));
        try {
            executeGoals(embedder, mavenRequest);
        } catch (PomExecutionException x) {
            PomExecutionException x2 = new PomExecutionException(x);
            x2.succeededPluginArtifactIds.addAll(succeededPlugins);
            throw x2;
        } finally {
            // Setting back System.out/err
            System.setOut(originalOut);
            System.setErr(originalErr);
        }
    } catch (IOException x) {
        throw new PomExecutionException(x);
    }
}
Also used : PrintStream(java.io.PrintStream) AbstractExecutionListener(org.apache.maven.execution.AbstractExecutionListener) ArrayList(java.util.ArrayList) PomExecutionException(org.jenkins.tools.test.exception.PomExecutionException) IOException(java.io.IOException) PomExecutionException(org.jenkins.tools.test.exception.PomExecutionException) IOException(java.io.IOException) MavenEmbedderException(hudson.maven.MavenEmbedderException) SystemIOLoggerFilter(org.jenkins.tools.test.logging.SystemIOLoggerFilter) ExecutionEvent(org.apache.maven.execution.ExecutionEvent) MavenRequest(hudson.maven.MavenRequest) File(java.io.File)

Example 2 with ExecutionEvent

use of org.apache.maven.execution.ExecutionEvent in project pom-manipulation-ext by release-engineering.

the class ManipulatingEventSpy method onEvent.

@Override
public void onEvent(final Object event) throws Exception {
    boolean required = false;
    try {
        if (event instanceof ExecutionEvent) {
            final ExecutionEvent ee = (ExecutionEvent) event;
            required = Boolean.parseBoolean(ee.getSession().getRequest().getUserProperties().getProperty(REQUIRE_EXTENSION, "false"));
            final ExecutionEvent.Type type = ee.getType();
            if (type == Type.ProjectDiscoveryStarted) {
                if (ee.getSession() != null) {
                    if (ee.getSession().getRequest().getLoggingLevel() == 0) {
                        final ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
                        root.setLevel(Level.DEBUG);
                    }
                    session.setMavenSession(ee.getSession());
                    if (ee.getSession().getRequest().getPom() != null) {
                        Properties config = configIO.parse(ee.getSession().getRequest().getPom().getParentFile());
                        String value = session.getUserProperties().getProperty("allowConfigFilePrecedence");
                        if (isNotEmpty(value) && "true".equalsIgnoreCase(value)) {
                            session.getUserProperties().putAll(config);
                        } else {
                            for (String key : config.stringPropertyNames()) {
                                if (!session.getUserProperties().containsKey(key)) {
                                    session.getUserProperties().setProperty(key, config.getProperty(key));
                                }
                            }
                        }
                    }
                    manipulationManager.init(session);
                } else {
                    logger.error("Null session ; unable to continue");
                    return;
                }
                if (!session.isEnabled()) {
                    logger.info("Manipulation engine disabled via command-line option");
                    return;
                } else if (ee.getSession().getRequest().getPom() == null) {
                    logger.info("Manipulation engine disabled. No project found.");
                    return;
                } else if (new File(ee.getSession().getRequest().getPom().getParentFile(), ManipulationManager.MARKER_FILE).exists()) {
                    logger.info("Skipping manipulation as previous execution found.");
                    return;
                }
                manipulationManager.scanAndApply(session);
            }
        }
    } catch (final ManipulationException e) {
        logger.error("Extension failure", e);
        if (required) {
            throw e;
        } else {
            session.setError(e);
        }
    }// Catch any runtime exceptions and mark them to fail the build as well.
     catch (final RuntimeException e) {
        logger.error("Extension failure", e);
        if (required) {
            throw e;
        } else {
            session.setError(new ManipulationException("Caught runtime exception", e));
        }
    } finally {
        super.onEvent(event);
    }
}
Also used : Logger(org.slf4j.Logger) Properties(java.util.Properties) Type(org.apache.maven.execution.ExecutionEvent.Type) ExecutionEvent(org.apache.maven.execution.ExecutionEvent) ManipulationException(org.commonjava.maven.ext.common.ManipulationException) File(java.io.File)

Aggregations

File (java.io.File)2 ExecutionEvent (org.apache.maven.execution.ExecutionEvent)2 MavenEmbedderException (hudson.maven.MavenEmbedderException)1 MavenRequest (hudson.maven.MavenRequest)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 AbstractExecutionListener (org.apache.maven.execution.AbstractExecutionListener)1 Type (org.apache.maven.execution.ExecutionEvent.Type)1 ManipulationException (org.commonjava.maven.ext.common.ManipulationException)1 PomExecutionException (org.jenkins.tools.test.exception.PomExecutionException)1 SystemIOLoggerFilter (org.jenkins.tools.test.logging.SystemIOLoggerFilter)1 Logger (org.slf4j.Logger)1