自动打包工具拓展自动打jar包

dev
youhong.ai 2023-07-04 17:53:59 +08:00
parent ff69438198
commit c95e6e41e8
2 changed files with 99 additions and 11 deletions

View File

@ -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;
/**
* <h1></h1>
*
* <p>create: 2023/7/4 16:34</p>
*
* @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();
}
}

View File

@ -13,17 +13,9 @@ public class FileTreeBuilder {
public static final String FILE_TYPE = "文件"; public static final String FILE_TYPE = "文件";
public static FileInfo buildFileTree() { public static FileInfo buildFileTree() {
String directoryPath = "target/"; String directoryPath = "target";
List<String> blackList = new ArrayList<>(); List<String> blackList = new ArrayList<>();
List<String> 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(); Properties properties = new Properties();
try (InputStream inputStream = FileTreeBuilder.class.getClassLoader().getResourceAsStream("application.properties")) { try (InputStream inputStream = FileTreeBuilder.class.getClassLoader().getResourceAsStream("application.properties")) {
properties.load(inputStream); properties.load(inputStream);
@ -33,7 +25,23 @@ public class FileTreeBuilder {
String[] split = value.split(";"); String[] split = value.split(";");
Collections.addAll(blackList, 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<String> 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); blackList.addAll(blacklistPrefixes);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();