Skip to content

Instantly share code, notes, and snippets.

@romain-grecourt
Created February 27, 2020 23:05
Show Gist options
  • Save romain-grecourt/06d8e7540d1025c8813ee6426ec3c21e to your computer and use it in GitHub Desktop.
Save romain-grecourt/06d8e7540d1025c8813ee6426ec3c21e to your computer and use it in GitHub Desktop.
maven life-cycle execution listener
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.build.maven;
import org.apache.maven.AbstractMavenLifecycleParticipant;
import org.apache.maven.MavenExecutionException;
import org.apache.maven.execution.AbstractExecutionListener;
import org.apache.maven.execution.ExecutionEvent;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecution;
import org.codehaus.plexus.component.annotations.Component;
@Component(role = AbstractMavenLifecycleParticipant.class)
public class LifecycleParticipant extends AbstractMavenLifecycleParticipant {
private final LifecycleExecutionListener listener;
public LifecycleParticipant() {
listener = new LifecycleExecutionListener();
}
@Override
public void afterProjectsRead(MavenSession session) throws MavenExecutionException {
session.getRequest().setExecutionListener(listener);
}
private static final class LifecycleExecutionListener extends AbstractExecutionListener {
private String phase;
@Override
public void mojoStarted(ExecutionEvent event) {
MojoExecution mojoExecution = event.getMojoExecution();
if (mojoExecution != null) {
String currentPhase = mojoExecution.getLifecyclePhase();
if (!currentPhase.equals(phase)) {
phase = currentPhase;
System.out.println("LIFECYCLE: " + phase);
}
}
}
}
}
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<version>2.1.0</version>
<executions>
<execution>
<goals>
<goal>generate-metadata</goal>
</goals>
</execution>
</executions>
</plugin>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment