如何将lib中的jar以maven的dependency形式导出

该文章介绍了一个Java程序,用于遍历指定lib目录下的jar文件,并通过阿里云Maven仓库API查询对应的groupId,生成Maven依赖XML格式的字符串,帮助用户快速将本地jar库转化为Maven项目依赖。
摘要由CSDN通过智能技术生成

如何将lib中的jar以maven的dependency形式导出

该文借鉴自:https://blog.csdn.net/qq_23850035/article/details/88779367,如另行发布,请带上之前的作者

package org.crazyit.activiti;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.jsoup.Jsoup;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

public class Test {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        Element dependencys = new DOMElement("dependencys");
        File dir = new File("/Users/xxx/Documents/24_activiti/codes/02/xiaozu/lib"); //lib目录
        for (File jar : dir.listFiles()) {
            String jarname = jar.getName();
            if (jarname.startsWith(".")) {
                continue;
            }
            int index = jarname.lastIndexOf("-");
            int jarIndex = jarname.lastIndexOf(".");

            System.out.println("jarname = " + jarname);
            String bundleName = jarname.substring(0, index);
            String bundleVersion = jarname.substring(index + 1, jarIndex);

            if (bundleName == null || bundleVersion == null) {
                continue;
            }

            System.out.println("<!--" + jar.getName() + "-->");
            System.out.println(getDependices(bundleName, bundleVersion));
            System.out.println();

        }

    }

    public static String getDependices(String key, String ver) {
        if (ver.equals("sources")) {
            return "";
        }
//        String url ="https://mvnrepository.com/search?q="+key;
        String url = "https://maven.aliyun.com/artifact/aliyunMaven/searchArtifactByGav?groupId=&artifactId="+ key +"&version="+ ver +"&repoId=all&_input_charset=utf-8";

        org.jsoup.nodes.Document doc = null;
        org.jsoup.nodes.Document doc2 = null;

        try {
            doc = Jsoup.connect(url).ignoreContentType(true).timeout(60000).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Thread.sleep(1000L + new Random().nextInt(200));
        } catch (Exception e) {

        }

        org.jsoup.nodes.Element elem = doc.body();
        Object href1 = elem.childNodes().get(0);
        HashMap hashMap = JSON.parseObject(JSON.toJSONString(href1), HashMap.class);

        HashMap hashMap2 = JSON.parseObject(hashMap.get("wholeText").toString(), HashMap.class);

        List<Item> ts = (List<Item>) JSONArray.parseArray(hashMap2.get("object").toString(), Item.class);
//        System.out.println(JSON.toJSON(ts));


        String groupId = null;
        for (Item item : ts) {
            if ("jar".equals(item.getPackaging())
                    && ver.equals(item.getVersion())
                    && key.equals(item.getArtifactId())) {
                groupId = item.getGroupId();
                break;
            }
        }

        String result = "<dependency>\n" +
                "    <groupId>" + groupId + "</groupId>\n" +
                "    <artifactId>" + key + "</artifactId>\n" +
                "    <version>" + ver + "</version>\n" +
                "</dependency>";

        return result;
    }
}

Item

package activiti;

import com.alibaba.fastjson.JSON;
import org.dom4j.Element;
import org.dom4j.dom.DOMElement;
import org.jsoup.Jsoup;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;

public class Item {

    private String artifactId;
    private String classifier;
    private String fileName;
    private String groupId;
    private String packaging;
    private String repositoryId;
    private String version;

    public String getArtifactId() {
        return artifactId;
    }

    public void setArtifactId(String artifactId) {
        this.artifactId = artifactId;
    }

    public String getClassifier() {
        return classifier;
    }

    public void setClassifier(String classifier) {
        this.classifier = classifier;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    public String getPackaging() {
        return packaging;
    }

    public void setPackaging(String packaging) {
        this.packaging = packaging;
    }

    public String getRepositoryId() {
        return repositoryId;
    }

    public void setRepositoryId(String repositoryId) {
        this.repositoryId = repositoryId;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值