1. 程式人生 > >將jar安裝到本地mvn倉庫

將jar安裝到本地mvn倉庫

宣告:僅限於將maven Repository下載的jar(使用maven打包的jar)安裝到本地的maven倉庫中,不保證全部成功,最初的時候新增依賴發現下載始終不成功,只能手動下載,但是手動下載完畢後,只能通過mvn install:install-file -Dfile=..這種方式安裝jar包到倉庫,實在是太過繁瑣,仔細觀察jar包後發現jar的座標資訊很容易從jar名稱已經jar內部的pom.properties檔案獲得,程式碼如下

  1 package installJarToMVN;
  2 
  3 import java.io.BufferedReader;
  4
import java.io.File; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.InputStreamReader; 8 import java.util.Enumeration; 9 import java.util.jar.JarEntry; 10 import java.util.jar.JarFile; 11 import java.util.zip.ZipEntry; 12 13 /** 14 * 讀取jar包內的pom.properties 獲得groupid
15 * version,artifactId可以從jar包名稱獲取,也可以從pom.properties獲取 16 * 17 * @author Tele 18 * 19 */ 20 21 public class InstallJar { 22 // 預設jar包路徑,填寫到目錄 23 private static String jarPath = "d:/jartoMVN/"; 24 private static BufferedReader reader; 25 public static void main(String[] args) {
26 27 if (args.length > 0) { 28 if (args[0] != null && args[0].trim().length() > 0) { 29 jarPath = args[0]; 30 } 31 } 32 33 File dir = new File(jarPath); 34 if (!dir.exists()) { 35 throw new RuntimeException("jar包目錄不存在!"); 36 } else { 37 if (!dir.isDirectory()) { 38 throw new RuntimeException("輸入的引數必須為jar包所在目錄!"); 39 } else { 40 File[] listFiles = dir.listFiles(); 41 if (listFiles.length == 0) { 42 throw new RuntimeException("當前目錄下沒有檔案"); 43 } 44 45 String[] params = new String[4]; 46 // 遍歷 47 for (int i = 0; i < listFiles.length; i++) { 48 File jarFile = listFiles[i]; 49 50 // 過濾非jar檔案 51 if (!jarFile.getName().contains(".jar")) { 52 continue; 53 } 54 55 // 去除字尾,jar的名字可能含有多個 ".",hadoop-yarn-server-applicationhistoryservice-3.1.1.jar 56 String jarName = jarFile.getName(); 57 // 保留原始的jar名稱 58 String orginalName = jarName; 59 60 // hadoop-yarn-server-applicationhistoryservice-3.1.1 61 jarName = jarName.substring(0, jarName.lastIndexOf(".")); 62 63 // 獲得artifactId 64 String artifactId = jarName.substring(0, jarName.lastIndexOf("-")); 65 66 // 獲得版本號 67 String version = jarName.substring(jarName.lastIndexOf("-") + 1); 68 69 // 獲得groupId 70 71 // 拼接的完整路徑 72 String groupId = readPomproperties(jarPath + orginalName); 73 if (groupId == null) { 74 throw new RuntimeException("獲取groupId失敗"); 75 } 76 groupId = groupId.split("=")[1]; 77 78 // 封裝引數 79 params[0] = jarPath + orginalName; 80 params[1] = groupId; 81 params[2] = artifactId; 82 params[3] = version; 83 84 install(params); 85 86 } 87 88 } 89 90 } 91 92 } 93 94 95 /** 96 * 97 * @param path groupId=org.apache.hadoop 98 * @return 獲得groupId,在pom.properties檔案的第四行 99 */ 100 public static String readPomproperties(String path) { 101 JarFile jarFile = null; 102 String groupId = null; 103 // groupId在第四行 104 int number = 4; 105 try { 106 jarFile = new JarFile(path); 107 Enumeration<JarEntry> entries = jarFile.entries(); 108 while (entries.hasMoreElements()) { 109 JarEntry jarEntry = entries.nextElement(); 110 111 String name = jarEntry.getName(); 112 113 if (name.contains("pom.properties")) { 114 reader = new BufferedReader(new InputStreamReader(jarFile.getInputStream(jarEntry), "utf-8")); 115 String line = ""; 116 117 // 計行數 118 int count = 0; 119 120 while ((line = reader.readLine()) != null) { 121 122 count++; 123 if (count == 4) { 124 groupId = line; 125 } 126 } 127 128 } 129 } 130 131 } catch (IOException e) { 132 // TODO Auto-generated catch block 133 e.printStackTrace(); 134 } finally { 135 if (reader != null) { 136 try { 137 reader.close(); 138 } catch (IOException e) { 139 // TODO Auto-generated catch block 140 e.printStackTrace(); 141 } 142 } 143 if (jarFile != null) { 144 try { 145 jarFile.close(); 146 } catch (IOException e) { 147 // TODO Auto-generated catch block 148 e.printStackTrace(); 149 } 150 } 151 } 152 return groupId; 153 } 154 155 // 執行安裝命令 156 public static void install(String[] params) { 157 // 拼接命令 158 String order = "mvn install:install-file" + " -Dfile=" + params[0] + " -DgroupId=" + params[1] 159 + " -DartifactId=" + params[2] + " -Dversion=" + params[3] + " -Dpackaging=jar"; 160 161 Runtime rt = Runtime.getRuntime(); 162 // 執行安裝 163 System.out.println(order); 164 Process p; 165 try { 166 p = rt.exec("cmd.exe /c " + " " + order); 167 168 reader = new BufferedReader(new InputStreamReader(p.getInputStream())); 169 String line; 170 // 輸出程序 171 while ((line = reader.readLine()) != null) { 172 System.out.println(line); 173 } 174 175 if (reader != null) { 176 reader.close(); 177 } 178 179 // waitFor()是阻塞方法,等待外部命令執行結束 180 p.waitFor(); 181 182 p.destroy(); 183 p = null; 184 185 } catch (IOException e) { 186 // TODO Auto-generated catch block 187 e.printStackTrace(); 188 } catch (InterruptedException e) { 189 // TODO Auto-generated catch block 190 e.printStackTrace(); 191 } 192 193 } 194 195 }

測試結果:

1.eclipse中執行

2.打包成jar

3.可以指定目錄.預設的目錄為d:/jartoMVN/ ,直接拷貝路徑時記得加上 "/"

 jar包連結:https://files.cnblogs.com/files/tele-share/installjar2mvn.zip