1. provide
Plug-in provider project structure
├ ─ ─ pom. XML ├ ─ ─ the SRC │ └ ─ ─ the main │ ├ ─ ─ Java │ │ └ ─ ─ com │ │ └ ─ ─ fantj │ │ └ ─ ─ mypluginprovide │ │ └ ─ ─ MyMojo. Java │ └ ─ ─ Resources │ └ ─ ─ application. The properties └ ─ ─ target │ └ ─ ─ my - plugin - dojo.provide - 0.0.1 - the SNAPSHOT. The jarCopy the code
1.1 modify the packaging
<packaging>maven-plugin</packaging>
Copy the code
1.2. The modified pom
< the dependency > < groupId > org, apache maven < / groupId > < artifactId > maven plugin - API < / artifactId > < version > 3.5.0 < / version > </dependency> <dependency> <groupId>org.apache.maven.plugin-tools</groupId> < artifactId > maven plugin - annotations < / artifactId > < version > 3.5 < / version > < / dependency >Copy the code
These two dependencies are required by a custom plug-in, which represents a Mojo project that contains Mojo interfaces and abstract classes and annotations.
1.3 Coding business logic
@Mojo(name = "fantj",defaultPhase = LifecyclePhase.PACKAGE)
public class MyMojo extends AbstractMojo {
@Parameter
private String msg;
@Parameter
private List<String> list;
public void execute() throws MojoExecutionException, MojoFailureException {
System.out.println("hello plugin: "+msg + "list: "+ list); }}Copy the code
Note here @ Parameter ` ` @ Mojo ` ` LifecyclePhase. The PACKAGE is org.. Apache maven. Plugins. Annotations PACKAGE under:
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
Copy the code
The @parameter annotation takes and assigns values to variables in the consumer profile.
DefaultPhase = lifecyclePhase. PACKAGE declares the lifecycle triggered by the plug-in.
@mojo Defines the goal name of the plug-in.
1.3. The clean and install
MVN Clean Install generates a jar package in the target directory, which is the plug-in package.
2. consume
Plug-in consumer: Project structure
├ ─ ─ pom. XML └ ─ ─ the SRC └ ─ ─ the main ├ ─ ─ Java │ └ ─ ─ com │ └ ─ ─ fantj │ └ ─ ─ mypluginuse └ ─ ─ resources └ ─ ─ application. The propertiesCopy the code
2.1 modified pom
<? xml version="1.0" encoding="UTF-8"? > <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> < modelVersion > 4.0.0 < / modelVersion > < groupId > com. Fantj < / groupId > < artifactId > my - plugin - consume < / artifactId > <version>0.0.1-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>com.fantj</groupId> <artifactId>my-plugin-provide</artifactId> <version>0.0.1 -snapshot </version> <configuration> < MSG >hello plugin</ MSG > <list> <list>one</list> <list>two</list> </list> </configuration> <! Executions <execution> <phase>package</phase> < Goals > <goal> Fantj </goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>Copy the code
Executions without
, can only be done by executing the plug-in or the command, if you want it to be executed automatically when the package is executed.
2.2 How to Pass Parameters to Plugin
In the plug-in provider, there is a MyMojo class with this code:
@Parameter
private String msg;
@Parameter
private List<String> list;
Copy the code
It is used to annotate the values of parameters, just like the Spring annotations you used.
Accordingly, in the plug-in consumer configuration we should give the corresponding parameter definition:
<configuration>
<msg>hello plugin</msg>
<list>
<list>one</list>
<list>two</list>
</list>
</configuration>
Copy the code
The above configuration corresponds to the variable name one by one. You will notice that the Maven plugin automatically adds a plugins option:
Run the plugin: MVN myProvide: Fantj or just click
[fantj@lalala my-plugin-consume]$ mvn myprovide:fantj
[INFO] Scanning forprojects... [INFO] [INFO] --------------------< com.fantj:my-plugin-consume >--------------------- [INFO] Building my-plugin-consume 0.0.1 - the SNAPSHOT [INFO] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- (jar) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- [INFO] - [INFO] My-plugin-provide :0.0.1-SNAPSHOT: Fantj (default-cli) @my-plugin-consume -- hello plugin: hello pluginlist: [one, two] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- [INFO] Total time: 0.347 s [INFO] Finished at: 2018-11-01T19:59:04+08:00 [INFO] ------------------------------------------------------------------------Copy the code
If you like my article, please pay attention to my official account, which focuses on the analysis of architect technology. The official account is still in the initial stage, thank you for your support.
Java architecture