zoukankan      html  css  js  c++  java
  • 根据已有项目jar文件生成maven的pom.xml

    package demo;
    
    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("/Users/xxx/Documents/work/workspaces/maven_demo/src/resources/webapp/WEB-INF/lib");
            for (File jar : dir.listFiles()) {
                JarInputStream jis = new JarInputStream(new FileInputStream(jar));
                Manifest mainmanifest = jis.getManifest();
                jis.close();
                if (mainmanifest == null) {
                    System.err.println(jar.getName());
                    continue;
                }
                String bundleName = mainmanifest.getMainAttributes().getValue("Bundle-Name");
                String bundleVersion = mainmanifest.getMainAttributes().getValue("Bundle-Version");
                Element ele = null;
                System.out.println(jar.getName());
    
                StringBuffer sb = new StringBuffer(jar.getName());
                if (bundleName != null) {
                    bundleName = bundleName.toLowerCase().replace(" ", "-");
                    sb.append(bundleName+"	").append(bundleVersion);
                    ele = getDependices(bundleName, bundleVersion);
                    System.out.println(sb.toString());
                    System.out.println(ele.asXML());
                }
                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+"	").append(bundleVersion);
                    System.out.println(sb.toString());
                    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");
            // 设置代理
            // 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;
        }
    
    }

    转自:https://my.oschina.net/zhhzhfya/blog/735050

  • 相关阅读:
    微信扫码跳转到H5页面输入时,如何去掉提示:防盗号或诈骗,请不要输入QQ密码?
    org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [/tmp/tomcat.1428942566812653608
    centos7 中查看log_bin是否开启以及开启log_bin
    从支付宝SDK的支付流程理解什么是公钥和私钥,什么是加密和数字签名
    Centos7中rc.local设置springboot项目开机自启动
    IIS配置实现反向代理(图文)
    【经验分享】卡方检验实战--检验次日留存率与用户分类的独立性
    R绘制3D散点图
    kmeans聚类理论篇
    PCA主成份分析学习记要
  • 原文地址:https://www.cnblogs.com/zhaochi/p/12748092.html
Copyright © 2011-2022 走看看