Maven2 plugin开发教程详解
首先,创建项目,创建一个文件夹:mkdir yakov
进入yakov目录,然后创建一个pom.xml:touch pom.xml,这个xml文件的结构会在另外的章节详细说一下。
使用vi编辑pom.xml,写入基本的项目信息,如下图:
单单是这些还是不够的,接下来需要,配置一些私服和集成。
注:上面的version改为3.0
有关的私服和集成服务在上一篇中写过:http://www.cnblogs.com/yakov/archive/2011/11/19/maven2_shi_jian.html
设置maven从nexus私服下载构件
可以设置某个项目从私服下载,设置项目的pom.xml如下:
<project> ... <repositories> <repository> <id>nexus</id> <name>nexus</name> <url>http://202.117.15.193:8010/nexus/content/groups/public/</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </repository> </repositories> <pluginrepositories> <pluginrepository> <id>nexus</id> <name>nexus</name> <url>http://202.117.15.193:8010/nexus/content/groups/public/</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginrepository> </pluginrepositories> ... </project>
但是这需要为每个项目配置一下,有可能你仅仅需要为你开发的所有项目都用这同一个私服,那么很好,settings.xml提供了profile来设置:
<settings> ... <profiles> <profile> <id>nexus</id> <repositories> <repository> <id>nexus</id> <name>nexus</name> <url>http://202.117.15.193:8010/nexus/content/groups/public/</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </repository> </repositories> <pluginrepositories> <pluginrepository> <id>nexus</id> <name>nexus</name> <url>http://202.117.15.193:8010/nexus/content/groups/public/</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginrepository> </pluginrepositories> </profile> </profiles> <activeprofiles> <activeprofile>nexus</activeprofile> </activeprofiles> ... </settings>
上面的配置是针对下载构件的,如果所有的下载都从私服上进行,就需要配置镜像了!如下所示:
<settings> ... <mirrors> <mirror> <id>nexus</id> <mirrorof>*</mirrorof> <url>http://202.117.15.193:8010/nexus/content/groups/public/</url> </mirror> </mirrors> <profiles> <profile> <id>nexus</id> <repositories> <repository> <id>central</id> <url>http://central</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </repository> </repositories> <pluginrepositories> <pluginrepository> <id>central</id> <url>http://central</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>true</enabled></snapshots> </pluginrepository> </pluginrepositories> </profile> </profiles> <activeprofiles> <activeprofile>nexus</activeprofile> </activeprofiles> ... </settings>
以上几个任选一种就可以了,我这里使用了最后一种。
部署自己的构件至nexus
直接在要部署的项目的pom.xml中写入如下代码:
还需要在settings.xml中设置用户名和密码,因为nexus的仓库对于匿名用户是readonly的:
至此,有关私服已经设置好了!
在目录src/main/java下编写plugin
在yakov下创建src/main/java目录
写一个yakovmojo的类:
import java.io.bufferedreader; import java.io.file; import java.io.filereader; import java.io.ioexception; import java.util.arraylist; import java.util.list; import org.apache.maven.model.resource; import org.apache.maven.plugin.abstractmojo; import org.apache.maven.plugin.mojoexecutionexception; import org.apache.maven.plugin.mojofailureexception; /** * * @author org.omylab.yakov * @goal yakov */ public class yakovmojo extends abstractmojo{ private final string[] includes_default={"java","xml","properties"}; /** * @parameter expression="${project.basedir}" * @required * @readonly */ private file basedir; /** * @parameter expression ="${project.build.sourcedirectory}" * @required * @readonly */ private file sourcedirectory; /** * @parameter expression ="${project.biuld.testsourcedirectory}" * @required * @readonly */ private file testsourcedirectory; /** * @parameter expression ="${project.build.resources}" * @required * @readonly */ private list<resource> resources; /** * @parameter expression "${project.build.testresources}" * @required * @readonly */ private list<resource> testresources; /** * the file types which will be included for counting * * @parameter */ private string[] includes; public void execute() throws mojoexecutionexception, mojofailureexception{ if(includes==null||includes.length==0){ includes=includes_default; } try{ countdir(sourcedirectory); countdir(testsourcedirectory); for(resource resource:resources){ countdir(new file(resource.getdirectory())); } for(resource resource:testresources){ countdir(new file(resource.getdirectory())); } }catch(ioexception e){ throw new mojoexecutionexception("unable to count lines of code.",e); } } private void countdir(file dir)throws ioexception{ if(!dir.exists())return; list<file> collected=new arraylist<file>(); collectfiles(collected,dir); int lines=0; for(file sourcefile:collected){ lines+=countline(sourcefile); } string path=dir.getabsolutepath().substring(basedir.getabsolutepath().length()); getlog().info(path+" : "+lines+" lines of code in "+collected.size()+" files"); } private void collectfiles(list<file> collected,file file){ if(file.isfile()){ for(string include:includes){ if(file.getname().endswith("."+include)){ collected.add(file); break; } } }else{ for(file sub:file.listfiles()){ collectfiles(collected,sub); } } } private int countline(file file)throws ioexception{ bufferedreader reader=new bufferedreader(new filereader(file)); int line =0; try{ while(reader.ready()){ reader.readline(); line++; } }finally{ reader.close(); } return line; } }
然后运行mvn clean compile,运行结果如下:
编译完成,这里可移执行安装了,事实上,还应该有对应的测试代码,以后再讲。
运行mvn clean install完后就安装成功了。
最后运行mvn clean deploy 完成发布,查看nexus如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Win10开启隐藏用户安装功能的方法
下一篇: C#多线程之Thread类详解