From c95e6e41e89e2a724420f3f0e37886056edae30e Mon Sep 17 00:00:00 2001 From: "youhong.ai" Date: Tue, 4 Jul 2023 17:53:59 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=89=93=E5=8C=85=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E6=8B=93=E5=B1=95=E8=87=AA=E5=8A=A8=E6=89=93jar?= =?UTF-8?q?=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/builderpackage/AutoPackageJar.java | 80 +++++++++++++++++++ .../java/builderpackage/FileTreeBuilder.java | 30 ++++--- 2 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 src/test/java/builderpackage/AutoPackageJar.java diff --git a/src/test/java/builderpackage/AutoPackageJar.java b/src/test/java/builderpackage/AutoPackageJar.java new file mode 100644 index 0000000..4897ef2 --- /dev/null +++ b/src/test/java/builderpackage/AutoPackageJar.java @@ -0,0 +1,80 @@ +package builderpackage; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.jar.JarEntry; +import java.util.jar.JarOutputStream; + +/** + *

测试自动打包

+ * + *

create: 2023/7/4 16:34

+ * + * @author youHong.ai + */ +public class AutoPackageJar { + + public static void main(String[] args) { + String path = AutoPackageJar.class.getResource("").getPath(); + String finalPath = path.split("/target/")[0] + File.separator + "target/classes" + File.separator; + System.out.println(finalPath); + createJar(finalPath + "aiyh", finalPath + "aiyh_utils.jar"); + } + + public static void createJar(String targetPath) { + String path = AutoPackageJar.class.getResource("").getPath(); + String finalPath = path.split(File.separator + targetPath)[0] + File.separator + targetPath + File.separator + "classes" + File.separator; + createJar(finalPath + "aiyh", finalPath + "aiyh_utils.jar"); + createJar(finalPath + "ebu7common", finalPath + "ebu7common.jar"); + } + + public static void createJar(String folderName, String jarName) { + File folder = new File(folderName); + File jarFile = new File(jarName); + + try (FileOutputStream fos = new FileOutputStream(jarFile); + JarOutputStream jos = new JarOutputStream(fos)) { + + addFolderToJar("", folder, jos); + + } catch (IOException e) { + e.printStackTrace(); + } + } + + private static void addFolderToJar(String parentPath, File folder, JarOutputStream jos) throws IOException { + String entryName = parentPath + folder.getName() + "/"; + jos.putNextEntry(new JarEntry(entryName)); + jos.closeEntry(); + + File[] files = folder.listFiles(); + if (files != null) { + for (File file : files) { + if (file.isDirectory()) { + addFolderToJar(entryName, file, jos); + } else { + addFileToJar(entryName, file, jos); + } + } + } + } + + private static void addFileToJar(String entryPath, File file, JarOutputStream jos) throws IOException { + String entryName = entryPath + file.getName(); + jos.putNextEntry(new JarEntry(entryName)); + + // 读取文件内容并写入到jar包中 + // 这里假设你的文件不是太大,可以一次性读取到内存中 + byte[] buffer = new byte[1024]; + int bytesRead; + try (FileInputStream fis = new FileInputStream(file)) { + while ((bytesRead = fis.read(buffer)) != -1) { + jos.write(buffer, 0, bytesRead); + } + } + + jos.closeEntry(); + } +} diff --git a/src/test/java/builderpackage/FileTreeBuilder.java b/src/test/java/builderpackage/FileTreeBuilder.java index 3d5072a..dd12388 100644 --- a/src/test/java/builderpackage/FileTreeBuilder.java +++ b/src/test/java/builderpackage/FileTreeBuilder.java @@ -13,17 +13,9 @@ public class FileTreeBuilder { public static final String FILE_TYPE = "文件"; public static FileInfo buildFileTree() { - String directoryPath = "target/"; + String directoryPath = "target"; List blackList = new ArrayList<>(); - List blacklistPrefixes = Arrays.asList( - "target" + File.separator + "generated-sources", - "target" + File.separator + "generated-test-sources", - "target" + File.separator + "test-classes", - "target" + File.separator + "classes" + File.separator + "aiyh", - "target" + File.separator + "classes" + File.separator + "ebu7common", - "target" + File.separator + "classes" + File.separator + "ln", - "target" + File.separator + "classes" + File.separator + "cus_getlog" - ); + Properties properties = new Properties(); try (InputStream inputStream = FileTreeBuilder.class.getClassLoader().getResourceAsStream("application.properties")) { properties.load(inputStream); @@ -33,7 +25,23 @@ public class FileTreeBuilder { String[] split = value.split(";"); Collections.addAll(blackList, split); } - directoryPath = properties.getProperty("packageRootPath"); + String packageRootPath = properties.getProperty("packageRootPath"); + if (StrUtil.isNotBlank(packageRootPath)) { + directoryPath = packageRootPath; + if (!directoryPath.endsWith(File.separator)) { + directoryPath += File.separator; + } + } + List blacklistPrefixes = Arrays.asList( + directoryPath + "generated-sources", + directoryPath + "generated-test-sources", + directoryPath + "test-classes", + directoryPath + "classes" + File.separator + "aiyh", + directoryPath + "classes" + File.separator + "ebu7common", + directoryPath + "classes" + File.separator + "ln", + directoryPath + "classes" + File.separator + "cus_getlog" + ); + AutoPackageJar.createJar(directoryPath); blackList.addAll(blacklistPrefixes); } catch (IOException e) { e.printStackTrace();