• 根据jar包批量获取对应的pom.xml文件


    代码有问题,不够严谨,暂做记录。

    package com.io;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.regex.Pattern;

    import org.jsoup.Jsoup;
    import org.jsoup.helper.StringUtil;

    public class JarToPom {
    public static void main(String[] args) throws FileNotFoundException, IOException {
    // Element dependencys = new DOMElement("dependencys");
    File files = new File("D:\webJar"); //lib目录
    /*File[] jars = files.listFiles(new FilenameFilter() {

    @Override
    public boolean accept(File dir, String name) {
    return name.endsWith(".jar");
    }
    });*/
    for (File jar : files.listFiles()) {
    String jarname = jar.getName();
    int index = jarname.lastIndexOf("-");
    int jarIndex = jarname.lastIndexOf(".");
    if(!jarname.contains("-")){
    System.out.println("<!--"+jar.getName()+"-->");
    System.out.println("该jar包不存在版本号");
    continue;
    }
    String bundleName = jarname.substring(0,index);
    String bundleVersion = jarname.substring(index +1 ,jarIndex);
    if (bundleName ==null || bundleVersion == null){
    System.out.println("<!--"+jar.getName()+"-->");
    System.out.println();
    continue;
    }
    Pattern p = Pattern.compile(".*\d+.*");
    if(!p.matcher(bundleVersion).matches()){
    System.out.println("<!--"+jar.getName()+"-->");
    System.out.println("该jar包不存在版本号");
    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 {
    doc = Jsoup.connect(url).ignoreContentType(true).timeout(60000).get();//ignoreContentType(true) ,这个是忽略请求类型
    } 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");//应该拿所有的,而不是第一个

    if("".equals(href)||null==href){
    return "中央仓库未查到";
    }
    String[] jarinfo = href.split("/");// /artifact/javax.activation/activation /artifact/org.activiti/activiti-bpmn-converter
    String result = "<dependency> " +
    " <groupId>"+jarinfo[2]+"</groupId> " +//此处还应该判断名字是否相同
    " <artifactId>"+key+"</artifactId> " +
    " <version>"+ver+"</version> " +
    "</dependency>";

    return result;
    }
    }

  • 相关阅读:
    递归的一些应用(一)遍历文件夹
    获取指定路径下文件夹所有文件的大小
    JavaScript Color Picker
    在ASP.NET中,用javascript给CuteEditor控件赋值
    通过URL判断文件是否有存在
    语音验证码
    [链接].net 学习网站
    [音乐]梦中的婚礼
    [转载]天堂猎人影院的爱情狩猎
    为应用程序添加消息过滤器
  • 原文地址:https://www.cnblogs.com/zhaochi/p/12694673.html
Copyright © 2020-2023  润新知