1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| package com.test;
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; 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 MakePomFromJars { public static void main(String[] args) throws FileNotFoundException, IOException { Element dependencys = new DOMElement("dependencys");
File dir = new File("E:\\my_project\\test\\src\\main\\lib"); for (File jar : dir.listFiles()) { JarInputStream jis = new JarInputStream(new FileInputStream(jar)); Manifest mainmanifest = jis.getManifest(); jis.close(); if(mainmanifest==null) { continue; } String bundleName = mainmanifest.getMainAttributes().getValue("Bundle-Name"); String bundleVersion = mainmanifest.getMainAttributes().getValue("Bundle-Version"); Element ele = null; StringBuffer sb = new StringBuffer(jar.getName()); if (bundleName != null) { bundleName = bundleName.toLowerCase().replace(" ", "-"); sb.append(bundleName+"\t").append(bundleVersion); ele = getDependices(bundleName, bundleVersion); } if (ele == null || ele.elements().size() == 0) { bundleName = ""; bundleVersion = ""; String[] ns = jar.getName().replace(".jar", "").split("-"); for (String s : ns) { if (Character.isDigit(s.charAt(0))) { bundleVersion += s + "-"; } else { bundleName += s + "-"; } } if (bundleVersion.endsWith("-")) { bundleVersion = bundleVersion.substring(0, bundleVersion.length() - 1); } if (bundleName.endsWith("-")) { bundleName = bundleName.substring(0, bundleName.length() - 1); } ele = getDependices(bundleName, bundleVersion); sb.setLength(0); sb.append(bundleName+"\t").append(bundleVersion); System.out.println(ele.asXML()); }
ele = getDependices(bundleName, bundleVersion); if (ele.elements().size() == 0) { ele.add(new DOMElement("groupId").addText("not find")); ele.add(new DOMElement("artifactId").addText(bundleName)); ele.add(new DOMElement("version").addText(bundleVersion)); } dependencys.add(ele); System.out.println(); } System.out.println(dependencys.asXML()); } public static Element getDependices(String key, String ver) { Element dependency = new DOMElement("dependency"); 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; } }
|