自定义Maven插件实践(descriptor failed: 52264问题解决)

59 篇文章 8 订阅

大致步骤:

1.创建Maven工程

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

    <groupId>cn.surpass.maven</groupId>
    <artifactId>plugins</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>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.14.4</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>

        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-model</artifactId>
            <version>2.2.1</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>
</project>

2.编写实现类

2.1 此类实现org.apache.maven.plugin.AbstractMojo

2.2 添加注解 org.apache.maven.plugins.annotations.Mojo

2.3 重写execute方法

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
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.io.File;
import java.io.IOException;

/**
 * mavenplugins
 * cn.surpass.maven.plugin
 *
 * @author surpass
 * @date 2020/3/15
 */
@Mojo(name = "copyFile", defaultPhase = LifecyclePhase.PACKAGE)
@Slf4j
public class CopyFile extends AbstractMojo {

    @Parameter
    private String srcPath;

    @Parameter
    private String descPath;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        if (null == srcPath || "".equalsIgnoreCase(srcPath)) {
            throw new RuntimeException("源文件夹不能为空");
        }
        if (null == descPath || "".equalsIgnoreCase(descPath)) {
            throw new RuntimeException("目标文件夹不能为空");
        }
        File srcDir = new File(srcPath);

        if (!srcDir.exists()) {
            String msg = String.format("源文件夹%s不存在或者路径不合法", srcPath);
            throw new RuntimeException(msg);
        }

        File descDir = new File(descPath);

        if (!descDir.exists()) {
            descDir.mkdirs();
        }

        try {
            FileUtils.copyDirectory(srcDir, descDir);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }


    public String getSrcPath() {
        return srcPath;
    }

    public void setSrcPath(String srcPath) {
        this.srcPath = srcPath;
    }

    public String getDescPath() {
        return descPath;
    }

    public void setDescPath(String descPath) {
        this.descPath = descPath;
    }

}

3.打包,执行如下命令:

mvn install

4.使用

这里需要注意的configuration的srcPath\descPath,正是CopyFile 类的两个参数。

<?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>cn.surpass.maven</groupId>
    <artifactId>copyfile</artifactId>
    <version>1.0-SNAPSHOT</version>
	<build>
   		<plugins>
    	    <plugin>
    	        <groupId>cn.surpass.maven</groupId>
    	        <artifactId>plugins</artifactId>
    	        <version>1.0-SNAPSHOT</version>
    	        <configuration>
    	            <srcPath>F:\test\test</prefix>
    	            <descPath>F:\test2</surfix>
    	        </configuration>
    	        <executions>
    	            <execution>
    	                <phase>package</phase>
    	                <goals>
    	                    <goal>renName</goal>
    	                </goals>
    	            </execution>
    	        </executions>
    	    </plugin>
    	</plugins>
    </build>
</project>

接下来执行命令即可看到效果。

mvn package

问题解决

问题描述

在进行打包的时候,出现错误maven-plugin-plugin:3.2:descriptor failed: 52264。经过多次尝试发现,在这个工程中每个方法体重不能有其他除了set/get/execute方法,不能有其他的方法,另外不能有其他的类(包括工具类)。

解决方法

对于面向对象的java来说的确出现上诉问题的确很难受,把所有的逻辑放到一个方法中确实不合适,所以各种查询资料,发现了蛛丝马迹,也就是显示引入插件,即在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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.surpass.maven</groupId>
    <artifactId>plugins</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>maven-plugin</packaging>

    <dependencies>
       ...
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-plugin-plugin</artifactId>
                <version>3.6.0</version>
                <executions>
                    <execution>
                        <id>default-addPluginArtifactMetadata</id>
                        <phase>package</phase>
                        <goals>
                            <goal>addPluginArtifactMetadata</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-descriptor</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>descriptor</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

 

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

surpassLiang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值