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

 package installJarToMVN;

 import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry; /**
* 读取jar包内的pom.properties 获得groupid
* version,artifactId可以从jar包名称获取,也可以从pom.properties获取
*
* @author Tele
*
*/ public class InstallJar {
// 默认jar包路径,填写到目录
private static String jarPath = "d:/jartoMVN/";
private static BufferedReader reader;
public static void main(String[] args) { if (args.length > 0) {
if (args[0] != null && args[0].trim().length() > 0) {
jarPath = args[0];
}
} File dir = new File(jarPath);
if (!dir.exists()) {
throw new RuntimeException("jar包目录不存在!");
} else {
if (!dir.isDirectory()) {
throw new RuntimeException("输入的参数必须为jar包所在目录!");
} else {
File[] listFiles = dir.listFiles();
if (listFiles.length == 0) {
throw new RuntimeException("当前目录下没有文件");
} String[] params = new String[4];
// 遍历
for (int i = 0; i < listFiles.length; i++) {
File jarFile = listFiles[i]; // 过滤非jar文件
if (!jarFile.getName().contains(".jar")) {
continue;
} // 去除后缀,jar的名字可能含有多个 ".",hadoop-yarn-server-applicationhistoryservice-3.1.1.jar
String jarName = jarFile.getName();
// 保留原始的jar名称
String orginalName = jarName; // hadoop-yarn-server-applicationhistoryservice-3.1.1
jarName = jarName.substring(0, jarName.lastIndexOf(".")); // 获得artifactId
String artifactId = jarName.substring(0, jarName.lastIndexOf("-")); // 获得版本号
String version = jarName.substring(jarName.lastIndexOf("-") + 1); // 获得groupId // 拼接的完整路径
String groupId = readPomproperties(jarPath + orginalName);
if (groupId == null) {
throw new RuntimeException("获取groupId失败");
}
groupId = groupId.split("=")[1]; // 封装参数
params[0] = jarPath + orginalName;
params[1] = groupId;
params[2] = artifactId;
params[3] = version; install(params); } } } } /**
*
* @param path groupId=org.apache.hadoop
* @return 获得groupId,在pom.properties文件的第四行
*/
public static String readPomproperties(String path) {
JarFile jarFile = null;
String groupId = null;
// groupId在第四行
int number = 4;
try {
jarFile = new JarFile(path);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry jarEntry = entries.nextElement(); String name = jarEntry.getName(); if (name.contains("pom.properties")) {
reader = new BufferedReader(new InputStreamReader(jarFile.getInputStream(jarEntry), "utf-8"));
String line = ""; // 计行数
int count = 0; while ((line = reader.readLine()) != null) { count++;
if (count == 4) {
groupId = line;
}
} }
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (jarFile != null) {
try {
jarFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return groupId;
} // 执行安装命令
public static void install(String[] params) {
// 拼接命令
String order = "mvn install:install-file" + " -Dfile=" + params[0] + " -DgroupId=" + params[1]
+ " -DartifactId=" + params[2] + " -Dversion=" + params[3] + " -Dpackaging=jar"; Runtime rt = Runtime.getRuntime();
// 执行安装
System.out.println(order);
Process p;
try {
p = rt.exec("cmd.exe /c " + " " + order); reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
// 输出进程
while ((line = reader.readLine()) != null) {
System.out.println(line);
} if (reader != null) {
reader.close();
} // waitFor()是阻塞方法,等待外部命令执行结束
p.waitFor(); p.destroy();
p = null; } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }

测试结果:

1.eclipse中运行

2.打包成jar

3.可以指定目录.默认的目录为d:/jartoMVN/ ,直接拷贝路径时记得加上 "/"

jar包链接:https://files.cnblogs.com/files/tele-share/installjar2mvn.zip

最新文章

  1. 2015游戏蛮牛——蛮牛杯第四届开发者大赛 创见VR未来开启报名
  2. mac下搭建redis环境
  3. Android进程间通讯之messenger
  4. 第 一 百 天上课 PHP TP框架 数据库修改和删除
  5. c++中的dictionary对象:map的使用备忘
  6. C++列出完数
  7. ubuntu12编译openwrt
  8. pubwin会员合并
  9. 高级UIKit-05(CoreData)
  10. (转载)DBCP、C3P0、Proxool 、 BoneCP开源连接池的比较
  11. 折腾Java设计模式之迭代器模式
  12. safarai - loading.close() 无效问题
  13. 查询Linux系统中glibc的版本
  14. java.util.function 中的 Function、Predicate、Consumer
  15. linux 安装软件
  16. 关于jsp页面的一些知识(一)
  17. RN中移动组件开发
  18. win8换win7的操作方法
  19. Django html标签make_safe
  20. Ubuntu-18.04 下修改root用户密码,安装SSH服务,允许root用户远程登录,安装vsftp服务器

热门文章

  1. RocketMQ(九):消息发送(续)
  2. Spark SQL概念学习系列之DataFrame与RDD的区别
  3. js面向对象2--原型
  4. 洛谷 P1178 到天宫做客
  5. linux下pthread_cancel无法取消线程的原因
  6. Oracle动态SQL语句
  7. Day2:列表、元组
  8. 判断Bigdecimal类型是否等于0的方法
  9. GridView与ArrayAdapter的结合
  10. [Ramda] Declaratively Map Predicates to Object Properties Using Ramda where