老项目转maven依赖

将文件夹下的jar包转为标签,有问题的标签再访问阿里云仓库手动修改 https://developer.aliyun.com/mvn/search

public static String doGet(String httpurl) {
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        String result = null;// 返回结果字符串
        try {
            // 创建远程url连接对象
            URL url = new URL(httpurl);
            // 通过远程url连接对象打开一个连接,强转成httpURLConnection类
            connection = (HttpURLConnection) url.openConnection();
            // 设置连接方式:get
            connection.setRequestMethod("GET");
            // 设置连接主机服务器的超时时间:15000毫秒
            connection.setConnectTimeout(15000);
            // 设置读取远程返回的数据时间:60000毫秒
            connection.setReadTimeout(60000);
            // 发送请求
            connection.connect();
            // 通过connection连接,获取输入流
            if (connection.getResponseCode() == 200) {
                is = connection.getInputStream();
                // 封装输入流is,并指定字符集
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                // 存放数据
                StringBuffer sbf = new StringBuffer();
                String temp = null;
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            connection.disconnect();// 关闭远程连接
        }

        return result;
    }
    public static String getDependices(String key, String ver) throws NoSuchAlgorithmException, KeyManagementException, IOException {

        String url ="https://developer.aliyun.com/artifact/aliyunMaven/searchArtifactByGav?repoId=central&_input_charset=utf-8&groupId=&artifactId="+key+"&version="+ver;

        String res = doGet(url);

        Map map = JsonUtils.getMap4Json(res);
        List<Map> list = (List<Map>) map.get("object");
        if(list!=null&&list.size()>0){
            for(Map map1:list){
                String filename= (String) map1.get("fileName");
                String groupId = (String) map1.get("groupId");
                String packaging = (String) map1.get("packaging");
                if("pom".equals(packaging)&&!(groupId.indexOf("#")>=0)){
                    String result = "<dependency>\n" +
                            "    <groupId>"+groupId+"</groupId>\n" +
                            "    <artifactId>"+key+"</artifactId>\n" +
                            "    <version>"+ver+"</version>\n" +
                            "</dependency>";
                    return result;
                }
            }
        }


        return null;
    }

    public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException, IOException {
        File dir = new File("F:\\lib"); //修改lib目录
        for (File jar : dir.listFiles()) {
            String jarname  = jar.getName();
            int index = jarname.lastIndexOf("-");
            int jarIndex = jarname.lastIndexOf(".");

            System.out.println("<!--"+jar.getName()+"-->");
            if(index!=-1){
                String  bundleName = jarname.substring(0,index);
                String bundleVersion = jarname.substring(index +1 ,jarIndex );

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


                System.out.println(getDependices(bundleName,bundleVersion));
                System.out.println();
            }else{
                System.out.println("无法查找");
            }


        }
    }
posted @ 2022-09-12 16:49  tongyongliang  阅读(50)  评论(0编辑  收藏  举报