maven-plugin的理解与定义

news/2024/11/16 18:06:34/

文章目录

  • 一、plugin的定义与配置
    • 1. 自定义plugin
    • 2. 绑定goal到maven执行周期
    • 3. 定义goal默认的maven周期
    • 4. 命令执行当前pom定义的execution
    • 5. plugin运行时采用最新dependencies
    • 6. 插件配置不在子pom生效
  • 二、扩展

一、plugin的定义与配置


1. 自定义plugin

参考: 官网

  • 基本定义

其中@Mojo.name为goal的名称,@Parameterconfiguraiton中配置的定义

@Mojo( name = "query" ) //定义goal的名称
public class MyQueryMojoextends AbstractMojo
{@Parameter(property = "query.url", required = true)private String url;@Parameter(property = "timeout", required = false, defaultValue = "50")private int timeout;@Parameter(property = "options")private String[] options;public void execute()throws MojoExecutionException{...}
}

使用插件

<project>...<build><plugins><plugin><artifactId>maven-myquery-plugin</artifactId><version>1.0</version><configuration><url>http://www.foobar.com/query</url><timeout>10</timeout><options><option>one</option><option>two</option><option>three</option></options></configuration></plugin></plugins></build>...
</project>

  • 定义配置项类型转换

配置值到具体类型配置项的转换

Parameter ClassConversion from String
BooleanBoolean.valueOf(String)
ByteByte.decode(String)
CharacterCharacter.valueOf(char) of the first character in the given string
ClassClass.forName(String)
java.util.DateSimpleDateFormat.parse(String) for the following patterns: yyyy-MM-dd hh:mm:ss.S a, yyyy-MM-dd hh:mm:ssa, yyyy-MM-dd HH:mm:ss.S or yyyy-MM-dd HH:mm:ss
DoubleDouble.valueOf(String)
EnumEnum.valueOf(String)
java.io.Filenew File(String) with the file separators normalized to File.separatorChar. In case the file is relative, is is made absolute by prefixing it with the project’s base directory.
FloatFloat.valueOf(String)
IntegerInteger.decode(String)
LongLong.decode(String)
ShortShort.decode(String)
Stringn/a
StringBuffernew StringBuffer(String)
StringBuildernew StringBuilder(String)
java.net.URInew URI(String)
java.net.URLnew URL(String)

  • 配置项为复杂对象时

通过xml标签的缩进来体现

<project>
...
<configuration><person><firstName>Jason</firstName><lastName>van Zyl</lastName></person>
</configuration>
...
</project>

  • 配置项为接口

具体通过实现类体现

<project>
...
<configuration><person implementation="com.mycompany.mojo.query.SuperPerson"><firstName>Jason</firstName><lastName>van Zyl</lastName></person>
</configuration>
...
</project>

  • 配置项为集合
public class MyAnimalMojoextends AbstractMojo
{@Parameter(property = "animals")private List<String> animals;public void execute()throws MojoExecutionException{...}
}
<project>...<build><plugins><plugin><artifactId>maven-myanimal-plugin</artifactId><version>1.0</version><configuration><animals><animal>cat</animal><animal>dog</animal><animal>aardvark</animal></animals></configuration></plugin></plugins></build>...
</project>
<project>...<build><plugins><plugin><artifactId>maven-myanimal-plugin</artifactId><version>1.0</version><configuration><animals>cat,dog,aardvark</animals></configuration></plugin></plugins></build>...
</project>

  • 配置项为map
...@Parameterprivate Map<String,String> myMap;
...
<project>
...<configuration><myMap><key1>value1</key1><key2>value2</key2></myMap></configuration>
...
</project>

  • 配置项为properties
...@Parameterprivate Properties myProperties;  
...
<project>
...<configuration><myProperties><property><name>propertyName1</name><value>propertyValue1</value></property><property><name>propertyName2</name><value>propertyValue2</value></property></myProperties></configuration>
...
</project>

2. 绑定goal到maven执行周期


  • 绑定到单个goal

注意: 第二个goal未绑定(除非定义时有默认的绑定周期)

<project>...<build><plugins><plugin><artifactId>maven-myquery-plugin</artifactId><version>1.0</version><executions><execution><id>execution1</id><phase>test</phase><configuration><url>http://www.foo.com/query</url><timeout>10</timeout><options><option>one</option><option>two</option><option>three</option></options></configuration><goals><goal>query</goal></goals></execution><execution><id>execution2</id><configuration><url>http://www.bar.com/query</url><timeout>15</timeout><options><option>four</option><option>five</option><option>six</option></options></configuration><goals><goal>query</goal></goals></execution></executions></plugin></plugins></build>...
</project>

  • 绑定到多个周期
<project>...<build><plugins><plugin>...<executions><execution><id>execution1</id><phase>test</phase>...</execution><execution><id>execution2</id><phase>install</phase><configuration><url>http://www.bar.com/query</url><timeout>15</timeout><options><option>four</option><option>five</option><option>six</option></options></configuration><goals><goal>query</goal></goals></execution></executions></plugin></plugins></build>...
</project>

3. 定义goal默认的maven周期

