Article directory
- 1. Definition and configuration of plugin
-
- 1. Custom plugin
- 2. Bind goal to maven execution cycle
- 3. Define the default maven cycle of the goal
- 4. The command executes the execution defined by the current pom
- 5. The plugin runs with the latest dependencies
- 6. The plugin configuration does not take effect in the child pom
- 2. Expansion
1. Definition and configuration of plugin
1. Custom plugin
Reference: official website
- basic definition
Among them, @Mojo.name
is the name of the goal, and @Parameter
is the definition configured in configuraiton
@Mojo( name = "query" ) //Define the name of the goal public class MyQueryMojo extends 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 {<!-- --> ... } }
use plugin
<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>
- Define configuration item type conversions
Conversion of configuration values to concrete type configuration items
Parameter Class | Conversion from String |
---|---|
Boolean |
Boolean.valueOf(String) |
Byte |
Byte.decode (String) |
Character |
Character.valueOf(char) of the first character in the given string |
Class |
Class.forName(String) |
java.util.Date |
SimpleDateFormat.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 |
Double |
Double.valueOf(String) |
Enum |
Enum.valueOf(String) |
java.io.File |
new 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. |
Float |
Float. valueOf(String) |
Integer |
Integer.decode(String) |
Long |
Long.decode(String) |
Short |
Short.decode(String) |
String |
n/a |
StringBuffer |
new StringBuffer(String) |
StringBuilder |
new StringBuilder( String) |
java.net.URI |
new URI(String) |
java.net.URL |
new URL(String) |
- When the configuration item is a complex object
It is reflected by the indentation of the xml tag
<project> ... <configuration> <person> <firstName>Jason</firstName> <lastName>van Zyl</lastName> </person> </configuration> ... </project>
- The configuration item is the interface
Concretely reflected by the implementation class
<project> ... <configuration> <person implementation="com.mycompany.mojo.query.SuperPerson"> <firstName>Jason</firstName> <lastName>van Zyl</lastName> </person> </configuration> ... </project>
- Configuration items are collections
public class MyAnimalMojo extends 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>
- The configuration item is map
... @Parameter private Map<String,String> myMap; ...
<project> ... <configuration> <myMap> <key1>value1</key1> <key2>value2</key2> </myMap> </configuration> ... </project>
- The configuration item is properties
... @Parameter private 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. Bind goal to maven execution cycle
- bound to a single goal
Note: The second goal is not bound (unless it is defined with a default binding period)
<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>
- bound to multiple cycles
<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. Define the default maven cycle of the goal
@Mojo( name = "query", defaultPhase = LifecyclePhase.PACKAGE ) public class MyBoundQueryMojo extends 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 {<!-- --> ... } }
It can also be changed to another cycle, and the original cycle will no longer take effect
<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. The command executes the execution defined by the current pom
mvn myquery:query@execution1
5. Plugin runs with the latest 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. The plug-in configuration does not take effect in the child pom
The default is propagated, just set 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>
2. Extension
- Plug-in usage recommendation
The parent pom defines version management, and the child pom directly references it
<project> ... <build> <!-- To define the plugin version in your parent POM --> <plugin Management> <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>
- The goal corresponding to the default cycle of maven
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