Search in sources :

Example 1 with AbstractExecutionListener

use of org.apache.maven.execution.AbstractExecutionListener 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 AbstractExecutionListener

use of org.apache.maven.execution.AbstractExecutionListener in project build-info by JFrogDev.

the class BuildInfoRecorderLifecycleParticipantTest method testParticipantImplementation.

public void testParticipantImplementation() throws Exception {
    BuildInfoRecorder buildInfoRecorder = new BuildInfoRecorder();
    BuildInfoRecorderLifecycleParticipant participant = new BuildInfoRecorderLifecycleParticipant();
    Class<BuildInfoRecorderLifecycleParticipant> participantClass = BuildInfoRecorderLifecycleParticipant.class;
    Field recorderField = participantClass.getDeclaredField("recorder");
    recorderField.set(participant, buildInfoRecorder);
    Field loggerField = participantClass.getDeclaredField("logger");
    loggerField.setAccessible(true);
    loggerField.set(participant, new AbstractLogger(1, "dummy") {

        public void debug(String message, Throwable throwable) {
            Assert.assertTrue(message.contains("value is true"));
        }

        public void info(String message, Throwable throwable) {
            assert false;
        }

        public void warn(String message, Throwable throwable) {
            assert false;
        }

        public void error(String message, Throwable throwable) {
            assert false;
        }

        public void fatalError(String message, Throwable throwable) {
            assert false;
        }

        public Logger getChildLogger(String name) {
            assert false;
            return null;
        }
    });
    PlexusContainer plexusContainerMock = EasyMock.createMock(PlexusContainer.class);
    RepositorySystemSession repositorySystemSession = EasyMock.createMock(RepositorySystemSession.class);
    MavenExecutionRequest requestMock = EasyMock.createMock(MavenExecutionRequest.class);
    Properties mockSessionProperties = new Properties();
    mockSessionProperties.setProperty(BuildInfoConfigProperties.ACTIVATE_RECORDER, "true");
    EasyMock.expect(requestMock.getSystemProperties()).andReturn(mockSessionProperties).once();
    EasyMock.expect(requestMock.getUserProperties()).andReturn(mockSessionProperties).once();
    AbstractExecutionListener existingListener = new AbstractExecutionListener();
    EasyMock.expect(requestMock.getExecutionListener()).andReturn(existingListener).times(1);
    EasyMock.expect(requestMock.getUserSettingsFile()).andReturn(null).once();
    EasyMock.expect(requestMock.setExecutionListener(buildInfoRecorder)).andReturn(null).once();
    EasyMock.replay(requestMock);
    MavenExecutionResult resultMock = EasyMock.createMock(MavenExecutionResult.class);
    MavenSession session = new MavenSession(plexusContainerMock, repositorySystemSession, requestMock, resultMock);
    // value is true
    participant.afterProjectsRead(session);
}
Also used : RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) PlexusContainer(org.codehaus.plexus.PlexusContainer) AbstractExecutionListener(org.apache.maven.execution.AbstractExecutionListener) MavenExecutionResult(org.apache.maven.execution.MavenExecutionResult) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) AbstractLogger(org.codehaus.plexus.logging.AbstractLogger) Logger(org.codehaus.plexus.logging.Logger) BuildInfoConfigProperties(org.jfrog.build.api.BuildInfoConfigProperties) Properties(java.util.Properties) Field(java.lang.reflect.Field) MavenSession(org.apache.maven.execution.MavenSession) AbstractLogger(org.codehaus.plexus.logging.AbstractLogger)

Aggregations

AbstractExecutionListener (org.apache.maven.execution.AbstractExecutionListener)2 MavenEmbedderException (hudson.maven.MavenEmbedderException)1 MavenRequest (hudson.maven.MavenRequest)1 File (java.io.File)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 ExecutionEvent (org.apache.maven.execution.ExecutionEvent)1 MavenExecutionRequest (org.apache.maven.execution.MavenExecutionRequest)1 MavenExecutionResult (org.apache.maven.execution.MavenExecutionResult)1 MavenSession (org.apache.maven.execution.MavenSession)1 PlexusContainer (org.codehaus.plexus.PlexusContainer)1 AbstractLogger (org.codehaus.plexus.logging.AbstractLogger)1 Logger (org.codehaus.plexus.logging.Logger)1 RepositorySystemSession (org.eclipse.aether.RepositorySystemSession)1 PomExecutionException (org.jenkins.tools.test.exception.PomExecutionException)1 SystemIOLoggerFilter (org.jenkins.tools.test.logging.SystemIOLoggerFilter)1 BuildInfoConfigProperties (org.jfrog.build.api.BuildInfoConfigProperties)1