将本地jar安装到本地mvn仓库

声明:仅限于将maven Repository下载的jar(使用maven打包的jar)安装到本地的maven仓库中,不保证全部成功,最初的时候添加依赖发现下载始终不成功,只能手动下载,但是手动下载完毕后,只能通过mvn install:install-file -Dfile=..这种方式安装jar包到仓库,实在是太过繁琐,仔细观察jar包后发现jar的坐标信息很容易从jar名称已经jar内部的pom.properties文件获得,代码如下

1packageinstallJarToMVN;23importjava.io.BufferedReader;4importjava.io.File;5importjava.io.IOException;6importjava.io.InputStream;7importjava.io.InputStreamReader;8importjava.util.Enumeration;9importjava.util.jar.JarEntry;10importjava.util.jar.JarFile;11importjava.util.zip.ZipEntry;1213/**14 * 读取jar包内的pom.properties 获得groupid15 * version,artifactId可以从jar包名称获取,也可以从pom.properties获取16*17*@authorTele18*19*/2021publicclass InstallJar {22//默认jar包路径,填写到目录23privatestatic String jarPath ="d:/jartoMVN/";24privatestatic BufferedReader reader;25publicstaticvoid main(String[] args) {2627if (args.length > 0) {28if (args[0] != null && args[0].trim().length() > 0) {29 jarPath = args[0];30}31}3233 File dir = newFile(jarPath);34if(!dir.exists()) {35thrownewRuntimeException("jar包目录不存在!");36}else{37if(!dir.isDirectory()) {38thrownewRuntimeException("输入的参数必须为jar包所在目录!");39}else{40 File[] listFiles =dir.listFiles();41if (listFiles.length == 0) {42thrownewRuntimeException("当前目录下没有文件");43}4445 String[] params = newString[4];46//遍历47for(int i = 0; i < listFiles.length; i++) {48 File jarFile =listFiles[i];4950//过滤非jar文件51if(!jarFile.getName().contains(".jar")) {52continue;53}5455// 去除后缀,jar的名字可能含有多个".",hadoop-yarn-server-applicationhistoryservice-3.1.1.jar56 String jarName =jarFile.getName();57//保留原始的jar名称58 String orginalName =jarName;5960//hadoop-yarn-server-applicationhistoryservice-3.1.161 jarName = jarName.substring(0, jarName.lastIndexOf("."));6263//获得artifactId64 String artifactId = jarName.substring(0, jarName.lastIndexOf("-"));6566//获得版本号67 String version = jarName.substring(jarName.lastIndexOf("-") + 1);6869//获得groupId7071//拼接的完整路径72 String groupId = readPomproperties(jarPath +orginalName);73if (groupId == null) {74thrownewRuntimeException("获取groupId失败");75}76 groupId = groupId.split("=")[1];7778//封装参数79 params[0] = jarPath +orginalName;80 params[1] =groupId;81 params[2] =artifactId;82 params[3] =version;8384install(params);8586}8788}8990}9192}939495/**96*97*@param path groupId=org.apache.hadoop98*@return获得groupId,在pom.properties文件的第四行99*/100publicstatic String readPomproperties(String path) {101 JarFile jarFile = null;102 String groupId = null;103//groupId在第四行104int number = 4;105try{106 jarFile = newJarFile(path);107 Enumeration<JarEntry> entries =jarFile.entries();108while (entries.hasMoreElements()) {109 JarEntry jarEntry =entries.nextElement();110111 String name =jarEntry.getName();112113if(name.contains("pom.properties")) {114 reader = newBufferedReader(new InputStreamReader(jarFile.getInputStream(jarEntry),"utf-8"));115 String line ="";116117//计行数118int count = 0;119120while ((line = reader.readLine()) != null) {121122count++;123if (count == 4) {124 groupId =line;125}126}127128}129}130131}catch (IOException e) {132// TODO Auto-generated catch block133e.printStackTrace();134}finally{135if (reader != null) {136try{137reader.close();138}catch (IOException e) {139// TODO Auto-generated catch block140e.printStackTrace();141}142}143if (jarFile != null) {144try{145jarFile.close();146}catch (IOException e) {147// TODO Auto-generated catch block148e.printStackTrace();149}150}151}152returngroupId;153}154155//执行安装命令156publicstaticvoid 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";160161 Runtime rt =Runtime.getRuntime();162//执行安装163System.out.println(order);164 Process p;165try{166 p = rt.exec("cmd.exe /c"+""+order);167168 reader = newBufferedReader(newInputStreamReader(p.getInputStream()));169 String line;170//输出进程171while ((line = reader.readLine()) != null) {172System.out.println(line);173}174175if (reader != null) {176reader.close();177}178179//waitFor()是阻塞方法,等待外部命令执行结束180p.waitFor();181182p.destroy();183 p = null;184185}catch (IOException e) {186// TODO Auto-generated catch block187e.printStackTrace();188}catch (InterruptedException e) {189// TODO Auto-generated catch block190e.printStackTrace();191}192193}194195}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwdzyw.html