Maven2 plugin開發(fā)教程詳解
首先,創(chuàng)建項(xiàng)目,創(chuàng)建一個(gè)文件夾:mkdir yakov
進(jìn)入yakov目錄,然后創(chuàng)建一個(gè)pom.xml:touch pom.xml,這個(gè)xml文件的結(jié)構(gòu)會(huì)在另外的章節(jié)詳細(xì)說一下。
使用vi編輯pom.xml,寫入基本的項(xiàng)目信息,如下圖:
單單是這些還是不夠的,接下來需要,配置一些私服和集成。
注:上面的version改為3.0
有關(guān)的私服和集成服務(wù)在上一篇中寫過:http://www.cnblogs.com/yakov/archive/2011/11/19/maven2_shi_jian.html
設(shè)置Maven從Nexus私服下載構(gòu)件
可以設(shè)置某個(gè)項(xiàng)目從私服下載,設(shè)置項(xiàng)目的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>
但是這需要為每個(gè)項(xiàng)目配置一下,有可能你僅僅需要為你開發(fā)的所有項(xiàng)目都用這同一個(gè)私服,那么很好,settings.xml提供了profile來設(shè)置:
<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>
上面的配置是針對(duì)下載構(gòu)件的,如果所有的下載都從私服上進(jìn)行,就需要配置鏡像了!如下所示:
<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>
以上幾個(gè)任選一種就可以了,我這里使用了最后一種。
部署自己的構(gòu)件至Nexus
直接在要部署的項(xiàng)目的pom.xml中寫入如下代碼:
還需要在settings.xml中設(shè)置用戶名和密碼,因?yàn)镹exus的倉庫對(duì)于匿名用戶是readonly的:
至此,有關(guān)私服已經(jīng)設(shè)置好了!
在目錄src/main/java下編寫plugin
在yakov下創(chuàng)建src/main/java目錄
寫一個(gè)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; } }
然后運(yùn)行mvn clean compile,運(yùn)行結(jié)果如下:
編譯完成,這里可移執(zhí)行安裝了,事實(shí)上,還應(yīng)該有對(duì)應(yīng)的測試代碼,以后再講。
運(yùn)行mvn clean install完后就安裝成功了。
最后運(yùn)行mvn clean deploy 完成發(fā)布,查看Nexus如下:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于SpringBoot實(shí)現(xiàn)動(dòng)態(tài)配置數(shù)據(jù)庫的加載
這篇文章主要介紹了Spring?Boot?如何動(dòng)態(tài)配置數(shù)據(jù)庫的加載,現(xiàn)項(xiàng)目有一個(gè)需求,期望通過在application.yml配置文件中設(shè)置一個(gè)開關(guān),來決定是否加載數(shù)據(jù)庫,文中通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-10-10Java Web監(jiān)聽器如何實(shí)現(xiàn)定時(shí)發(fā)送郵件
這篇文章主要介紹了Java Web監(jiān)聽器如何實(shí)現(xiàn)定時(shí)發(fā)送郵件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12Java AQS中CyclicBarrier回環(huán)柵欄的使用
這篇文章主要介紹了Java中的 CyclicBarrier詳解,CyclicBarrier沒有顯示繼承哪個(gè)父類或者實(shí)現(xiàn)哪個(gè)父接口, 所有AQS和重入鎖不是通過繼承實(shí)現(xiàn)的,而是通過組合實(shí)現(xiàn)的,下文相關(guān)內(nèi)容需要的小伙伴可以參考一下2023-02-02SpringBoot異步實(shí)現(xiàn)的8種方式
異步執(zhí)行對(duì)于開發(fā)者來說并不陌生,在實(shí)際的開發(fā)過程中,很多場景多會(huì)使用到異步,本文主要介紹了SpringBoot異步實(shí)現(xiàn)的8種方式,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09Spring在多線程環(huán)境下如何確保事務(wù)一致性問題詳解
這篇文章主要介紹了Spring在多線程環(huán)境下如何確保事務(wù)一致性問題詳解,說到異步執(zhí)行,很多小伙伴首先想到Spring中提供的@Async注解,但是Spring提供的異步執(zhí)行任務(wù)能力并不足以解決我們當(dāng)前的需求,需要的朋友可以參考下2023-11-11VSCode?配置?Spring?Boot?項(xiàng)目開發(fā)環(huán)境的全過程
兩三年前曾經(jīng)試過配置Java環(huán)境, 存在不少問題作罷. 最近搜了下相關(guān)的文章, 感覺VSCode對(duì)Java項(xiàng)目的支持比三年前完善了不少. 今天實(shí)際配置了一下環(huán)境, 把自己常用的功能過了一遍, 基本能跑通開發(fā)流程, 做個(gè)筆記,需要的朋友可以參考下2024-03-03