声明:仅限于将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
原文地址:https://www.cnblogs.com/tele-share/p/9945753.html
时间: 2024-10-09 12:00:34