jar包转换pom依赖文件 maven项目

Posted i like China

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jar包转换pom依赖文件 maven项目相关的知识,希望对你有一定的参考价值。

所需的源码

package com.example.demo.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");
        //jar包路径
        File dir = new File("D:\\\\SSM-Demo6-3\\\\WebRoot\\\\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 + "\\t").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 + "\\t").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;
    }

}

pom依赖

        <!--jar包转换pom依赖文件 -->
        <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.13.1</version>
        </dependency>

效果图

jar包位置在这里插入图片描述
控制台pom依赖生成
在这里插入图片描述
参考连接:my.oschina.net/zhhzhfya/blog/735050
本文到这里就结束了,你学会了吗?有问题欢迎讨论!
给大家分享一个我平时找jar包和pom依赖文件的网站:https://mvnrepository.com/
如本文对你有用,那就一键三连吧!

以上是关于jar包转换pom依赖文件 maven项目的主要内容,如果未能解决你的问题,请参考以下文章

maven pom jar包依赖怎么添加

pom文件必须和maven版本一致吗

Maven pom.xml 添加本地jar包依赖以及打包方法

如何导出项目pom中引用的mavenjar包

Maven的Pom文件中的隐式依赖导致Jar包冲突的问题

如何用maven将配置文件打在jar包外