diff --git a/src/test/java/BuilderPackageEcology.java b/src/test/java/BuilderPackageEcology.java index ae43a2c..760c550 100644 --- a/src/test/java/BuilderPackageEcology.java +++ b/src/test/java/BuilderPackageEcology.java @@ -55,7 +55,7 @@ public class BuilderPackageEcology extends Application { try { FileCompressor.compressFiles(filePaths, outputFile.getAbsolutePath(), path -> { String rootPath = Util.class.getResource("/").getPath(); - rootPath = rootPath.split(File.separator + "target" + File.separator)[0] + File.separator + "target" + File.separator + "classes" + File.separator; + rootPath = rootPath.split(File.separator + FileTreeBuilder.targetPath)[0] + File.separator + FileTreeBuilder.targetPath + "classes" + File.separator; String replace = "/ecology/" + (isEcology ? "WEB-INF/classes/" : "classbean/"); if (path.endsWith(".jar")) { replace = "/ecology/WEB-INF/lib/"; 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..ee9dd68 100644 --- a/src/test/java/builderpackage/FileTreeBuilder.java +++ b/src/test/java/builderpackage/FileTreeBuilder.java @@ -12,18 +12,12 @@ public class FileTreeBuilder { public static final String DIRECTORY_TYPE = "文件夹"; public static final String FILE_TYPE = "文件"; + public static String targetPath; + public static FileInfo buildFileTree() { - String directoryPath = "target/"; + String directoryPath = "target"; List