Maven plugin 开发

Ant Maven2 plug-in 开发
 
Maven的一个很明显有别与Ant的优势就是在于明确了工程中所用资源包的版本信息,目前我们在工程开发过程中都不免需要用到大量的免费开源的第三方插件,而这些插件在升级过程中的向前兼容做的有时候确实不太理想,就想Hibernate在升级到3.0后包结构的变化一样,让很多开发人员在刚开始使用时都是一头雾水,但是由于Maven2可以明确指明所使用的资源包版本信息,这样就避免了工程中由于资源包版本混乱导致程序崩溃的问题。
 
Ant脚本的plug-in开发需要在POM中声明两个资源包,一个是在编译打包生成plug-in的jar文件时所需要用到的资源包,一个是在plug-in在被执行时,运行Ant脚本所需要的资源包。具体POM的配置如下所示:
可以看出我这里并没有对Maven2的路径做任何显示的声明。根据Maven2的标准路径,我们需要将plug-in工程中的脚本文件放在src/main/scripts路径下去。所以,首先在工程中建立这样一个目录,然后在目录中开始编写Ant脚本(hello.build.xml):
接下来编写MOJO脚本(hello.mojos.xml),MOJO是Ant脚本执行的关键,我们也需要将其与Ant脚本放置在同一个目录src/main/scripts下,在该文件中定义Maven2的goal和需要执行的Ant脚本中target名称。具体内容如下:
至此,一个完整的基于Maven2的Ant plug-in就已经开发完毕。为了方便举例,我在这里的Ant脚本写得相当简单,你可以在这里根据你的工程具体需求尽情发挥。
 
 
接下来执行一下“mvn install”,提示内容如下则说明已经成功发布到了本地仓库中:
D:/eclipse/workspace/hello-plugin>mvn install -e -o
+ Error stacktraces are turned on.
[INFO]
NOTE: Maven is executing in offline mode. Any artifacts not already in your local
repository will be inaccessible.
[INFO] Scanning for projects...
[INFO] ----------------------------------------------------------------------------
[INFO] Building Hello Plugin
[INFO]    task-segment: [install]
[INFO] ----------------------------------------------------------------------------
[INFO] [plugin:descriptor]
[WARNING] Goal prefix is: hello; Maven currently expects it to be hello
[INFO] Using 3 extractors.
[INFO] Applying extractor for language: java
[INFO] Extractor for language: java found 1 mojo descriptors.
[INFO] Applying extractor for language: bsh
[INFO] Extractor for language: bsh found 0 mojo descriptors.
[INFO] Applying extractor for language: ant
[INFO] Extractor for language: ant found 1 mojo descriptors.
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
[INFO] No sources to compile
[INFO] [surefire:test]
[INFO] No tests to run.
[INFO] [jar:jar]
[INFO] Building jar: D:/eclipse/workspace/hello-plugin/target/hello-plugin-1.0-S
NAPSHOT.jar
[INFO] [plugin:addPluginArtifactMetadata]
[INFO] [install:install]
[INFO] Installing D:/eclipse/workspace/hello-plugin/target/hello-plugin-1.0-SNAP
SHOT.jar to E:/maven-2.0.4/.m2/repository/org/myproject/plugins/hello-plugin/1.0
-SNAPSHOT/hello-plugin-1.0-SNAPSHOT.jar
[INFO] [plugin:updateRegistry]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 seconds
[INFO] Finished at: Thu Jun 28 10:39:02 CST 2007
[INFO] Final Memory: 9M/17M
[INFO] ------------------------------------------------------------------------
 
D:/eclipse/workspace/hello-plugin>
接下来就可以在你的工程中将该插件集成上去了,具体做法为将如下代码加入你工程的POM中:
当然,Maven2对于Ant脚本的支持也并不是仅仅只到了插件级,你还可以在POM中直接使用Ant脚本来完成一些任务。为了保证本篇文章的完整性,关于这个专题我打算再开一篇文章专门说明。
 
 
 
Java Maven2 plug-in 开发
 
在已经建立的Ant plug-in工程中引入代码如下(对与Struts程序员来说,是不是有点似曾相识的感觉!):
其中AbstractMojo和MojoExecutionException都是Maven2核心支持包中的类,你可以在Maven2安装路径的lib目录下找到maven-core-2.0.6-uber.jar(我用的是version2.0.6,不过现在我在官方网站看到2.0.7也已经出来了)
重新编译,重新install。然后新建工程,以便测试插件是否能够正常工作。
新工程的POM如下:
执行命令,运行结果如下则说明插件开发成功!
E:/myproj/mvnproj>mvn package hello:hello hello:sayhi -e -o
+ Error stacktraces are turned on.
[INFO]NOTE: Maven is executing in offline mode. Any artifacts not already in your local
repository will be inaccessible.
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'hello'.
[INFO] ----------------------------------------------------------------------------
[INFO] Building mvnproj
[INFO]    task-segment: [package]
[INFO] ----------------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test]
[INFO] Surefire report directory: E:/myproj/mvnproj/target/surefire-reports
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.danlley.com.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.047 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] [jar:jar]
[INFO] Building jar: E:/myproj/mvnproj/target/mvnproj-1.0-SNAPSHOT.jar
[INFO] ----------------------------------------------------------------------------
[INFO] Building mvnproj
[INFO]    task-segment: [hello:hello] (aggregator-style)
[INFO] ----------------------------------------------------------------------------
[INFO] [hello:hello]
hello:
     [echo] ANT SCRIPT ECHOS:Hello, World
[INFO] ----------------------------------------------------------------------------
[INFO] Building mvnproj
[INFO]    task-segment: [hello:sayhi]
[INFO] ----------------------------------------------------------------------------
[INFO] [hello:sayhi]
[INFO] -------------------------------------------
[INFO] |              Hello, world.               |
[INFO] -------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Thu Jun 28 12:58:51 CST 2007
[INFO] Final Memory: 5M/10M
[INFO] ------------------------------------------------------------------------
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值