文章

Maven批量安装本地Jar文件小工具

Maven 批量安装本地 Jar文件到本地Maven库小程序。根据自己的需求临时开发完成。

使用方式:

  • config.properties中,配置待安装jar文件的存放路径。
  • 安装时groupId,artifactId,version等信息,根据jar文件的文件名获得。采用分割”-“的方式,解析出来。所以,推荐jar的文件名中含有两个”-“。例如:group-artifact-version.jar。如果为group-artifactversion.jar,则groupId=group,artifactId=version=artifactversion
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
public class Main { 
     
    private static final Log _log = LogFactory.getLog(Main.class); 
    private static PropertyHelper propHelper = new PropertyHelper("config"); 
    private static Runtime _runRuntime = Runtime.getRuntime(); 
    private static boolean isDelete = Boolean.valueOf(propHelper.getValue("delete-installed-jar")); 
    private static boolean isMove = Boolean.valueOf(propHelper.getValue("move-installed-jar")); 
    private static final String KEY_JARPATH = "jar-path"; 
    private static final String KEY_BACKUPPATH = "back-path"; 
    private static final String ENCODE = "gbk"; 
    private static final String INSTALL_PATH = propHelper.getValue(KEY_JARPATH); 
    private static  String CMD_INSTALL_FILE; 
    private static  String CMD_BACKUP_JAR; 
     
    public static void main(String[] args) { 
         
        _log.info("The path of the jars is ["+INSTALL_PATH+"]."); 
        File file = new File(INSTALL_PATH); 
        if(!file.isDirectory()){ 
            _log.warn("The path must be a directory."); 
            return; 
        } 
        FilenameFilter filter = new JarFilter(); 
        File[] jarFiles = file.listFiles(filter); 
        for(File jar: jarFiles){ 
            installJarToMaven(jar); 
            if(isDelete){ 
                _log.info("Delete the original jar file ["+jar.getName()+"]."); 
                jar.delete(); 
            }else{ 
                if(isMove){ 
                    String backupPath = propHelper.getValue(KEY_BACKUPPATH); 
                    backupJar(jar,file,backupPath); 
                } 
            } 
        } 
    } 
 
    private static void backupJar(File jar, File file, String backupPath) { 
        CMD_BACKUP_JAR = "copy "+INSTALL_PATH+File.separator+jar.getName()+" "+backupPath; 
        String[] cmds = new String[]{"cmd", "/C",CMD_BACKUP_JAR}; 
        try { 
            Process process =_runRuntime.exec(cmds,null,file); 
            printResult(process); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
            _log.info("The jar ["+jar.getName()+"]  is backup, it's will be deleted.\r"); 
            jar.delete(); 
    } 
 
    private static void installJarToMaven(File file) { 
        String fileName = file.getName(); 
        String jarName = getJarName(fileName); 
        String groupId=null; 
        String artifactId=null; 
        String version=null; 
        int groupIndex = jarName.indexOf("-"); 
        if(groupIndex==-1){ 
            version = artifactId = groupId = jarName; 
        }else{ 
            groupId = jarName.substring(0,groupIndex); 
            int versionIndex = jarName.lastIndexOf("-"); 
            if(groupIndex==versionIndex){ 
                version = artifactId = jarName.substring(versionIndex+1,jarName.length()); 
            }else{ 
                artifactId = jarName.substring(groupIndex+1,versionIndex); 
                version = jarName.substring(versionIndex+1,jarName.length()); 
            } 
        } 
        _log.info("Jar ["+jarName+"] will be installed with the groupId="+groupId+" ," 
                +"artifactId="+artifactId+" , version="+version+"."); 
        executeInstall( groupId,  artifactId,  version, file.getPath()); 
    } 
 
    private static void executeInstall(String groupId, String artifactId, 
            String version, String path) { 
        CMD_INSTALL_FILE = createInstallFileCMD( groupId,  artifactId, 
                 version,  path); 
        String[] cmds = new String[]{"cmd", "/C",CMD_INSTALL_FILE}; 
        try { 
            Process process = _runRuntime.exec(cmds); 
            printResult(process); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
    } 
     
    private static void printResult(Process process) throws IOException { 
        InputStream is = process.getInputStream(); 
        BufferedReader br = new BufferedReader(new InputStreamReader(is,ENCODE)); 
        String lineStr; 
        while((lineStr= br.readLine()) !=null){ 
            System.out.println(lineStr); 
        } 
    } 
 
    private static String createInstallFileCMD(String groupId, 
            String artifactId, String version, String path) { 
        StringBuffer sb = new StringBuffer(); 
        sb.append("mvn install:install-file -DgroupId=").append(groupId) 
            .append(" -DartifactId=").append(artifactId) 
            .append(" -Dversion=").append(version) 
            .append(" -Dpackaging=jar") 
            .append(" -Dfile=").append(path); 
        _log.debug(sb.toString()); 
        return sb.toString(); 
    } 
 
    private static String getJarName(String fileName) { 
        int index = fileName.indexOf(".jar"); 
        return fileName.substring(0, index); 
    } 
 
} 
1
2
3
4
5
6
7
8
9
10
11
12
13
public class PropertyHelper { 
     
    private ResourceBundle propBundle;  
     
    public PropertyHelper(String bundle){ 
        propBundle = PropertyResourceBundle.getBundle(bundle); 
    } 
     
    public  String getValue(String key){ 
        return this.propBundle.getString(key); 
    } 
 
} 

sourceforge地址:

本文由作者按照 CC BY 4.0 进行授权