根据jar包批量获取对应的pom.xml文件

package com.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.helper.StringUtil;

public class JarToPom {
public static void main(String[] args) throws FileNotFoundException, IOException {
// Element dependencys = new DOMElement("dependencys");
File files = new File("D:\\webJar"); //lib目录
/*File[] jars = files.listFiles(new FilenameFilter() {

@Override
public boolean accept(File dir, String name) {
return name.endsWith(".jar");
}
});*/
for (File jar : files.listFiles()) {
String jarname = jar.getName();
int index = jarname.lastIndexOf("-");
int jarIndex = jarname.lastIndexOf(".");
if(!jarname.contains("-")){
System.out.println("<!--"+jar.getName()+"-->");
System.out.println("该jar包不存在版本号");
continue;
}
String bundleName = jarname.substring(0,index);
String bundleVersion = jarname.substring(index +1 ,jarIndex);
if (bundleName ==null || bundleVersion == null){
System.out.println("<!--"+jar.getName()+"-->");
System.out.println();
continue;
}
Pattern p = Pattern.compile(".*\\d+.*");
if(!p.matcher(bundleVersion).matches()){
System.out.println("<!--"+jar.getName()+"-->");
System.out.println("该jar包不存在版本号");
continue;
}

System.out.println("<!--"+jar.getName()+"-->");
System.out.println(getDependices(bundleName,bundleVersion));
System.out.println();

}

}

public static String getDependices(String key, String ver) {

String url ="https://mvnrepository.com/search?q="+key;

org.jsoup.nodes.Document doc = null;

try {
doc = Jsoup.connect(url).ignoreContentType(true).timeout(60000).get();//ignoreContentType(true) ,这个是忽略请求类型
} catch (IOException e) {
e.printStackTrace();
}

org.jsoup.nodes.Element elem = doc.body();

String href = elem.childNodes().get(1).childNodes().get(2).childNodes().get(2).childNodes().get(0).attributes().get("href");//应该拿所有的,而不是第一个

if("".equals(href)||null==href){
return "中央仓库未查到";
}
String[] jarinfo = href.split("/");// /artifact/javax.activation/activation /artifact/org.activiti/activiti-bpmn-converter
String result = "<dependency>\n" +
" <groupId>"+jarinfo[2]+"</groupId>\n" +//此处还应该判断名字是否相同
" <artifactId>"+key+"</artifactId>\n" +
" <version>"+ver+"</version>\n" +
"</dependency>";

return result;
}
}

猜你喜欢

转载自www.cnblogs.com/zhaochi/p/12694673.html