1.第一种用引入jar包的方法
网盘链接:https://pan.baidu.com/s/10HNjNeZc1d5QrFNtvLPWBA
提取码:oako
以上是整个文件直接用idea打开即可
1 import com.gugugu.maven.Poms;
2
3 import java.io.IOException;
4
5 /**
6 * @author Administrator
7 * @data 2020/2/22 - 13:50
8 **/
9 public class PomTest1 {
10 public static void main(String[] args) throws IOException {
11 Poms.getPomXMl("E:\后端笔记\常用工具\数据校验\lib");//jar包的位置
12 }
13 }
2.第二种:测试类解决
无需其他jar包,只需要测试类,
1 import com.alibaba.fastjson.JSONObject;
2 import org.dom4j.Element;
3 import org.dom4j.dom.DOMElement;
4 import org.jsoup.Jsoup;
5 import org.junit.Test;
6
7 import java.io.File;
8 import java.io.FileInputStream;
9 import java.io.FileNotFoundException;
10 import java.io.IOException;
11 import java.util.jar.JarInputStream;
12 import java.util.jar.Manifest;
13
14 /**
15 * @author wxy
16 * @data 2020/2/22 - 13:42
17 **/
18 public class PomTest {
19 public static void main(String[] args) throws FileNotFoundException, IOException {
20 Element dependencys = new DOMElement("dependencys");
21 File dir = new File("E:\后端笔记\常用工具\数据校验\lib");
22 for (File jar : dir.listFiles()) {
23 JarInputStream jis = new JarInputStream(new FileInputStream(jar));
24 Manifest mainmanifest = jis.getManifest();
25 jis.close();
26 String bundleName = mainmanifest.getMainAttributes().getValue("Bundle-Name");
27 String bundleVersion = mainmanifest.getMainAttributes().getValue("Bundle-Version");
28 Element ele = null;
29 System.out.println(jar.getName());
30 StringBuffer sb = new StringBuffer(jar.getName());
31 if (bundleName != null) {
32 bundleName = bundleName.toLowerCase().replace(" ", "-");
33 sb.append(bundleName+" ").append(bundleVersion);
34 ele = getDependices(bundleName, bundleVersion);
35 System.out.println(sb.toString());
36 System.out.println(ele.asXML());
37 }
38 if (ele == null || ele.elements().size() == 0) {
39 bundleName = "";
40 bundleVersion = "";
41 String[] ns = jar.getName().replace(".jar", "").split("-");
42 for (String s : ns) {
43 if (Character.isDigit(s.charAt(0))) {
44 bundleVersion += s + "-";
45 } else {
46 bundleName += s + "-";
47 }
48 }
49 if (bundleVersion.endsWith("-")) {
50 bundleVersion = bundleVersion.substring(0, bundleVersion.length() - 1);
51 }
52 if (bundleName.endsWith("-")) {
53 bundleName = bundleName.substring(0, bundleName.length() - 1);
54 }
55 ele = getDependices(bundleName, bundleVersion);
56 sb.setLength(0);
57 sb.append(bundleName+" ").append(bundleVersion);
58 System.out.println(sb.toString());
59 System.out.println(ele.asXML());
60 }
61
62 ele = getDependices(bundleName, bundleVersion);
63 if (ele.elements().size() == 0) {
64 ele.add(new DOMElement("groupId").addText("not find"));
65 ele.add(new DOMElement("artifactId").addText(bundleName));
66 ele.add(new DOMElement("version").addText(bundleVersion));
67 }
68 dependencys.add(ele);
69 System.out.println();
70 }
71 System.out.println(dependencys.asXML());
72 }
73 public static Element getDependices(String key, String ver) {
74 Element dependency = new DOMElement("dependency");
75 // 设置代理
76 // System.setProperty("http.proxyHost", "127.0.0.1");
77 // System.setProperty("http.proxyPort", "8090");
78 try {
79 String url = "http://search.maven.org/solrsearch/select?q=a%3A%22" + key + "%22%20AND%20v%3A%22" + ver + "%22&rows=3&wt=json";
80 org.jsoup.nodes.Document doc = Jsoup.connect(url).ignoreContentType(true).timeout(30000).get();
81 String elem = doc.body().text();
82 JSONObject response = JSONObject.parseObject(elem).getJSONObject("response");
83 if (response.containsKey("docs") && response.getJSONArray("docs").size() > 0) {
84 JSONObject docJson = response.getJSONArray("docs").getJSONObject(0);
85 Element groupId = new DOMElement("groupId");
86 Element artifactId = new DOMElement("artifactId");
87 Element version = new DOMElement("version");
88 groupId.addText(docJson.getString("g"));
89 artifactId.addText(docJson.getString("a"));
90 version.addText(docJson.getString("v"));
91 dependency.add(groupId);
92 dependency.add(artifactId);
93 dependency.add(version);
94 }
95 } catch (Exception e) {
96 e.printStackTrace();
97 }
98 return dependency;
99 }
100 }