maven根据外部依赖lib文件夹生成dependencies

有时候maven需要添加lib下的外部依赖,但是一个个去maven页面找太麻烦,可通过此类来生成依赖
调整: https://mvnrepository.com 直接访问会403,所以需要先浏览器访问,F12拿到请求头,写入代码来模拟浏览器访问


/**
 * @author alexlee
 * @version 1.0
 * @date 2022/1/19 17:12
 */

import org.jsoup.Connection;
import org.jsoup.Jsoup;

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

public class Test {
    public static void main(String[] args)  {
        File dir = new File("C:\\Users\\a1exl\\Desktop\\XXXX\\lib"); //lib目录
        for (File jar : dir.listFiles()) {
            String jarname  = jar.getName();
            int index = jarname.lastIndexOf("-");
            int jarIndex = jarname.lastIndexOf(".");
            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) {

        String url ="https://mvnrepository.com/search?q="+key;

        org.jsoup.nodes.Document doc = null;

        try {
            Connection conn = Jsoup.connect(url).timeout(30000);
            conn.header("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            conn.header("Accept-Encoding","gzip, deflate, sdch");
            conn.header("Accept-Language","zh-CN,zh;q=0.8");
            conn.header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36 Edg/97.0.1072.62");
            conn.header("sec-ch-ua","\" Not;A Brand\";v=\"99\", \"Microsoft Edge\";v=\"97\", \"Chromium\";v=\"97\"");

            //实际使用时需要调整
            HashMap<String, String> map = new HashMap<>();
            map.put("cf_chl_2","46adccb0765ee98");
            map.put("cf_chl_prog","x10");
            map.put("cf_clearance","ItwpljRmi9NIYanszrCB81H2ba3vOpVvcmsQ6UijMSE-1642583791-0-250");
            map.put("_ga","GA1.2.1475575576.1642583792");
            map.put("_gid","GA1.2.162733513.1642583792");
            map.put("__cf_bm",".gpWC9OrngH8EuFcqgxCg2uy5hxmKA00UW_sHb0lmXM-1642583792-0-AWewCSAYDUqf1Y8FTt4Un5cxVCQ9MTGyqL5cGASdqTlhsY80pKkIun4Wds3GGGT6LfIQ1zEKYhW68l1VQS8UJVYZsVjHvmPdqpj5LskGvxbAuo4HA/Gaea/8zUb/YX79AQ==");
            map.put("_gat","1");
            map.put("MVN_SESSION","eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVpZCI6IjdkZDJhOWEwLTc5MDgtMTFlYy04ODU2LTU5NTI5ODkzYzRlYSJ9LCJleHAiOjE2NzQxMjAxNzQsIm5iZiI6MTY0MjU4NDE3NCwiaWF0IjoxNjQyNTg0MTc0fQ.2axvM5nP4pfnbLC_EYTl9Fq5-esf4vRMuW-o-YqccTE");
            conn.cookies(map);

            doc = conn.get();
        } catch (IOException e) {
            e.printStackTrace();
        }

        org.jsoup.nodes.Element elem = doc.body();

        String href = elem.childNodes().get(1).childNodes().get(2).childNodes().get(2).childNodes().get(0).attributes().get("href");

        String[] jarinfo = href.split("/");

        String result = "<dependency>\n" +
                "    <groupId>"+jarinfo[2]+"</groupId>\n" +
                "    <artifactId>"+key+"</artifactId>\n" +
                "    <version>"+ver+"</version>\n" +
                "     <scope>system</scope>\n" +
                "     <systemPath>${project.basedir}/lib/"+key+"-"+ver+".jar</systemPath> " +
                "</dependency>";



        return result;
    }
}
posted @ 2022-01-20 15:43  A1exLee  阅读(192)  评论(0)    收藏  举报