maven作为项目管理工具,自身实现了好多功能,均以插件形式存在,一般是够用了,但为了了解,学习了下Mojo,maven提供的一个自定义插件的功能。
MOJO: maven old java object
https://www.jianshu.com/p/f0258f7628c2
maven插件的组成:
插件--->goals
maven自定义插件命名 xxx-maven-plugin
maven官方插件命名 maven-xxx-plugin
打包方式:
<packaging>maven-plugin</packaging>
maven命令的执行:
命令行执行: mvn groupid:artifactid:version:goal
如果插件的命名符合上面的自定义命名规范,可以简短写成:mvn xxx:goal
通过maven项目实现,可以构建一个archetype为mojo类型的maven项目:
pom.xml
<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.lsx.test</groupId>
<artifactId>lsxtest2-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>lsxtest2-maven-plugin Maven Plugin</name>
<!-- FIXME change it to the project's website -->
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope>
</dependency>
<!-- 否则Mojo中的org.apache.maven.model.Resource,无法解析 -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>2.2.1</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.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.5</version>
<configuration>
<goalPrefix>lsxtest2</goalPrefix>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
</configuration>
<executions>
<!--Mojo extractor for language: java-annotations found 0 mojo descriptors.
Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:3.2:helpmojo (help-goal) on project test1-generate_plugin: Error extracting plugin descriptor: 'Goal: count already exists in the plugin descriptor for prefix: test1 -->
<!-- <execution>
<id>mojo-descriptor</id>
<goals>
<goal>descriptor</goal>
</goals>
</execution> -->
<!-- <execution>
<id>help-goal</id>
<goals>
<goal>helpmojo</goal>
</goals>
</execution> -->
</executions>
</plugin>
</plugins>
</build>
</project>
如何自定义一个MOJO类(一个mojo类就对应一个goal,整个project对应一个插件)
2种方式:
在eclipse里如何运行,定义的maven命令:
项目右击--run as--mvn build ..
如何安装maven插件,安装到哪里了?
按照pom.xml中的配置,项目右击-->run as--->mvn install
生成的文件位于:
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ lsxtest2-maven-plugin ---
[INFO] Installing D:\workspace\lsxtest2-maven-plugin\target\lsxtest2-maven-plugin-1.0-SNAPSHOT.jar to D:\placeMavenRepository\com\lsx\test\lsxtest2-maven-plugin\1.0-SNAPSHOT\lsxtest2-maven-plugin-1.0-SNAPSHOT.jar
[INFO] Installing D:\workspace\lsxtest2-maven-plugin\pom.xml to D:\placeMavenRepository\com\lsx\test\lsxtest2-maven-plugin\1.0-SNAPSHOT\lsxtest2-maven-plugin-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
在我本地指定的maven仓库的位置,是package包存放,同时包含jar和pom.xml文件
仓库位置由下载安装的maven下的settings.xml中指定
不止这里的maven自定义插件项目,任何一个maven项目在最后构建的查无jar or war,mvn install 都会安装到这个指定的位置
如何将生成大的产物jar上传到maven中央仓库,供他人使用?
需要先到中央仓库管理处注册认证,提供相关信息,再在pom.xml中做相关发布配置,
参考博客https://blog.csdn.net/z69183787/article/details/81981423