在改造老旧项目过程中,需要将旧项目的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包文件并写入到指定文件中。
效果图: