Maven2 plugin开发教程详解

 更新时间:2017年06月28日 11:46:59   作者:Yakov  
这篇文章主要为大家详细介绍了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如下:

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

相关文章

  • Java 8跳过本次循环,继续执行以及跳出循环,终止循环的代码实例

    Java 8跳过本次循环,继续执行以及跳出循环,终止循环的代码实例

    今天小编就为大家分享一篇关于Java 8跳过本次循环,继续执行以及跳出循环,终止循环的代码实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-10-10
  • javaweb上传下载实例完整版解析(下)

    javaweb上传下载实例完整版解析(下)

    这篇文章主要为大家详细解析了javaweb上传下载实例,本文重点在于文件下载功能的实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • Nacos1.4.0 Windows10单机模式启动和集群启动过程解析

    Nacos1.4.0 Windows10单机模式启动和集群启动过程解析

    这篇文章主要介绍了Nacos1.4.0 Windows10单机模式启动和集群启动,第一次使用nacos,废话不多说,记录下自己启动Nacos遇到的坑,感兴趣的朋友跟随小编一起看看吧
    2023-10-10
  • 一段代码搞懂关于Java中List、Set集合及Map的使用

    一段代码搞懂关于Java中List、Set集合及Map的使用

    这篇文章主要介绍了关于Java中List、Set集合及Map的使用及list,set和map三者的区别介绍,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-08-08
  • Java中的OkHttp使用教程

    Java中的OkHttp使用教程

    OkHttp是目前非常火的网络库,OKHttp与HttpClient类似,也是一个Http客户端,提供了对 HTTP/2 和 SPDY 的支持,并提供了连接池,GZIP 压缩和 HTTP 响应缓存功能,本文重点给大家介绍Java OkHttp使用,感兴趣的朋友一起看看吧
    2022-04-04
  • SpringBoot 项目中的图片处理策略之本地存储与路径映射

    SpringBoot 项目中的图片处理策略之本地存储与路径映射

    在SpringBoot项目中,静态资源存放在static目录下,使得前端可以通过URL来访问这些资源,我们就需要将文件系统的文件路径与URL建立一个映射关系,把文件系统中的文件当成我们的静态资源即可,本文给大家介绍SpringBoot本地存储与路径映射的相关知识,感兴趣的朋友一起看看吧
    2023-12-12
  • java wait()/notify() 实现生产者消费者模式详解

    java wait()/notify() 实现生产者消费者模式详解

    这篇文章主要介绍了java wait()/notify() 实现生产者消费者模式详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • 详解JAVA中接口的定义和接口的实现

    详解JAVA中接口的定义和接口的实现

    这篇文章主要介绍了JAVA中接口的定义和接口的实现,文中讲解非常细致,配合代码更好的帮大家学习参考,感兴趣的朋友可以了解下
    2020-06-06
  • Java动态循环队列是如何实现的

    Java动态循环队列是如何实现的

    今天带大家学习java队列的相关知识,文章围绕着如何实现Java动态循环队列展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06
  • java并发学习之BlockingQueue实现生产者消费者详解

    java并发学习之BlockingQueue实现生产者消费者详解

    这篇文章主要介绍了java并发学习之BlockingQueue实现生产者消费者详解,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11

最新评论