根据自己的需求临时开发完成。使用方式:
在config.properties中,配置待安装jar文件的存放路径。
安装时groupId,artifactId,version等信息,根据jar文件的文件名获得。采用分割"-"的方式,解析出来。
所以,推荐jar的文件名中含有两个"-"。例如:group-artifact-version.jar。
如果为group-artifactversion.jar,则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.}