普通工程轉maven工程lib下jar包一次查詢dependency
在將普通工程轉maven管理時候頭疼的難點之一就是依賴包的導入。這裏寫了一個直接查詢中央maven庫的小程序,希望可以減少些麻煩。 使用了網友的代碼並做了些改動。
package com;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.jar.JarInputStream;
import java.util.jar.Manifest;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.jsoup.Jsoup;
import com.alibaba.fastjson.JSONObject;
public class DependencysSearch {
public static void main(String[] args) throws FileNotFoundException, IOException {
File dir = new File("D:/developrepertory/eclipse2016/myproj/WebRoot/WEB-INF/lib"); // 需生成pom.xml
// 文件的
// lib路徑
DependencysSearch tt1 = new DependencysSearch();
for (File jar : dir.listFiles()) {
tt1.getJarDependices(jar);
}
System.out.println("============dependencys============");
for (String s : tt1.fund) {
System.out.println(s);
}
System.out.println("============nofound============");
for (String s : tt1.nofound) {
System.out.println(s);
}
System.out.println("===========noparse=============");
for (String s : tt1.noparse) {
System.out.println(s);
}
// Element element = tt1.getJarDependices(new File("D:/developrepertory/eclipse2016/myproj/WebRoot/WEB-INF/lib/spring-webmvc-3.1.1.RELEASE.jar"));
}
private List<String> nofound=new ArrayList<String>();
private List<String> noparse=new ArrayList<String>();
private Element dependencys =new DOMElement("dependencys");
private List<String> fund =new ArrayList<String>();
/**
* 獲取傳入的jar包的依賴
*
* @param jar
* @author Norton Lai
* @return
* @throws IOException
* @throws FileNotFoundException
* @created 2019年2月21日 上午11:46:46
*/
public Element getJarDependices(File jar) throws FileNotFoundException, IOException {
System.out.println("開始獲取:"+jar.getName()+" 的依賴");
Element ele = null;
String jarName = jar.getName().substring(0, jar.getName().length()-4);
//獲取jar包內的信息
String[] jarInfo = getJarInfoFromMf(jar);
String[] jarInfo1 =getJarInfoFromFileName(jar);
String bundleName = jarInfo[0];
String bundleVersion = jarInfo[1];
System.out.println("mf獲取內容 "+Arrays.toString(jarInfo));
if (bundleName==null) {
bundleName = jarInfo1[0];
bundleVersion = jarInfo1[1];
System.out.println("文件解析獲取內容 "+Arrays.toString(jarInfo1));
}
if (bundleName==null) {
System.out.println(jarName+"無法解析出合適的名稱和版本");
noparse.add(jar.getName());
return null;
}
ele = getDependices(bundleName, bundleVersion);
if (ele==null||ele.asXML().equals("<dependency/>")||ele.elements().size() == 0) {
System.out.println("無法查詢到jar包信息: bundleName是否錯誤 "+bundleName+" 使用文件名重試 "+jarName);
bundleName=jarName.substring(0,jarName.length()- bundleVersion.length()-1
);
System.out.println(bundleName);
ele = getDependices(bundleName, bundleVersion);
}
if (ele==null||ele.elements().size()==0||ele.asXML().equals("<dependency/>")) {
System.out.println(jar.getName()+"未能找到依賴");
nofound.add(jar.getName());
}else {
System.out.println(jar.getName()+"找到依賴 "+ele.asXML());
dependencys.add(ele);
fund.add(ele.asXML());
}
return ele;
}
/**
* 從文件名中拆分出版本和依賴名,可能不準
* @param jar
* @return
* @author Norton Lai
* @created 2019年2月21日 下午2:19:15
*/
private String[] getJarInfoFromFileName(File jar){
String jarName = jar.getName().substring(0, jar.getName().length()-4);
String bundleName = null;
String bundleVersion = null;
String[] res=new String[2];
int idx = jarName.lastIndexOf("-");
if (idx==-1) {
return res;
}
bundleName=jarName.substring(0, idx);
bundleVersion=jarName.substring(idx+1, jarName.length());
res[0]=bundleName;
res[1]=bundleVersion;
return res;
}
/**
* 讀取mf文件獲取jar包信息,可能獲取不到的。
* @param jar
* @return
* @author Norton Lai
* @created 2019年2月21日 下午2:16:07
*/
private String[] getJarInfoFromMf(File jar){
String bundleName = null;
String bundleVersion = null;
String[] res=new String[2];
try {
//獲取jar包內的信息
JarInputStream jis = new JarInputStream(new FileInputStream(jar));
Manifest mainmanifest = jis.getManifest();
jis.close();
bundleName = mainmanifest.getMainAttributes().getValue("Bundle-Name");
bundleVersion = mainmanifest.getMainAttributes().getValue("Bundle-Version");
if (bundleName!=null) {
bundleName = bundleName.toLowerCase().replace(" ", "-");
}
} catch (Exception e) {
}
res[0]=bundleName;
res[1]=bundleVersion;
return res;
}
/**
* 傳入包的名稱和版本 ,返回pom的依賴
*
* @param key
* @param ver
* @return
* @author Norton Lai
* @created 2019年2月21日 上午11:38:07
*/
public static Element getDependices(String key, String ver) {
Element dependency = new DOMElement("dependency");
// 設置代理
// System.setProperty("http.proxyHost", "127.0.0.1");
// System.setProperty("http.proxyPort", "8090");
try {
String url = "http://search.maven.org/solrsearch/select?q=a%3A%22" + key + "%22%20AND%20v%3A%22" + ver
+ "%22&rows=3&wt=json";
org.jsoup.nodes.Document doc = Jsoup.connect(url).ignoreContentType(true).timeout(30000).get();
String elem = doc.body().text();
JSONObject response = JSONObject.parseObject(elem).getJSONObject("response");
if (response.containsKey("docs") && response.getJSONArray("docs").size() > 0) {
JSONObject docJson = response.getJSONArray("docs").getJSONObject(0);
Element groupId = new DOMElement("groupId");
Element artifactId = new DOMElement("artifactId");
Element version = new DOMElement("version");
groupId.addText(docJson.getString("g"));
artifactId.addText(docJson.getString("a"));
version.addText(docJson.getString("v"));
dependency.add(groupId);
dependency.add(artifactId);
dependency.add(version);
}
} catch (Exception e) {
e.printStackTrace();
}
return dependency;
}
}
使用了 dom4j-1.6.1 fastjson-1.1.37 jsoup-1.10.3 三個jar包