JAVA程序实现 maven本地jar包安装


前段时间研究heritrix ,要搭建一个本地项目,但是发现很多jar包都下不下来(可恶的防火墙), 无赖只有自己手动安装本地maven库了.

于是动手写了个小程序,来实现本地jar包的安装.

废话不多说,上代码

package com.local.install;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;


public class InstallMavenJar {
	/**
	 * 扫描所有的jar包
	 * @return
	 */
	public List<String> scanJars(){
		String filePath = getRootDirectory();
		List<String> jars = new ArrayList<String>();
		if(filePath == null || "".equals(filePath.trim())){
			System.out.println("对不起没有找到,librariesPath,请检查配置");
			return jars ;
		}
		File file = new File(filePath);
		if(file.isDirectory()){
			File [] files = file.listFiles();
			for(File f : files){
				if(f.isDirectory()){
					continue;
				}
				String fileName = f.getName();
				if(fileName.endsWith(".jar")){
					jars.add(f.getAbsolutePath());
				}
			}
			return jars;
		}
		String fileName = file.getName();
		if(fileName.endsWith(".jar")){
			jars.add(file.getAbsolutePath());
		}
		return jars;
	}
	
	/**
	 * 执行安装
	 */
	public void installMavenLib(){
		List<String> jars = scanJars();
		Runtime runtime = Runtime.getRuntime();
		for(String jar : jars){
			String mavenCmd = getMavenPath()+"mvn install:install-file -Dfile=";
			int index = jar.lastIndexOf("\\");
			String jarName = jar.substring(index+1);
			String[] splits = jarName.split("-");
			String version = "";
			String groupId = "";
			for(String s : splits){
				if(s.contains(".jar")){
					int suffixIndex = s.indexOf(".jar");
					version = s.substring(0,suffixIndex);
					int groupIndex = jarName.indexOf(version);
					groupId = jarName.substring(0,groupIndex-1);
				}
			}
			mavenCmd += jar;
			mavenCmd += " -DgroupId="+groupId;
			mavenCmd += " -DartifactId="+groupId;
			mavenCmd +=" -Dversion="+version;
			mavenCmd +=" -Dpackaging=jar";
			System.out.println("-*-*-*-*-*-*开始安装"+groupId+"-*-*-*-*-*-*");
			try {
				String[] cmds = new String[]{"cmd", "/c",mavenCmd}; 
				Process p = runtime.exec(cmds);
//				InputStream in = p.getErrorStream();
				InputStream in = p.getInputStream();
				
				BufferedReader reader = new BufferedReader(new InputStreamReader(in));
				String line = null;
				System.out.println("-*-*-*-*-*-*执行结果如下-*-*-*-*-*-*");
				while ( (line = reader.readLine()) != null ){
					System.out.println(line);
				}
				p.destroy();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	}
	/**
	 * 获取jar包所在的根目录
	 * @return
	 */
	public String  getRootDirectory(){
		InputStream in = InstallMavenJar.class.getClassLoader().getResourceAsStream("libraries.properties");
		Properties props = new Properties();
		try {
			props.load(in);
			String filePath = props.getProperty("librariesPath");
			return filePath;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取maven的路径
	 * @return
	 */
	public String getMavenPath(){
		InputStream in = InstallMavenJar.class.getClassLoader().getResourceAsStream("libraries.properties");
		Properties props = new Properties();
		try {
			props.load(in);
			String filePath = props.getProperty("mavenPath");
			return filePath;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static void main(String[] args) {
		InstallMavenJar install = new InstallMavenJar();
		install.installMavenLib();
	}
	
	
}
代码中的获取maven 目录和jar包所在的目录,需要在配置文件中配置.

librariesPath=F:\\tomcat\\tomcat7\\webapps\\activiti-explorer\\WEB-INF\\lib
mavenPath=D:/maven/apache-maven-3.2.3/bin/
整个项目结构如下图


将配置文件置于根目录下即可.

代码中的getmavenpath 和getrootdirectory 方法可以合并成一个 返回map就行了. 还少了很多资源的浪费. 但是作者当时弄的时候是后来加的 代码都搞得差不多了. 就直接copy上去了. 有兴趣的童鞋可以改改.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值