@Mojo( name = "query", defaultPhase = LifecyclePhase.PACKAGE )
public class MyBoundQueryMojoextends AbstractMojo
{@Parameter(property = "query.url", required = true)private String url;@Parameter(property = "timeout", required = false, defaultValue = "50")private int timeout;@Parameter(property = "options")private String[] options;public void execute()throws MojoExecutionException{...}
}

也可以换绑到其他周期,原来的周期不再生效

<project>...<build><plugins><plugin><artifactId>maven-myquery-plugin</artifactId><version>1.0</version><executions><execution><id>execution1</id><phase>install</phase><configuration><url>http://www.bar.com/query</url><timeout>15</timeout><options><option>four</option><option>five</option><option>six</option></options></configuration><goals><goal>query</goal></goals></execution></executions></plugin></plugins></build>...
</project>

4. 命令执行当前pom定义的execution

mvn myquery:query@execution1

5. plugin运行时采用最新dependencies

<project>...<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><version>1.2</version>...<dependencies><dependency><groupId>org.apache.ant</groupId><artifactId>ant</artifactId><version>1.7.1</version></dependency><dependency><groupId>org.apache.ant</groupId><artifactId>ant-launcher</artifactId><version>1.7.1</version></dependency></dependencies></plugin></plugins></build>...
</project>

6. 插件配置不在子pom生效

默认是传播的,设置inherited=false即可

<project>...<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-antrun-plugin</artifactId><version>1.2</version><inherited>false</inherited>...</plugin></plugins></build>...
</project>

二、扩展


  • 插件使用方式推荐

父pom定义版本管理,子pom直接引用

<project>...<build><!-- To define the plugin version in your parent POM --><pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>3.3.1</version></plugin>...</plugins></pluginManagement><!-- To use the plugin goals in your POM or parent POM --><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId></plugin>...</plugins></build>...
</project>

  • maven的默认周期对应的goal
validate
initialize
generate-sources
process-sources
generate-resources
process-resources
compile
process-classes
generate-test-sources
process-test-sources
generate-test-resources
process-test-resources
test-compile
process-test-classes
test
prepare-package
package
pre-integration-test
integration-test
post-integration-test
verify
install
deploy

http://www.ppmy.cn/news/675373.html

相关文章

【Docker私有仓库】创建与配置

Docker私有仓库 一、 私有仓库搭建与配置 &#xff08;1&#xff09;拉取私有仓库镜像&#xff08;此步省略&#xff09; docker pull registry&#xff08;2&#xff09;启动私有仓库容器 docker run -di --nameregistry -p 5000:5000 registry&#xff08;3&#xff09;打…

【python】解决TypeError: ‘str‘ object cannot be interpreted as an integer

当用python的input输入一个数字时 其格式默认为string格式 所以要用int()转换为int格式 比如 import math def opgg(): # x input("请输入第一个数字") # y input("请输入第二个数字") x int(input("请输入第一个数字")) …

lol最克制诺手的英雄_LOL:62个上单英雄,竟只有3个能克制诺手?第1名很多人想不到!...

在英雄联盟中&#xff0c;诺克萨斯之手一直是个非常热门的英雄&#xff0c;以暴力的伤害能力&#xff0c;极强的对面能力和大招斩杀技能的快感&#xff0c;深受很多玩家的喜欢&#xff0c;无论是低分段还是高分段&#xff0c;使用诺手上单的玩家都很多。诺手在上路可以算是食物…

wegame饥荒一直登录中_WeGame到底有多难用?“LOL大神自己编写一个插件,完美代替WG”...

前言&#xff1a;英雄联盟作为一款已经运营了十年之久的游戏&#xff0c;已经有了非常多的玩家和粉丝&#xff0c;随时游戏的不断发展&#xff0c;比赛机制的不断完善&#xff0c;这款游戏正在逐渐的走向世界。电竞行业的越来越热门&#xff0c;导致腾讯官方加大了对游戏的监管…

SAP PP T-Code

事务码 描述(中英文) CNR1 Create Work Center 生成工作中心 CNR2 Change Work Center 更改工作中心 CNR3 Display Work Center 显示工作中心 CR00 Resource Planning Menu 资源计划菜单 CR01 Create Work Center 生成工作中心 CR02 Change Work Center 更改工作中心 CR03 Disp…

利用matlab爬虫整理云顶之弈英雄信息

问题描述 https://lolchess.gg是一个很权威的云顶之弈资料站。从这个网站上可以整理出云顶之弈所有英雄的详细资料。可惜是英文网站&#xff0c;且不像OPGG一样有中文版&#xff0c;所以我们爬取的数据只能是英文版的。真是感叹国内居然没有一家网站去认真地去做云顶之弈的资料…

Java 微信jsapi支付

spring boot微信jsapi支付 话不多说&#xff0c;撸起袖子就是干&#xff0c;下面上源码pom.xml配置文件 resources/wx.properties配置类service(WechatPayService)上impl(WechatPayServiceImpl)之前还是先上一个获取IP的工具类和返回工具类&#xff08; ResResult&#xff09;R…

关于修改host的一点整理

在和GFW玩耍的道路上尝试了挺多方法&#xff0c;这里仅对上不去的网站进行分析的方法和修改host的方法做一点整理总结&#xff0c;基本的方法和原理网上有很多教程&#xff0c;就不再赘述了&#xff0c;纯属小白的一点小小尝试。 网站被墙的三种状态 原文地址&#xff1a;htt…