maven插件配置
自定义绑定
除了默认绑定的一些操作,我们也可以将一些阶段绑定到指定的插件目标上来完成一些操作,这种自定义绑定让maven项目在构件的过程中可以执行更多更丰富的操作;
mvn source:help
比如验证阶段打包源码:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.0</version>
<executions>
<!-- 使用插件需要执行的任务 -->
<execution>
<!-- 任务id唯一,如果不指定,默认为default -->
<id>attach-source</id>
<!-- 任务中插件的目标,可以指定多个 -->
<goals>
<goal>jar-no-fork</goal>
</goals>
<!-- 绑定的阶段 -->
<phase>verify</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
比如验证阶段进行清理:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<!-- 使用插件需要执行的任务 -->
<execution>
<!-- 任务中插件的目标,可以指定多个 -->
<id>clean-target</id>
<goals>
<goal>clean</goal>
</goals>
<!-- 绑定的阶段 -->
<phase>validate</phase>
</execution>
</executions>
</plugin>
POM.xml插件配置
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<!-- 插件参数配置,对插件中所有的目标起效 -->
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
插件的目标 == 相当于一个方法
插件目标的参数 == 相当于一个方法的参数
跳过测试还可以:
mvn -Dmaven.test.skip=tue
mvn -Dskip=true
properties中配置: <maven.test.skip>true</maven.test.skip>
例子二:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<executions>
<execution>
<goals>
<goal>test</goal>
<goal>help</goal>
</goals>
<phase>pre-clean</phase>
<!-- 这个地方配置只对当前任务有效 -->
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
如上自定义了一个绑定,在clean周期的pre-clean阶段绑定了插件maven-surefire-plugin的两个目标test和help,execution元素没有指定id,所以默认id是default;
插件仓库
与其他maven构件一样,插件构件也是基于坐标存储在maven仓库中,有需要的时候,maven会从本地查找插件,如果不存在,则到远程仓库查找,找到了以后下载到本地仓库,然后使用;
pom.xml中可以配置依赖的构件的仓库地址:
<repositories>
<repository>
<id>Custom Central Repository</id>
<url>https://repo.maven.apache.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
pom.xml中也可以配置插件的仓库地址:
<pluginRepositories>
<pluginRepository>
<id>myplugin-repository</id>
<url>http://repo1.maven.org/maven2/</url>
<releases>
<enabled>true</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
在pom.xml中配置插件的时候,如果是官方的插件,可以省略groupId;
默认为:
<groupId>org.apache.maven.plugins</groupId>
建议写上groupId,比较清晰;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2019-02-02 Quartz使用一