栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Maven2 plugin开发教程详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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如下:


...
  
    
      nexus
      Nexus
      http://202.117.15.193:8010/nexus/content/groups/public/
      true
      true
    
  
  
    
      nexus
      Nexus
      http://202.117.15.193:8010/nexus/content/groups/public/
      true
      true
    
  
...


但是这需要为每个项目配置一下,有可能你仅仅需要为你开发的所有项目都用这同一个私服,那么很好,settings.xml提供了profile来设置:


  ...
  
    
      nexus
      
 
   nexus
   Nexus
   http://202.117.15.193:8010/nexus/content/groups/public/
   true
   true
 
      
      
 
   nexus
   Nexus
   http://202.117.15.193:8010/nexus/content/groups/public/
   true
   true
 
      
    
  
  
    nexus
  
  ...

上面的配置是针对下载构件的,如果所有的下载都从私服上进行,就需要配置镜像了!如下所示:


  ...
  
    
      nexus
      *
      http://202.117.15.193:8010/nexus/content/groups/public/
    
  
  
    
      nexus
      
 
   central
   http://central
   true
   true
 
      
      
 
   central
   http://central
   true
   true
 
      
    
  
  
    nexus
  
  ...

以上几个任选一种就可以了,我这里使用了最后一种。

部署自己的构件至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;

public class YakovMojo extends AbstractMojo{
  private final String[] INCLUDES_DEFAULT={"java","xml","properties"};

  
  private File basedir;
  
  private File sourceDirectory;
  
  private File testSourceDirectory;
  
  private List resources;
  
  private List testResources;
  
  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 collected=new ArrayList();
    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 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如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/145476.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号