声明:仅限于将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. VC 常见问题百问

    http://www.cnblogs.com/cy163/archive/2006/06/19/429796.html 经典Vc书 Charles Petzold 的<Programming W ...

  2. 获取iOS顶部状态栏和Navigation的高度

    状态栏的高度 20 [[UIApplication sharedApplication] statusBarFrame].size.height Navigation的高度 44 self.navig ...

  3. C++ Tricks(一)—— 判断字符串 string 对象的所有字符都相等

    S == string(S.size(), S[0]);

  4. js 小数乘积位数太长

    function accMul(arg1,arg2) { var m=0,s1=arg1.toString(),s2=arg2.toString(); try{m+=s1.split(".& ...

  5. ZOJ 2850和ZOJ 1414

    下午上数据结构,结果竟然没有新题.T T果断上OJ来水一发 ZOJ 2850   Beautiful Meadow 传送门http://acm.zju.edu.cn/onlinejudge/showP ...

  6. mysql :Native table 'performance_schema'.'cond_instances' has the wrong structure

    err: 150418 13:25:06 [ERROR] Native table 'performance_schema'.'cond_instances' has the wrong struct ...

  7. vue-cli3使用vue-svg-loader加载svg

    vue-svg-loader Documentation - FAQ webpack loader that lets you use SVG files as Vue components Micr ...

  8. 9、getopt的用法,被用来解析命令行选项参数

    #include <unistd.h>       extern char *optarg;  //选项的参数指针       extern int optind,   //下一次调用ge ...

  9. C++利用SOAP开发WebService

    // soapconsole.cpp : Defines the entry point for the console application.// #include "stdafx.h& ...

  10. angular风格指南

    原文 https://www.jianshu.com/p/1a0a0a74769a 大纲 综述 1.单一职责 2.命名 3.LIFT-D应用程序结构 4.组件 综述 以下说的准则是根据angular官 ...