声明:仅限于将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

将jar安装到本地mvn仓库的更多相关文章

  1. 本地JAR包打入本地mvn仓库

    新建目录my-lib,将jar包移动到目录中,添加pom文件(用alipay测试) <project xmlns="http://maven.apache.org/POM/4.0.0& ...

  2. 添加jar包到本地Maven仓库

              在使用Maven的过程中,经常碰到有些jar包在中央仓库没有的情况.如果公司有私服,那么就把jar包安装到私服上.如果没有私服,那就把jar包安装到本地Maven仓库.今天介绍2种 ...

  3. 【Maven】2.使用Nexus3搭建Maven私服+上传第三方jar包到本地maven仓库

    参考文章: http://www.cnblogs.com/luotaoyeah/p/3791966.html --------------------------------------------- ...

  4. 使用Nexus3搭建Maven私服+上传第三方jar包到本地maven仓库

    1.搭建Maven私服背景 公司还是按捺不住,要搭建一个自己的Maven本地仓库,可以让开发人员down架包,从内网还是快很多. 这样公司的maven本地仓库就是 开发人员自己电脑上的maven仓库 ...

  5. Maven:将Jar安装到本地仓库和Jar上传到私服

    1.依赖如下: <dependency> <groupId>org.quartz-scheduler.internal</groupId> <artifact ...

  6. Maven : 将Jar安装到本地仓库和Jar上传到私服 转

    http://blog.csdn.net/we_shell/article/details/49819221 Jar的maven配置 <dependency><groupId> ...

  7. maven如何将本地jar安装到本地仓库

    1.首先确认你的maven是否已经配置: 指令:mvn -v 2.本地的jar包位置: 3.在自己项目pom.xml中添加jar依赖: <dependency> <groupId&g ...

  8. 将Jar安装到本地仓库和Jar上传到私服

    举例 1. 依赖如下: <dependency> <groupId>org.quartz-scheduler.internal</groupId> <arti ...

  9. 把jar包安装到本地Maven仓库

    使用的场景 自己写的工具类想安装到本地 从Maven仓库中下载不下来的jar 使用的步骤       首先要保证自己的Maven配置全局环境变量,如果没有配置过maven全局变量,可以按照下面的步骤配 ...

随机推荐

  1. 洛谷 P1599 结算日

    洛谷 P1599 结算日 题目描述 “不放债不借债”,贝西多么希望自己可以遵循这个忠告.她已经和她的N(1 <= N <= 100,000)个朋友有了债务关系,或者借债了,或者放债了.她的 ...

  2. [Angular] Progress HTTP Events with 'HttpRequest'

    New use case that is supported by the HTTP client is Progress events. To receive these events, we cr ...

  3. jquery设置attr属性值

    1.返回属性值 $(selector).attr(attribute); 2.设置属性值 $(selector).attr(attribute,value); 3.设置多个属性值 $(selector ...

  4. JS学习笔记 - 点击、回车、ctrl+回车提交留言

    疑点: oTxt1.onkeydown = function (ev) 为什么这里的onkeydown = function有变量 (ev),前面onclick函数没有? window.onload ...

  5. Loadrunner--web_find和web_reg_find的用法和区别

    一.web_find()函数 该函数的作用是“在页面中查找相应的内容”,常用参数及含义如下: web_find("web_find", //定义该查找函数的名称 "Rig ...

  6. amazeui学习笔记--js插件(UI增强2)--按钮交互Button

    amazeui学习笔记--js插件(UI增强2)--按钮交互Button 一.总结 1.按钮loading状态: <button type="button" class=&q ...

  7. 表单提交数据格式form data

    前言: 最近遇到的最多的问题就是表单提交数据格式问题了. 常见的三种表单提交数据格式,分别举例说明:(项目是vue的框架) 1.application/x-www-form-urlencoded 提交 ...

  8. UVA 11136 - Hoax or what (可以提交了,不会Submission error了)

    看题传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  9. 9.4 Binder系统_驱动情景分析_服务使用过程

    5. 服务使用过程 test_client进程: 用户态: (1)已结获得了“hello”服务,handle=1; (2)构造数据:code(那个函数)和函数参数 (3)发送ioctl后进入内核态,先 ...

  10. Java 学习(19):Java 多线程编程

    Java 多线程编程 Java 给多线程编程提供了内置的支持.一个多线程程序包含两个或多个能并发运行的部分.程序的每一部分都称作一个线程,并且每个线程定义了一个独立的执行路径. 多线程是多任务的一种特 ...