You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
5.7 KiB
Groovy

import java.nio.file.StandardCopyOption
import java.util.zip.ZipEntry
plugins {
id 'java'
}
group 'com.weaver.seconddev'
version '1.0.2'
description 'asd'
java.targetCompatibility = JavaVersion.VERSION_1_8
compileJava {
options.encoding = 'UTF-8'
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
jar {
manifest {
attributes 'weaver-ecode-seconddev-id': rootProject.group + '-' + rootProject.name,
'Implementation-Version': version,
'Implementation-Vendor-Id': rootProject.group,
'Implementation-Title': rootProject.name
}
}
repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
mavenLocal()
mavenCentral()
}
// task
// 三方包处理
def createJarFlagId(File jarFile) {
return "${rootProject.group}-${rootProject.name}-lib^${jarFile.getName()}";
}
def appendFlagIdToMF(File jarFile) {
println("====> 处理jar包: ${jarFile.getName()}");
// 重新写入 jar包内
def zipIn = new java.util.zip.ZipInputStream(new FileInputStream(jarFile));
def tmpFile = new java.io.File(System.getProperty("java.io.tmpdir") + File.separator + jarFile.getName());
def zipOut = new java.util.zip.ZipOutputStream(new FileOutputStream(tmpFile));
def entry = null;
while ((entry = zipIn.getNextEntry()) != null) {
if (!entry.isDirectory() && entry.getName().equalsIgnoreCase("META-INF/MANIFEST.MF")) {
// 如果是要修改的文件,将修改后的内容写入内存
def reader = new BufferedReader(new InputStreamReader(zipIn))
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null ) {
if (line.length() > 0) {
if (sb.length() == 0) {
sb.append(line);
} else {
sb.append("\n").append(line);
}
}
}
// 修改内容
def content = sb.toString();
if (!content.contains('weaver-ecode-seconddev-id')) {
// 去除为首空格
content = content.trim();
// 添加id
def createId = createJarFlagId(jarFile);
println("添加flagId => ${createId}");
content += "\nweaver-ecode-seconddev-id: " + createId + "\n";
}
byte[] modifiedBytes = content.getBytes();
def byteArrayOutputStream = new ByteArrayOutputStream();
byteArrayOutputStream.write(modifiedBytes);
// 将修改后的内容写回 ZIP 文件
def modifiedEntry = new java.util.zip.ZipEntry(entry.getName());
zipOut.putNextEntry(modifiedEntry);
zipOut.write(byteArrayOutputStream.toByteArray());
zipOut.closeEntry();
} else {
zipOut.putNextEntry(new java.util.zip.ZipEntry(entry.getName()));
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = zipIn.read(buffer)) != -1) {
zipOut.write(buffer, 0, bytesRead);
}
zipOut.closeEntry();
}
}
zipIn.close();
zipOut.close();
// 调整jar包的原有名称_开头
def filename = jarFile.getName();
if (!filename.startsWith("_")) {
filename = '_' + filename;
}
java.nio.file.Files.move(tmpFile.toPath(), new File(jarFile.getParentFile(), filename).toPath(),
java.nio.file.StandardCopyOption.REPLACE_EXISTING);
}
task processDevLib() {
doFirst {
println("====> 处理第三方包manifest文件");
// 扫描第三方包目录
File devDir = new File("${project.rootDir}/devLib");
if (Objects.nonNull(devDir) && devDir.isDirectory()) {
for (File file : devDir.listFiles()) {
if (file.getName().endsWith(".jar") && !file.getName().startsWith("_")) {
try {
appendFlagIdToMF(file)
} catch (Exception e) {
println("自动处理失败:${file.getName()}")
e.printStackTrace()
}
};
}
}
}
}
task fixSoftLink() {
doLast {
println("====> 替换secDevLib中的软连接为目标文件")
println("====> secDevLib path: ${project.rootDir}/secDevLib")
File secDir = new File("${project.rootDir}/secDevLib");
if (!secDir.exists() || !secDir.isDirectory()) {
return;
}
File[] jars = secDir.listFiles();
for (File jarFile : jars) {
if (jarFile.getAbsolutePath().equals(jarFile.getCanonicalPath())) continue;
// 判断如果是目标链接则拷贝替换为实际文件
java.nio.file.Path symbolicLink = java.nio.file.Paths.get(jarFile.toURI());
java.nio.file.Path realPath = symbolicLink.toRealPath();
println("jar file => ${jarFile.getName()}, realPath => ${realPath.toFile().getPath()}")
java.nio.file.Files.copy(realPath, jarFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
}
dependencies {
// implementation ('org.springframework.boot:spring-boot-starter-tomcat') {
// exclude group: 'ch.qos.logback', module: 'logback-classic'
// } // java.lang.NoClassDefFoundError: org/apache/neethi/AssertionBuilderFactory
// 添加二开服务拉取的清单依赖
def includeType = ['**/*.jar', '**/*.class']
implementation fileTree(dir: project.projectDir.getPath() + '/secDevLib', includes: includeType)
// 项目二开自定义依赖
implementation fileTree(dir: project.projectDir.getPath() + "/devLib", includes: includeType)
}