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的倉(cāng)庫(kù)對(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)的測(cè)試代碼,以后再講。
運(yùn)行mvn clean install完后就安裝成功了。
最后運(yùn)行mvn clean deploy 完成發(fā)布,查看Nexus如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot 集成 Mybatis Plus 自動(dòng)填充字段的實(shí)例詳解
這篇文章主要介紹了Spring Boot 集成 Mybatis Plus 自動(dòng)填充字段,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
springboot整合redis過期key監(jiān)聽實(shí)現(xiàn)訂單過期的項(xiàng)目實(shí)踐
現(xiàn)在各種電商平臺(tái)都有自己的訂單過期時(shí)間設(shè)置,那么如何設(shè)置訂單時(shí)間過期呢,本文主要介紹了springboot整合redis過期key監(jiān)聽實(shí)現(xiàn)訂單過期的項(xiàng)目實(shí)踐,感興趣的可以了解一下2023-12-12
詳解SpringBoot多跨域請(qǐng)求的支持(JSONP)
跨域是很多項(xiàng)目需要遇到的文章,本篇文章主要介紹了詳解SpringBoot多跨域請(qǐng)求的支持(JSONP),具有一定的參考價(jià)值,有興趣的可以了解一下2017-04-04
基于Java方式實(shí)現(xiàn)數(shù)據(jù)同步
這篇文章主要為大家詳細(xì)介紹了基于Java方式實(shí)現(xiàn)數(shù)據(jù)同步,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Java讀取json數(shù)據(jù)并存入數(shù)據(jù)庫(kù)的操作代碼
很多朋友問大佬們JAVA怎么把json存入數(shù)據(jù)庫(kù)啊,這一問題就把我難倒了,糾結(jié)如何操作呢,下面小編把我的經(jīng)驗(yàn)分享給大家,感興趣的朋友一起看看吧2021-08-08
Java對(duì)世界不同時(shí)區(qū)timezone之間時(shí)間轉(zhuǎn)換的處理方法
這篇文章主要介紹了Java對(duì)世界不同時(shí)區(qū)timezone之間時(shí)間轉(zhuǎn)換的處理方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
使用@PathVariable注解如何實(shí)現(xiàn)動(dòng)態(tài)傳值
這篇文章主要介紹了使用@PathVariable注解如何實(shí)現(xiàn)動(dòng)態(tài)傳值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10

