• 一款清理本地仓库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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
       <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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    7月底截止截止!汉阳区2022年度总部企业申报条件、原则和材料
    小红书达人笔记的优势以及达人笔记形式有哪些
    虚拟机的网络模式
    数据加密和BCrypt哈希算法应用 | StartDT Tech Lab 15
    什么是 ping (ICMP) 洪水 DDOS 攻击?
    【Linux学习】基础IO
    Tomcat 漏洞处理
    2022-Docker常用命令
    Android 获取版本号
    系列七、栈 & 堆
  • 原文地址:https://blog.csdn.net/qq_37148232/article/details/133298543