Install local JAR file gadgets in batches using Maven

Source: Internet
Author: User

Maven batch installs local jar files to the local Maven library applet.

The development is completed on demand. Usage:

In config. properties, configure the storage path of the JAR file to be installed.

Information such as groupid, artifactid, and version during installation is obtained based on the JAR file name. It is parsed by dividing.

Therefore, the recommended JAR file name contains two "-". Example: group-artifact-version.jar.

If the group-artifactversion.jar is used, groupid = group, artifactid = version = artifactversion.

 

 

01.public class Main { 02.     03.    private static final Log _log = LogFactory.getLog(Main.class); 04.    private static PropertyHelper propHelper = new PropertyHelper("config"); 05.    private static Runtime _runRuntime = Runtime.getRuntime(); 06.    private static boolean isDelete = Boolean.valueOf(propHelper.getValue("delete-installed-jar")); 07.    private static boolean isMove = Boolean.valueOf(propHelper.getValue("move-installed-jar")); 08.    private static final String KEY_JARPATH = "jar-path"; 09.    private static final String KEY_BACKUPPATH = "back-path"; 10.    private static final String ENCODE = "gbk"; 11.    private static final String INSTALL_PATH = propHelper.getValue(KEY_JARPATH); 12.    private static  String CMD_INSTALL_FILE; 13.    private static  String CMD_BACKUP_JAR; 14.     15.    public static void main(String[] args) { 16.         17.        _log.info("The path of the jars is ["+INSTALL_PATH+"]."); 18.        File file = new File(INSTALL_PATH); 19.        if(!file.isDirectory()){ 20.            _log.warn("The path must be a directory."); 21.            return; 22.        } 23.        FilenameFilter filter = new JarFilter(); 24.        File[] jarFiles = file.listFiles(filter); 25.        for(File jar: jarFiles){ 26.            installJarToMaven(jar); 27.            if(isDelete){ 28.                _log.info("Delete the original jar file ["+jar.getName()+"]."); 29.                jar.delete(); 30.            }else{ 31.                if(isMove){ 32.                    String backupPath = propHelper.getValue(KEY_BACKUPPATH); 33.                    backupJar(jar,file,backupPath); 34.                } 35.            } 36.        } 37.    } 38. 39.    private static void backupJar(File jar, File file, String backupPath) { 40.        CMD_BACKUP_JAR = "copy "+INSTALL_PATH+File.separator+jar.getName()+" "+backupPath; 41.        String[] cmds = new String[]{"cmd", "/C",CMD_BACKUP_JAR}; 42.        try { 43.            Process process =_runRuntime.exec(cmds,null,file); 44.            printResult(process); 45.        } catch (IOException e) { 46.            e.printStackTrace(); 47.        } 48.            _log.info("The jar ["+jar.getName()+"]  is backup, it's will be deleted.\r"); 49.            jar.delete(); 50.    } 51. 52.    private static void installJarToMaven(File file) { 53.        String fileName = file.getName(); 54.        String jarName = getJarName(fileName); 55.        String groupId=null; 56.        String artifactId=null; 57.        String version=null; 58.        int groupIndex = jarName.indexOf("-"); 59.        if(groupIndex==-1){ 60.            version = artifactId = groupId = jarName; 61.        }else{ 62.            groupId = jarName.substring(0,groupIndex); 63.            int versionIndex = jarName.lastIndexOf("-"); 64.            if(groupIndex==versionIndex){ 65.                version = artifactId = jarName.substring(versionIndex+1,jarName.length()); 66.            }else{ 67.                artifactId = jarName.substring(groupIndex+1,versionIndex); 68.                version = jarName.substring(versionIndex+1,jarName.length()); 69.            } 70.        } 71.        _log.info("Jar ["+jarName+"] will be installed with the groupId="+groupId+" ," 72.                +"artifactId="+artifactId+" , version="+version+"."); 73.        executeInstall( groupId,  artifactId,  version, file.getPath()); 74.    } 75. 76.    private static void executeInstall(String groupId, String artifactId, 77.            String version, String path) { 78.        CMD_INSTALL_FILE = createInstallFileCMD( groupId,  artifactId, 79.                 version,  path); 80.        String[] cmds = new String[]{"cmd", "/C",CMD_INSTALL_FILE}; 81.        try { 82.            Process process = _runRuntime.exec(cmds); 83.            printResult(process); 84.        } catch (IOException e) { 85.            e.printStackTrace(); 86.        } 87.    } 88.     89.    private static void printResult(Process process) throws IOException { 90.        InputStream is = process.getInputStream(); 91.        BufferedReader br = new BufferedReader(new InputStreamReader(is,ENCODE)); 92.        String lineStr; 93.        while((lineStr= br.readLine()) !=null){ 94.            System.out.println(lineStr); 95.        } 96.    } 97. 98.    private static String createInstallFileCMD(String groupId, 99.            String artifactId, String version, String path) { 100.        StringBuffer sb = new StringBuffer(); 101.        sb.append("mvn install:install-file -DgroupId=").append(groupId) 102.            .append(" -DartifactId=").append(artifactId) 103.            .append(" -Dversion=").append(version) 104.            .append(" -Dpackaging=jar") 105.            .append(" -Dfile=").append(path); 106.        _log.debug(sb.toString()); 107.        return sb.toString(); 108.    } 109. 110.    private static String getJarName(String fileName) { 111.        int index = fileName.indexOf(".jar"); 112.        return fileName.substring(0, index); 113.    } 114. 115.} 01.public class PropertyHelper { 02.     03.    private ResourceBundle propBundle;  04.     05.    public PropertyHelper(String bundle){ 06.        propBundle = PropertyResourceBundle.getBundle(bundle); 07.    } 08.     09.    public  String getValue(String key){ 10.        return this.propBundle.getString(key); 11.    } 12. 13.} 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.