将本地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

原文地址:https://www.cnblogs.com/tele-share/p/9945753.html

时间: 2024-07-29 10:17:19

将本地jar安装到mvn仓库中的工具类的相关文章

本地jar包在maven工程中pom引用

背景 ??在使用Maven的过程中,经常碰到有些jar包在中央仓库没有的情况.如果公司有私服,那么就把jar包安装到私服上.如果没有私服,那就把jar包安装到本地Maven仓库.下面是如何把jar包导入本地maven仓库. 解决方法 1.确定包信息 groupId:设置项目代码的包名(一般用公司或组织名) artifactId:设置项目名或模块名 version:版本号 packaging:什么类型的文件(jar包) filePath:指定jar文件路径与文件名(同目录只需文件名) 2.在工程根

maven添加本地jar包到maven仓库

maven添加本地jar包到maven仓库mvn install:install-file -DgroupId=io.netty -DartifactId=netty-all -Dversion=5.0.0.Alpha1 -Dpackaging=jar -Dfile=G:/java/jar包/netty-all-5.0.0.Alpha1.jar 我下载的这个 jar 包是放到了 D:\mvn 目录下(D:\mvn\spring-context-support-3.1.0.RELEASE.jar)

手动将jar添加到maven仓库中

?? 1.将jar放到E:\workspace\lib中.如下图: 2.编写pom.xml文件,定义jfinal的坐标. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://

简单了解Spring中常用工具类_java - JAVA

文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 文件资源操作 Spring 定义了一个 org.springframework.core.io.Resource 接口,Resource 接口是为了统一各种类型不同的资源而定义的,Spring 提供了若干 Resource 接口的实现类,这些实现类可以轻松地加载不同类型的底层资源,并提供了获取文件名.URL 地址以及资源内容的操作方法 访问文件资源 * 通过 FileSystemResource 以文件系统绝对路径的

spring中常用工具类介绍

文件资源操作 Spring 定义了一个 org.springframework.core.io.Resource 接口,Resource 接口是为了统一各种类型不同的资源而定义的,Spring 提供了若干 Resource 接口的实现类,这些实现类可以轻松地加载不同类型的底层资源,并提供了获取文件名.URL 地址以及资源内容的操作方法 访问文件资源* 通过 FileSystemResource 以文件系统绝对路径的方式进行访问:* 通过 ClassPathResource 以类路径的方式进行访问

Java中的工具类和新特性

1:Collections集合框架工具类: /* 集合框架的工具类. Collections:集合框架的工具类.里面定义的都是静态方法. Collections和Collection有什么区别? Collection是集合框架中的一个顶层接口,它里面定义了单列集合的共性方法. 它有两个常用的子接口, List:对元素都有定义索引.有序的.可以重复元素. Set:不可以重复元素.无序. Collections是集合框架中的一个工具类.该类中的方法都是静态的 提供的方法中有可以对list集合进行排序

集合中的工具类Collections和Arrays

集合框架的工具类: Collections: 方法sort(): List<String> list = new ArrayList<String>();        list.add("gfhd");        list.add("abc");        list.add("a");        list.add("aaaadfs");sort(list);//按自然顺序排列,String

在Java中Arrays工具类实现功能的六种方法

使用Arrays工具类,要先导入包即:import.java.util.Arrays 以下是实现六种功能的方法: 1.比较两个数组值是否相等: 结果为true.false.(布尔型不能比较) int []a={10,20,30}; int []b={10,20,30}; int []c={1,2,3}; boolean isEqual=Arrays.equals(a,b); System.out.println(isEqual); System.out.println(Arrays.equals

13弹;集合中的工具类Collections和Arrays

集合框架的工具类: Collections: 方法sort() System.out.println(list); // 自然排序 Collections.sort(list); System.out.println(list); // 按照字符串长度排序,再自然排序 Collections.sort(list,new Compare()); System.out.println(list); binarySearch 方法 前提是此集合必须是有序的 返回插入位置 角标的负数再减一: fill方