maven 自定义插件开发及使用

  • Post author:
  • Post category:其他


mave自定义插件

介绍

本文采用maven自定义插件实现了解决html引入css、js缓存的问题。问题解决思路为扫描路径下的html文件,并将html文件中引入的css、js路径后缀加入版本号的方案。如将

https://code.jquery.com/jquery-3.2.1.slim.min.js

变更为

https://code.jquery.com/jquery-3.2.1.slim.min.js?v=1558055492531

要实现自定义插件的开发,基本步骤为:

  • pom文件中加入配置


    <packaging>maven-plugin</packaging>,


    并引入依赖

    maven-plugin-api



    maven-plugin-annotations
  • 新建类继承


    extends AbstractMojo,


    并在类头加入注解

    @Mojo
  • 如需定义参数,类中参数变量上加入

    @Parameter

    注解
  • 开发完成后,使用 mvn install命令将自定义插件加载到本地仓库。


demo部分源码


pom文件配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yanmei.study</groupId>
    <artifactId>yanmei-maven-plugin</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--自定义插件开发需加入配置-->
    <packaging>maven-plugin</packaging>

    <dependencies>
       <!--自定义插件开发需引入依赖 -->
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.0</version>
        </dependency>
        <!--自定义插件开发需引入依赖 -->
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.3</version>
            <scope>provided</scope>
        </dependency>
        <dependency> <!-- 采用jsoup包来处理html页面-->
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.11.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <version>1.5.7.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.11.RELEASE</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

新建工程,主类代码为

package com.yanmei.study.maven.plugin;

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 org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;

@Mojo(name="yanmeiplugin", defaultPhase = LifecyclePhase.PACKAGE)
public class TestDemo extends AbstractMojo{
    @Parameter
    private String dirPath;
    @Parameter
    private Boolean skip=false;

    public void execute() throws MojoExecutionException, MojoFailureException {
        if(skip == true){
            System.out.println("skip is true");
            return;
        }
        if(dirPath == null || dirPath.equals("")){
            System.out.println("the param can't be empty");
            return;
        }
        System.out.println("the param is "+dirPath);
        File file = new File(dirPath);
        findFile(file);
    }
   // 查找文件
    private void findFile(File files){
        if(!files.exists()) {
            System.out.println("the file or the dir is not exist!");
            return;
        }

        File[] childFiles = files.listFiles();
        if(childFiles == null || childFiles.length<=0){
            System.out.println("the file or the dir is not exist");
            return;
        }
        for(File file: childFiles){
            if(file.isDirectory()){
                findFile(file);
            }else {
                // 处理文件
                processFile(file);
            }

        }

    }
    // 对找到的html文件进行处理
    private void processFile(File file) {
        String name = file.getName();
        if (name.endsWith(".html")) {
            Document document = null;
            try {
                document = Jsoup.parse(file, "utf-8");
                System.out.println("find html file--" + name + "-the dir is-"+file.getPath());
                processTag(document,"link","href");
                processTag(document,"script","src");
                FileOutputStream fos = null;
                fos = new FileOutputStream(file, false);
                OutputStreamWriter osw = null;
                osw = new OutputStreamWriter(fos, "utf-8");
                osw.write(document.html());
                osw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    // 对找到的属性值进行链接后面加入版本号的处理
    private void processTag(Document document,String tag,String attr) {
        Elements elements = document.getElementsByTag(tag);
        for (Element element : elements) {
            String attrValue = element.attr(attr);
            attrValue += "?v=" + System.currentTimeMillis();
            System.out.println(attrValue);
            element.attr(attr,attrValue);
        }
    }
}

最后执行 mvn install,将自定义的插件保存到本地仓库。

调用自定义插件

调用自定义插件时,先在pom文件中引入定义插件,然后执行mvn install执行即可。部分代码如下:

pom文件配置

<build>
    <plugins>
        <plugin>
            <groupId>com.yanmei.study</groupId>
            <artifactId>yanmei-maven-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
            <configuration>
                <dirPath>${project.basedir}\src\main\resources</dirPath>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>yanmeiplugin</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

之后执行mvn clean install 或者mvn install即可。

demo源码地址:

https://github.com/xiaoyuer2012/projectmanager

常见maven插件

记录几个常见的maven插件



版权声明:本文为yanmei_yaomy原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。