了解Maven:Maven插件使用,Archetype制作及自定义插件

一、插件使用(举“栗子”)

插件地址:https://maven.apache.org/plugins/

仓库地址1:https://mvnrepository.com/artifact/org.codehaus.mojo/versions-maven-plugin/2.6

仓库地址2:https://maven.aliyun.com/mvn/search

1.tomcat插件使用

    点开插件地址,找到tomcat(可以Ctrl+F搜索一下),如图:

    点开Apached Tomcat Project,选择需要的版本,如图:

    不用看英文,直接看插件内容,如图有tomcat6和tomcat7,复制一个插件即可(下面还介绍了在settings.xml文件中配置tomcat,感兴趣可以看一下),如图:

    IDEA直接在Terminal运行:mvn tomcat7:run,使用eclipse,选择Run As---Maven build,在Goals运行tomcat7:run,如图:

2.versions插件使用

    插件地址没有找到versions插件引用(可能versions现在使用依赖了),可以去仓库地址查询:versions-maven-plugin,添加依赖:

<dependency>
    <groupId>org.codehaus.mojo</groupId>
	<artifactId>versions-maven-plugin</artifactId>
	<version>2.6</version>
        <scope>test</scope>
</dependency>

    运行:mvn versions:set -DnewVersion=新版本号,例如:versions:set -DnewVersion=1.0.0.SNAPSHOT

二、自定义插件(引用自https://blog.csdn.net/sdksdk0/article/details/80678434

1.基本使用

    新建一个maven项目,我这边jdk的build选择的是1.8,在pom.xml中添加

<packaging>maven-plugin</packaging>

    添加依赖

<dependencies>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>3.5.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugin-tools</groupId>
        <artifactId>maven-plugin-annotations</artifactId>
        <version>3.5</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

   java类:插件名为sogoucloud,支持msg,List数组和带参数的使用

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.util.List;


@Mojo(name="sogoucloud",defaultPhase= LifecyclePhase.PACKAGE)
public class SogoucloudMojo  extends AbstractMojo {

    @Parameter
    private String msg;

    @Parameter
    private List<String> options;

    @Parameter(property = "args")
    private String args;


    public void execute() throws MojoExecutionException,MojoFailureException{
        System.out.println("hello sogoucloud:"+msg);
        System.out.println("hello sogoucloud:"+options);
        System.out.println("hello sogoucloud:"+args);
    }

}

    打包 mvn clean  install ,安装到仓库,便于被其他项目引用

    新建另一个maven web项目,引入前面的插件

<build>
    <plugins>
        <plugin>
            <groupId>cn.sogoucloud</groupId>
            <artifactId>sogoucloud-maven-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <configuration>
                <msg>This is my message</msg>
                <options>
                    <option>one Array</option>
                    <option>two Array</option>
                </options>

            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>sogoucloud</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>


</build>

     使用,mvn install -Dargs=aaa,将这个maven项目install起来,就可以看到之前传的参数了

2.实战Archetype,生成骨架

    新建好骨架的maven工程之后,然后再项目路径下执行:mvn  archetype:create-from-project

   

    然后会在target目录中生成generated-sources\archetype,然后进入这个目录

    cd   D:\IntelWorkspace\sogoucloud-plugin-demo\target\generated-sources\archetype

    将这个骨架install到仓库中:mvn install 

    eclipse Window添加maven archetype的xml文件(在maven仓库的根目录下),以后创建项目可以选择自己添加的archetype。

    使用创建的骨架来使用:mvn archetype:generate -DarchetypeCatalog=local


    然后就可以在工作空间中生成以这个骨架为基础的项目工程。

3.案例实战:写一个maven插件统计当前工程里面有多少个java文件:

    java类

@Mojo(name="countFile",defaultPhase= LifecyclePhase.PACKAGE)
public class CountFileMojo extends AbstractMojo {

    @Parameter(property = "path")
    private String path;
    int num1 = 0;   //总文件数量
    int num2 = 0;   //目录数量
    int num3 = 0;   //java文件数量

    public void execute() throws MojoExecutionException,MojoFailureException{
        System.out.println(path);
        System.out.println(countFile(path));
    }

    public String  countFile(String dir){
        File f = new File(dir);
        File fs[] = f.listFiles();


        if(fs!=null){
            for(int i=0;i<fs.length;i++){
                File  currenFile = fs[i];
                if(currenFile.isFile()){
                    num1+=1;   //如果是文件就加1
                    String fileName = currenFile.getName();
                    String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
                    if(suffix.equals("java")){
                        num3+=1;
                    }
                }else{
                    //否则就是目录
                    num2+=1;
                    countFile(currenFile.getAbsolutePath());
                }
            }
        }
        return "Total number of File:"+num1+"||||The number of dir is:"+num2+"||||Total number of Java File:"+num3;
    }


}

    在另外一个工程中引用:

<plugin>
    <groupId>cn.sogoucloud</groupId>
    <artifactId>sogoucloud-maven-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>
    <configuration>
     
        <path>${project.basedir}</path>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <!--<goal>sogoucloud</goal>-->
                <goal>countFile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

    mvn clean package 查看结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值