使用Archetype创建自己的plugin

1 使用 mvn archetype : generate命令,下载对应的jar和进入Interactive mode。默认情况,上面命令会显示所有的可用的archetype artifact。由于输出的archetype artifact太多,建议使用加Filter参数,我们需要创建一个Archetype ArtifactId为maven-archetype-plugin的project, 所有命令行如下:
D:\test>mvn archetype:generate -Dfilter=maven-archetype-plugin
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.3:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.3:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.3:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: remote -> org.apache.maven.archetypes:maven-archetype-plugin (An archetype which contains a sample Maven plugin.)
2: remote -> org.apache.maven.archetypes:maven-archetype-plugin-site (An archetype which contains a sample Maven plugin site. This archetype can be layered upon an
    existing Maven plugin project.)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): :
按照提示,填充groupId, artifactId, version, package,即可正常的生成项目。
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1
Choose org.apache.maven.archetypes:maven-archetype-plugin version:
1: 1.0
2: 1.1
3: 1.2
Choose a number: 3:
Define value for property 'groupId': : com.lewi
Define value for property 'artifactId': : plugin
Define value for property 'version':  1.0-SNAPSHOT: : 1.0
Define value for property 'package':  com.lewi: :
Confirm properties configuration:
groupId: com.lewi
artifactId: plugin
version: 1.0
package: com.lewi
 Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-plugin:1.2
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.lewi
[INFO] Parameter: artifactId, Value: plugin
[INFO] Parameter: version, Value: 1.0
[INFO] Parameter: package, Value: com.lewi
[INFO] Parameter: packageInPathFormat, Value: com/lewi
[INFO] Parameter: package, Value: com.lewi
[INFO] Parameter: version, Value: 1.0
[INFO] Parameter: groupId, Value: com.lewi
[INFO] Parameter: artifactId, Value: plugin
[INFO] project created from Archetype in dir: D:\test\plugin
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:22 min
[INFO] Finished at: 2015-06-30T14:01:30+08:00
[INFO] Final Memory: 16M/244M
[INFO] ------------------------------------------------------------------------
在目录D:\test\plugin下面,可以看到生成成功的pom.xml和src文件夹。

2 进入D:\test\plugin目录,进行项目的编辑和安装。
运行mvn install,可以查看到安装成功。
[INFO] --- maven-install-plugin:2.4:install (default-install) @ plugin ---
[INFO] Installing D:\test\plugin\target\plugin-1.0.jar to C:\Users\lyue\.m2\repository\com\lewi\plugin\1.0\plugin-1.0.jar
[INFO] Installing D:\test\plugin\pom.xml to C:\Users\lyue\.m2\repository\com\lewi\plugin\1.0\plugin-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.674 s
[INFO] Finished at: 2015-06-30T14:04:55+08:00
[INFO] Final Memory: 23M/244M
[INFO] ------------------------------------------------------------------------
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1
Choose org.apache.maven.archetypes:maven-archetype-plugin version:
1: 1.0
2: 1.1
3: 1.2
Choose a number: 3:
Define value for property 'groupId': : com.lewi
Define value for property 'artifactId': : plugin
Define value for property 'version':  1.0-SNAPSHOT: : 1.0
Define value for property 'package':  com.lewi: :
Confirm properties configuration:
groupId: com.lewi
artifactId: plugin
version: 1.0
package: com.lewi
 Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: maven-archetype-plugin:1.2
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.lewi
[INFO] Parameter: artifactId, Value: plugin
[INFO] Parameter: version, Value: 1.0
[INFO] Parameter: package, Value: com.lewi
[INFO] Parameter: packageInPathFormat, Value: com/lewi
[INFO] Parameter: package, Value: com.lewi
[INFO] Parameter: version, Value: 1.0
[INFO] Parameter: groupId, Value: com.lewi
[INFO] Parameter: artifactId, Value: plugin
[INFO] project created from Archetype in dir: D:\test\plugin
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 03:22 min
[INFO] Finished at: 2015-06-30T14:01:30+08:00
[INFO] Final Memory: 16M/244M
[INFO] ------------------------------------------------------------------------
在目录D:\test\plugin下面,可以看到生成成功的pom.xml和src文件夹。

2 进入D:\test\plugin目录,进行项目的编辑和安装。
运行mvn install,可以查看到安装成功。
[INFO] --- maven-install-plugin:2.4:install (default-install) @ plugin ---
[INFO] Installing D:\test\plugin\target\plugin-1.0.jar to C:\Users\lyue\.m2\repository\com\lewi\plugin\1.0\plugin-1.0.jar
[INFO] Installing D:\test\plugin\pom.xml to C:\Users\lyue\.m2\repository\com\lewi\plugin\1.0\plugin-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.674 s
[INFO] Finished at: 2015-06-30T14:04:55+08:00
[INFO] Final Memory: 23M/244M
[INFO] ------------------------------------------------------------------------
同时可以看到生生成的目录:D:\test\plugin\target和里面package的jar和 compile的 class

3 查看新plugin是否可以工作
根据上面的提示或者查看pom.xml文件,我们创建了一个groupId为com.lewi,artifactID为plugin,version为1.0的maven-plugin。
查看本项目的src\main\java\com\lewi\MyMojo.java文件,可以看到此Mojo名为 touch,操作为创建一个新的touch.txt文件,并在文件中写入“touch.txt”.
运行如下命令: 
mvn com.lewi:plugin:1.0:touch (version 1.0可以省去)
运行结束后,查看在target文件夹是否新建了文件touch.txt。

4 在pom.xml里面,看到还有一个run-its的profile。此profile中,加入了一个maven-invoker-plugin用来测试上面生产的touch goal。
使用如下命令尝试运行此profile.
mvn verify -P run-its
出现如下错误:
Maven invocation failed. Error configuring command-line. Reason: Maven executable not found at: D:\webServers\apache-maven-3.3.3\bin\mvn.bat
Google下,发现是新版maven不兼容maven-invoker-plugin。新版的maven安装目录下,mvn主程序名字更改成了maven.cmd,但是maven-invoker-plugin还在寻找maven.bat。(Detail: https://issues.apache.org/jira/browse/MNG-5792)

Reference:
First Mojo: http://maven.apache.org/guides/plugin/guide-java-plugin-development.html
Archetypes: http://maven.apache.org/guides/introduction/introduction-to-archetypes.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值