maven引入代替jar包引入

在改造老旧项目过程中,需要将旧项目的jar引入替换成maven引入,简化打包和部署。

由于引入的jar包可能存在以下情况:(1)maven仓库中无对应版本的jar包,尤其是离线环境或者内网隔离的开发环境;(2)jar包被重命名后引入的情况。

期望完成需要:

        将项目中引入的jar改成maven引入,jar包文件地址改为本地文件。

package com.example.test.controller;

import cn.hutool.core.util.ObjectUtil;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.*;

public class GetPomInfo {

    //生成jar包对应的坐标的文件
    private static final String outPomPath = "E:\\desktop\\pomPath.txt";
    //自定义成jar包坐标的位置数据的文件
    private static final String outNoGenPomPath = "E:\\desktop\\outGetPom.txt";
    //项目外部jar包的位置
    private static final String libPath = "E:\\desktop\\lib";
    //要在jar包里面寻找的文件名称(总共是maven jar包固定文件)
    private static final String selectFileName="pom.properties";

    public static void main(String[] args) throws Exception {
        //生成外部jar包对应的maven坐标
        getDependency();
    }

    /**
     * 生成外部jar包对应的maven坐标
     */
    private static void getDependency(){
        File directory = new File(libPath);
        if (!directory.isDirectory()) {
            throw new RuntimeException("lib文件夹不存在!");
        }
        File outPomPathFile = new File(outPomPath);
        File outNoGetPomPathFile = new File(outNoGenPomPath);
        //创建输出文件
        createPathFile(outPomPathFile, outNoGetPomPathFile);
        File[] files = directory.listFiles();
        System.out.println("总共有jar包文件:"+files.length+"个");
        FileOutputStream outPomPathFileOutPutStream=null;
        FileOutputStream outNoGenPomPathFileStream=null;
        try {
            outPomPathFileOutPutStream=new FileOutputStream(outPomPathFile);
            outNoGenPomPathFileStream=new FileOutputStream(outNoGetPomPathFile);
            for (File file : files) {
                Map<String, String> pomPropertiesInfoMap = getPomPropertiesInfo(file, selectFileName);
                //读取数据是非空
                if (null != pomPropertiesInfoMap && !pomPropertiesInfoMap.isEmpty()){
                    //组装pom文件格式
                    String version = pomPropertiesInfoMap.get("version");
                    String groupId = pomPropertiesInfoMap.get("groupId");
                    String artifactId = pomPropertiesInfoMap.get("artifactId");
                    String data = ("     <dependency>\r\n" +
                            "            <groupId>" + groupId + "</groupId>\r\n" +
                            "            <artifactId>" + artifactId + "</artifactId>\r\n" +
                            "            <version>" + version + "</version>\r\n" +
                            "            <scope>system</scope>\r\n"+
                            "            <systemPath>${project.basedir}/lib/"+file.getName()+"</systemPath>\r\n"+
                            "          </dependency>\r\n");
                    try {
                        outPomPathFileOutPutStream.write(data.getBytes());
                    } catch (IOException e) {
                        System.out.println("写入jar maven坐标信息失败,信息为:"+data);
                    }
                }else {
                    //未读取到数据
                    String name = file.getName();
                    int i = name.lastIndexOf(".jar");
                    String groudId = name.substring(0, i);
                    String data = ("     <dependency>\r\n" +
                            "            <groupId>" + groudId + "</groupId>\r\n" +
                            "            <artifactId>" + groudId + "</artifactId>\r\n" +
                            "            <version>" + "1.0" + "</version>\r\n" +
                            "            <scope>system</scope>\r\n"+
                            "            <systemPath>${project.basedir}/lib/"+file.getName()+"</systemPath>\r\n"+
                            "          </dependency>\r\n");
                    outNoGenPomPathFileStream.write(data.getBytes());
                }
            }
            //获取文件的行数
            Long fileLineNum = getFileLineNum(outPomPath);
            System.out.println("生成jar包对应的坐标的个数:"+(null != fileLineNum?fileLineNum/7:0));
            Long outNoGenPomNum = getFileLineNum(outNoGenPomPath);
            System.out.println("自定义生成jar包对应的坐标的个数:"+(null != outNoGenPomNum?outNoGenPomNum/7:0));
        } catch (Exception e) {
            System.out.println("循环获取jar包数据失败");
            e.printStackTrace();
        }finally {
            if (null != outPomPathFileOutPutStream){
                try {
                    outPomPathFileOutPutStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null !=outNoGenPomPathFileStream){
                try {
                    outNoGenPomPathFileStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    /**
     * 创建输出文件
     * @param outPomPathFile jar包maven坐标信息的文件
     * @param outNoGenPomPathFile 未有jar包坐标信息的文件
     */
    private static void createPathFile(File outPomPathFile, File outNoGenPomPathFile) {
        if (!outPomPathFile.exists()){
            File parentFile = outPomPathFile.getParentFile();
            if (!parentFile.exists()){
                if (!parentFile.isDirectory()){
                    try {
                        parentFile.mkdirs();
                    } catch (Exception e) {
                        System.out.println("创建jar包maven坐标信息文件夹失败:"+outPomPath);
                        throw new RuntimeException();
                    }
                }
            }
            try {
                outPomPathFile.createNewFile();
            } catch (IOException e) {
                System.out.println("创建jar包maven坐标信息文件失败:"+outPomPath);
                throw new RuntimeException();
            }
        }
        if (!outNoGenPomPathFile.exists()){
            File parentFile = outNoGenPomPathFile.getParentFile();
            if (!parentFile.exists()){
                try {
                    if (!parentFile.isDirectory()){
                        parentFile.mkdirs();
                    }
                } catch (Exception e) {
                    System.out.println("创建未能生成jar包坐标的位置文件夹失败:"+outNoGenPomPath);
                    throw new RuntimeException();
                }
            }
            try {
                outNoGenPomPathFile.createNewFile();
            } catch (IOException e) {
                System.out.println("创建未能生成jar包坐标的位置数据的文件失败:"+outNoGenPomPath);
                throw new RuntimeException();
            }
        }
    }

    /**
     * 读取文件转换为jar file类
     * @param file jar包的file
     * @param filename 要寻找的文件名称,如 pom.properties
     * @return
     */
    private static byte[] getFileFromJar(File file, String filename){
        try (JarFile jarFile = new JarFile(file)){
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()){
                JarEntry jarEntry = entries.nextElement();
                if (jarEntry.getName().endsWith(filename)) {
                    try (InputStream inputStream = jarFile.getInputStream(jarEntry)){
                        byte[] bytes = new byte[inputStream.available()];
                        inputStream.read(bytes);
                        return bytes;
                    }
                }
            }
        } catch (IOException e) {
            //匹配出异常返回null
            return null;
        }
        return null;
    }

    /**
     * 获取PomPropertiesInfo的数据
     * @param file 读取文件
     * @param selectFileName 在jar包里面寻找文件
     * @return
     */
    private static Map<String,String> getPomPropertiesInfo(File file,String selectFileName){
        Map<String,String> map=new HashMap<>();
        Properties properties = new Properties();
        byte[] bytes = getFileFromJar(file, selectFileName);
        if (null !=bytes && bytes.length>0){
            try {
                properties.load(new ByteArrayInputStream(bytes));
            } catch (IOException e) {
                return null;
            }
            if(ObjectUtil.isNotNull(properties.get("version"))){
                map.put("version",properties.get("version").toString());
            }
            if(ObjectUtil.isNotNull(properties.get("version"))){
                map.put("groupId",properties.get("groupId").toString());
            }
            if(ObjectUtil.isNotNull(properties.get("version"))){
                map.put("artifactId",properties.get("artifactId").toString());
            }
            return map;
        }
        return null;
    }

    /**
     * 获取文件的行数
     * @param filePath
     * @return
     */
    public static long getFileLineNum(String filePath) {
        try {
            return Files.lines(Paths.get(filePath)).count();
        } catch (IOException e) {
            return -1;
        }
    }
}

代码解读:

(1)读取本地jar包文件;

(2)按照pom文件中maven的jar格式组装jar包,路径为本地文件;

(3)生成jar包文件并写入到指定文件中。

效果图:

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值