[[432395]]
大家好,手撸我是款属冰河~~
今天,冰河给大家分享一篇大部分人都不会的于自技能,那就是己的件我们自己动手写一款属于自己的Maven插件。好了,说干就干直接进入今天的手撸主题吧。
插件与普通jar包一样包含坐标定位属性即:groupId、artifactId、于自version,己的件当使用该插件时会从本地仓库中搜索,如果没有则从远程仓库下载,例如下面的配置所示。
- <!-- 唯一定位到dependency 插件 -->
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-dependency-plugin</artifactId>
- <version>2.10</version>
execution 配置包含一组指示插件如何执行的属性:
将插件依赖拷贝到指定目录。
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-dependency-plugin</artifactId>
- <version>3.1.1</version>
- <executions>
- <execution>
- <id>copy-dependencies</id>
- <phase>package</phase>
- <goals>
- <goal>copy-dependencies</goal>
- </goals>
- <configuration>
- <outputDirectory>${ project.build.directory}/alternateLocation</outputDirectory>
- <overWriteReleases>false</overWriteReleases>
- <overWriteSnapshots>true</overWriteSnapshots>
- <excludeTransitive>true</excludeTransitive>
- </configuration>
- </execution>
- </executions>
- </plugin>
除了通过配置的方式使用插件以外,Maven也提供了通过命令直接调用插件目标其命令格式如下所示。
- mvn groupId:artifactId:version:goal -D{ 参数名}
通过命令执行插件示例
(2)展示pom的依赖关系树
- mvn org.apache.maven.plugins:maven-dependency-plugin:2.10:tree
(3)直接简化版的命令,但前提必须是maven 官方插件
- mvn dependency:tree
(1)查看pom 文件的最终配置
- mvn help:effective-pom
(2)原型项目生成
- archetype:generate
(3)快速创建一个Web程序
- mvn archetype:generate -DgroupId=io.mykit.web -DartifactId=mykit-maven-web -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
(4)快速创建一个Java项目
- mvn archetype:generate -DgroupId=io.mykit.java -DartifactId=mykit-maven-java -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
这里,我们先来说说自定义Maven插件的步骤,这里,我将自定义Maven插件的步骤总结如下。
(1)创建maven 插件项目
(2)设定packaging 为maven-plugin
(3)添加插件依赖
(4)编写插件实现逻辑
(5)打包构建插件
接下来,我们就来实现自定义Maven插件。
这里,我直接跳过了Maven项目的创建,相信小伙伴们都会创建Maven项目,这里,我就不再赘述了,直接给出pom.xml文件的配置,如下所示。
- <?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>io.mykit.plugin</groupId>
- <version>1.0.0-SNAPSHOT</version>
- <artifactId>mykit-maven-plugin</artifactId>
- <packaging>maven-plugin</packaging>
- <dependencies>
- <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>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.6.1</version>
- <configuration>
- <source>1.8 </source>
- <target>1.8 </target>
- </configuration>
- </plugin>
- </plugins>
- </build>
- </project>
接下来,我们来编写Maven插件的实现类。这里,我直接给出类的源代码,如下所示。
- package io.mykit.plugin;
- import javafx.beans.DefaultProperty;
- import org.apache.maven.plugin.AbstractMojo;
- import org.apache.maven.plugin.MojoExecutionException;
- import org.apache.maven.plugin.MojoFailureException;
- import org.apache.maven.plugins.annotations.LifecyclePhase;
- import org.apache.maven.plugins.annotations.Mojo;
- import org.apache.maven.plugins.annotations.Parameter;
- /**
- * @author binghe
- * @description 自定义Maven插件
- */
- @Mojo(name = "binghe")
- public class MykitMavenPlugin extends AbstractMojo {
- private final Logger logger = LoggerFactory.getLogger(MykitMavenPlugin.class);
- @Parameter(property="sex")
- String sex;
- @Parameter(property="describe")
- String describe;
- public void execute() throws MojoExecutionException, MojoFailureException {
- logger.info(String.format("binghe sex=%s describe=%s",sex,describe));
- }
- }
将自定义的Maven插件打包并安装到本地Maven仓库,如下所示。
- mvn clean package
- mvn clean install
如果需要在其他项目中使用我们自定义的Maven插件,则只需要创建一个Maven项目,在Maven项目的pom.xml文件中引入如下配置即可。
- <build>
- <plugins>
- <plugin>
- <!--引入刚刚的插件依赖-->
- <groupId>io.mykit.plugin</groupId>
- <version>1.0.0-SNAPSHOT</version>
- <artifactId>mykit-maven-plugin</artifactId>
- <!-- 这里加入的是相关参数,这些参数必须跟插件实现里面的属性保持一致 -->
- <configuration>
- <sex>男</sex>
- <describe>冰河技术微信公众号作者</describe>
- </configuration>
- </plugin>
- </plugins>
- </build>
好了,今天就到这儿吧,我是冰河,我们下期见!!
责任编辑:武晓燕 来源: 冰河技术 插件MavengroupId
(责任编辑:综合)
信用购关闭后还能开通吗 征信上的信用购基本借贷记录会一直存在吗?
《SYNDUALITY: Echo of Ada》Steam页面上线 年内发售
棠记控股(08305.HK)预计年度亏损不少于50万港元 毛利严重下降