构建失败时运行Maven插件 [英] Run a Maven plugin when the build fails
问题描述
我正在使用一个插件通过Maven发送Slack消息.我想知道在构建失败时是否可以使用插件,以便自动通知失败的构建?
您可以通过 ExecutionEvent
.每个事件都有项目失败,项目已跳过,等等.在这种情况下,项目失败事件就是您要寻找的. p>
事件的自定义监视只是实现EventSpy
接口的Java类.最好,它应该从AbstractEventSpy
辅助类继承.作为示例,创建一个新项目(将其命名为my-spy
),然后在包下添加以下Java类:
import org.apache.maven.eventspy.AbstractEventSpy;
import org.apache.maven.eventspy.EventSpy;
import org.apache.maven.execution.ExecutionEvent;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
@Component(role = EventSpy.class)
public class BuildFailureEventSpy extends AbstractEventSpy {
@Requirement
private Logger logger;
@Override
public void onEvent(Object event) throws Exception {
if (event instanceof ExecutionEvent) {
ExecutionEvent executionEvent = (ExecutionEvent) event;
if (executionEvent.getType() == ExecutionEvent.Type.ProjectFailed) {
logger.info("My spy detected a build failure, do the necessary here!");
}
}
}
}
此代码仅通过Plexus的@Component
批注注册间谍,并在项目构建失败时记录一条消息.要编译该类,您只需将对Maven Core的依赖关系和对plexus-component-metadata
插件的执行添加到my-spy
项目中,以为该组件创建正确的Plexus元数据.
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<version>1.6</version>
<executions>
<execution>
<goals>
<goal>generate-metadata</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
一旦该项目被编译并安装到本地存储库中(通过 只是声明一个指向间谍项目的Maven坐标的扩展名. Maven(≥ 3.3.1)默认情况下将查找该文件,因此,您的间谍将在整个构建过程中正确注册和调用. 剩下的唯一要做的事情就是编写间谍应该做的事情.在您的情况下,它应该调用Maven插件,因此您可以查看 Mojo执行器库,这很容易做到. I am using a plugin to send a Slack message through Maven. I am wondering if it's possible to use a plugin when the build failed so I get automatically notified about the failed build? You could do that within Maven itself, through the A custom spy on events is just a Java class that implements the This code simply registers the spy through the Plexus' Once this project is compiled and installed into your local repository (through Before Maven 3.3.1, you had to drop the which just declares an extension pointing to the Maven coordinates of the spy project. Maven (≥ 3.3.1) will by default look for that file, and, as such, your spy will be correctly registered and invoked throughout the build. The only remaining thing to do, is to code what the spy should do. In your case, it should invoke a Maven plugin, so you take a look at the Mojo Executor library, which makes that very easy to do. 这篇关于构建失败时运行Maven插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!mvn clean install
),您可以通过
EventSpy
mechanism, built-in from Maven 3.0.2. At each step of the build, several events are raised by Maven itself, or by custom code, and it is possible to listen to those events to perform some actions. The execution event raised by Maven are represented by the class ExecutionEvent
. Each event has a type, that describes what kind of event it represents: project failure, Mojo failure, project skipped, etc. In this case, the project failure event is what you're looking for.EventSpy
interface. Preferably, it should inherit from the AbstractEventSpy
helper class. As an example, create a new project (let's call it my-spy
), and add the following Java class under a package:import org.apache.maven.eventspy.AbstractEventSpy;
import org.apache.maven.eventspy.EventSpy;
import org.apache.maven.execution.ExecutionEvent;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
@Component(role = EventSpy.class)
public class BuildFailureEventSpy extends AbstractEventSpy {
@Requirement
private Logger logger;
@Override
public void onEvent(Object event) throws Exception {
if (event instanceof ExecutionEvent) {
ExecutionEvent executionEvent = (ExecutionEvent) event;
if (executionEvent.getType() == ExecutionEvent.Type.ProjectFailed) {
logger.info("My spy detected a build failure, do the necessary here!");
}
}
}
}
@Component
annotation, and logs a message when a project failed to build. To compile that class, you just need to add to the my-spy
project a dependency on Maven Core and an execution of the plexus-component-metadata
plugin to create the right Plexus metadata for the component.<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<version>1.6</version>
<executions>
<execution>
<goals>
<goal>generate-metadata</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
mvn clean install
), you can add it to the build of another project through the core extensions mechanism.my-spy
JAR into your ${MAVEN_HOME}/lib/ext
folder, so that Maven could find it. As of 3.3.1, you don't need to fiddle with your Maven installation, and can create a file .mvn/extensions.xml
in your project base directory (${maven.multiModuleProjectDirectory}/.mvn/extensions.xml
). Its content would be<?xml version="1.0" encoding="UTF-8"?>
<extensions>
<extension>
<groupId>my.spy</groupId>
<artifactId>my-spy</artifactId>
<version>0.0.1</version>
</extension>
</extensions>