一款清理本地仓库jar包的maven插件

项目中,需要强制更新业务jar包。以达到同一个jar包版本更新代码的目地。因此有了如下插件,原理也简单,读到仓库地址,直接删jar文件。主打一个简单粗爆有效。

package com.xxx.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 java.io.File;
import java.util.ArrayList;
import java.util.List;

@Mojo(name = "clearModel", defaultPhase = LifecyclePhase.PRE_CLEAN)
public class BusinessModelClear extends AbstractMojo {

    private final static String PUBLIC_GROUP_ID = "/xxx/";

    @Parameter(defaultValue = "${settings.localRepository}", readonly = true, required = true)
    private String localMavenRepository;
    
    // 清项目groupId下面的所有jar包
    @Parameter(required = false)
    private List<String> clearArtifactIds;
    
    // 指定清哪些个
    @Parameter(required = false, defaultValue = "false")
    private String clearAll;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        if ("true".equals(clearAll)) {
            clearDependences(null);
            return;
        }else if(null!=clearArtifactIds){
               clearByArtifactIds(clearArtifactIds);
        }
    }

    private void clearByArtifactIds(List<String> artifactIds) {
        if (null != artifactIds && artifactIds.size() > 0) {
            for (String clearArtifactId : artifactIds) {
                clearDependences(clearArtifactId);
            }
        }
    }

    private void clearDependences(String artifactId) {
        if (null != artifactId) {
            String jarFileForld = localMavenRepository + PUBLIC_GROUP_ID + artifactId;
            this.getLog().info("zysoft current project clear dependency--> [" + jarFileForld + "]");
            deleteFile(new File(jarFileForld));
        } else {
            String jarFileForld = localMavenRepository + PUBLIC_GROUP_ID;
            this.getLog().info("zysoft current project clear dependency--> begin to clear com.zysoft.future all jars");
            deleteFile(new File(jarFileForld));
        }
    }

    private void deleteFile(File file) {
        if (!file.exists()) {// 判断文件是否存在
            return;
        }
        
        File[] files = file.listFiles();
        if (null == files || files.length == 0) {
        	file.delete();
            return;
        }
        
        for (File newfile : files) {// 遍历文件夹下的目录
            if (newfile.isFile()) {// 如果是文件而不是文件夹==>可直接删除
                newfile.delete();
            } else {
                deleteFile(newfile);// 是文件夹,递归调用方法
            }
        }
       file.delete();
    }
}
   <build>
        <plugins>
            <plugin>
                <groupId>com.zysoft</groupId>
                <artifactId>xxx-business-model-update</artifactId>
                <version>${xxx.clearmodel.plugin.version}</version>
                  <executions>
                        <execution>
                            <phase>clean</phase>
                            <goals>
                                <goal>clearModel</goal>
                            </goals>
                        </execution>
                    </executions>
                <configuration>
                    <clearAll>true</clearAll>
                </configuration>
            </plugin>
        </plugins>
    </build>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值