自定义maven插件

1、pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!--打包方式   这里区别于普通maven项目-->
    <packaging>maven-plugin</packaging>

    <groupId>cn.com.test</groupId>
    <artifactId>encrytfilepack</artifactId>
    <version>0.0.1-SNAPSHOT</version>


    <dependencies>

        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>2.3.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.3.4.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-plugin-plugin</artifactId>
                <version>3.5.2</version>
                <!-- 插件执行命令前缀 -->
                <configuration>
                    <goalPrefix>encrypt</goalPrefix>
                    <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

 

具体实现:这个方法测试的是给具体文件进行加密操作,也可以自定义。

package cn.com.test.util;

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.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;


/**
 * author scl
 * date 2020/10/28 0028 14:36
 */
// mojo注解就是maven插件的注解,具体什么我忘记了。name就是后面使用该插件的时候excuation里面的,
// 后面配置的是生命周期,我这里配置了install,即默认是安装时候执行本插件(这个可以在pom文件指定)
 @Mojo(name = "encryptresource", defaultPhase = LifecyclePhase.INSTALL) public class EncryptResource extends AbstractMojo { private final String pre_path = "src/main/resources/static/";

  
   // 配置的是本maven插件的位置,在pom使用configuration标签进行配置property就是名字 @Parameter(property = "sourceFolderPath") String sourceFolderPath; public void execute() throws MojoExecutionException, MojoFailureException { System.out.println("this is encrypt file logs!"); System.out.println(sourceFolderPath); //对path进行加密 if (sourceFolderPath.contains(",")) { String[] split = sourceFolderPath.split(","); encryptFile(split); } else { encryptFile(sourceFolderPath); } } private void encryptFile(String... path) { String[] strArr = path; for (int i = 0; i < strArr.length; i++) { String path1 = ""; try { path1 = ResourceUtils.getURL(pre_path + strArr[i]).getPath(); File file = new File(path1); System.out.println(file.getAbsolutePath()); System.out.println(file.getName()); EncyptUtil.Base64Encryt(file, file); System.out.println("[INFO]:----------"); System.out.println(strArr[i] + ":--success--"); } catch (IOException e) { System.out.println("[INFO]:----------"); e.printStackTrace(); System.out.println(strArr[i] + "--fail"); continue; } } } }

 

然后执行:maven clean install

把插件打到本地仓库。

 

 

另一个项目引用插件:

pom文件:

<!--加密过程-->
            <plugin>
                <groupId>cn.com.test</groupId>
                <artifactId>encrytfilepack</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>encryptresource</goal>
                        </goals>
               <!--重要点-->  <phase>package</phase> </execution> </executions> <configuration> <sourceFolderPath>blue.svg</sourceFolderPath> </configuration> </plugin>

 

执行package即可看到日志输出。

 

 

ps:还遇到一个关于maven仓库的问题:

maven  Error extracting plugin descriptor: 'invalid distance too far back'

有关是因为maven仓库的包不完整,把maven仓库指定的包删掉再从新下载即可;删掉了org下的,现在还在下载中,哭了

 

 

ps2:

maven生命周期:

其中,这边的需求是在项目打包之前对指定文件进行加密处理,自己在按照上面的插件写完之后,执行完package,但是本地文件进行加密了,但是打包完的文件并没有加密,根据日志定位出来是maven的插件执行顺序问题,因为都是package格式,所以实在是找不到怎么定义顺序,自定义的总是在打包完执行;所以通过看https://zhuanlan.zhihu.com/p/139485157的相关,把自定义的格式改为compile,这样可定在打包之前执行。

 

posted @ 2020-10-30 13:42  忧伤还是快乐EL  阅读(250)  评论(0编辑  收藏  举报