maven项目中pom.xml快速生成

项目引用的jar包数了一下将近100个,直接引用其他项目的pom文件自然也不适合。所以有了下面的代码,把使用到的jar包放在文件夹中,运行一下,自动生成。

  1.  
    import java.io.File;
  2.  
    import java.io.FileInputStream;
  3.  
    import java.io.FileNotFoundException;
  4.  
    import java.io.IOException;
  5.  
    import java.util.jar.JarInputStream;
  6.  
    import java.util.jar.Manifest;
  7.  
    import org.dom4j.Element;
  8.  
    import org.dom4j.dom.DOMElement;
  9.  
    import org.jsoup.Jsoup;
  10.  
    import com.alibaba.fastjson.JSONObject;
  11.  
    public class makePom {
  12.  
    public static void main(String[] args) throws FileNotFoundException, IOException {
  13.  
    Element dependencys = new DOMElement("dependencys");
  14.  
    File dir = new File("F:/lib"); //需生成pom.xml 文件的 lib路径
  15.  
    for (File jar : dir.listFiles()) {
  16.  
    JarInputStream jis = new JarInputStream(new FileInputStream(jar));
  17.  
    Manifest mainmanifest = jis.getManifest();
  18.  
    jis.close();
  19.  
    String bundleName = mainmanifest.getMainAttributes().getValue("Bundle-Name");
  20.  
    String bundleVersion = mainmanifest.getMainAttributes().getValue("Bundle-Version");
  21.  
    Element ele = null;
  22.  
    System.out.println(jar.getName());
  23.  
    StringBuffer sb = new StringBuffer(jar.getName());
  24.  
    if (bundleName != null) {
  25.  
    bundleName = bundleName.toLowerCase().replace(" ", "-");
  26.  
    sb.append(bundleName+"\t").append(bundleVersion);
  27.  
    ele = getDependices(bundleName, bundleVersion);
  28.  
    System.out.println(sb.toString());
  29.  
    System.out.println(ele.asXML());
  30.  
    }
  31.  
    if (ele == null || ele.elements().size() == 0) {
  32.  
    bundleName = "";
  33.  
    bundleVersion = "";
  34.  
    String[] ns = jar.getName().replace(".jar", "").split("-");
  35.  
    for (String s : ns) {
  36.  
    if (Character.isDigit(s.charAt(0))) {
  37.  
    bundleVersion += s + "-";
  38.  
    } else {
  39.  
    bundleName += s + "-";
  40.  
    }
  41.  
    }
  42.  
    if (bundleVersion.endsWith("-")) {
  43.  
    bundleVersion = bundleVersion.substring(0, bundleVersion.length() - 1);
  44.  
    }
  45.  
    if (bundleName.endsWith("-")) {
  46.  
    bundleName = bundleName.substring(0, bundleName.length() - 1);
  47.  
    }
  48.  
    ele = getDependices(bundleName, bundleVersion);
  49.  
    sb.setLength(0);
  50.  
    sb.append(bundleName+"\t").append(bundleVersion);
  51.  
    System.out.println(sb.toString());
  52.  
    System.out.println(ele.asXML());
  53.  
    }
  54.  
     
  55.  
    ele = getDependices(bundleName, bundleVersion);
  56.  
    if (ele.elements().size() == 0) {
  57.  
    ele.add(new DOMElement("groupId").addText("not find"));
  58.  
    ele.add(new DOMElement("artifactId").addText(bundleName));
  59.  
    ele.add(new DOMElement("version").addText(bundleVersion));
  60.  
    }
  61.  
    dependencys.add(ele);
  62.  
    System.out.println();
  63.  
    }
  64.  
    System.out.println(dependencys.asXML());
  65.  
    }
  66.  
    public static Element getDependices(String key, String ver) {
  67.  
    Element dependency = new DOMElement("dependency");
  68.  
    // 设置代理
  69.  
    // System.setProperty("http.proxyHost", "127.0.0.1");
  70.  
    // System.setProperty("http.proxyPort", "8090");
  71.  
    try {
  72.  
    String url = "http://search.maven.org/solrsearch/select?q=a%3A%22" + key + "%22%20AND%20v%3A%22" + ver + "%22&rows=3&wt=json";
  73.  
    org.jsoup.nodes.Document doc = Jsoup.connect(url).ignoreContentType(true).timeout(30000).get();
  74.  
    String elem = doc.body().text();
  75.  
    JSONObject response = JSONObject.parseObject(elem).getJSONObject("response");
  76.  
    if (response.containsKey("docs") && response.getJSONArray("docs").size() > 0) {
  77.  
    JSONObject docJson = response.getJSONArray("docs").getJSONObject(0);
  78.  
    Element groupId = new DOMElement("groupId");
  79.  
    Element artifactId = new DOMElement("artifactId");
  80.  
    Element version = new DOMElement("version");
  81.  
    groupId.addText(docJson.getString("g"));
  82.  
    artifactId.addText(docJson.getString("a"));
  83.  
    version.addText(docJson.getString("v"));
  84.  
    dependency.add(groupId);
  85.  
    dependency.add(artifactId);
  86.  
    dependency.add(version);
  87.  
    }
  88.  
    } catch (Exception e) {
  89.  
    e.printStackTrace();
  90.  
    }
  91.  
    return dependency;
  92.  
    }
  93.  
    }

生成之后可能会引用一些依赖,不想要这些依赖的话可以在pom.xml加上这些就不引用依赖了

 <exclusions>
            <exclusion>
                <groupId>*</groupId>
                <artifactId>*</artifactId>
            </exclusion>
     </exclusions>

posted @ 2019-04-05 20:17  滤v镜  阅读(3099)  评论(0编辑  收藏  举报