Files
window-axis-innovators-box1.17/build.gradle
tzdwindows 7 b20c3fc340 perf(runtime): 优化 Java 应用启动性能和内存管理
- 添加 JVM 启动参数提升启动速度,包括设置 TieredStopAtLevel=1 和跳过字节码验证
- 配置类数据共享 (CDS) 以减少类加载时间
- 限制堆内存大小 (Xms128m/Xmx512m) 和元空间大小防止内存过度占用
- 启用 G1 垃圾回收器并设置最大 GC 暂停时间为 50ms 优化 UI 响应
- 开启字符串去重功能节省 10%-20% 内存占用
- 添加 OpenGL 硬件加速和 macOS 菜单栏优化提升 Swing 渲染性能
2026-01-03 20:13:22 +08:00

274 lines
11 KiB
Groovy
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import org.gradle.internal.os.OperatingSystem
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.1.0'
id 'org.springframework.boot' version '3.2.0'
id 'io.spring.dependency-management' version '1.1.4'
id 'edu.sc.seis.launch4j' version '2.5.4'
}
group = 'com.axis.innovators.box'
version = '0.0.1'
// JDK 版本检查
java {
toolchain {
languageVersion = JavaLanguageVersion.of(20)
}
}
repositories {
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
maven { url 'https://jitpack.io' }
mavenCentral()
}
// 操作系统 Native 标识符
def lwjglNatives = ""
if (OperatingSystem.current().isWindows()) {
lwjglNatives = "natives-windows"
} else if (OperatingSystem.current().isLinux()) {
lwjglNatives = "natives-linux"
} else if (OperatingSystem.current().isMacOsX()) {
lwjglNatives = System.getProperty("os.arch") == "aarch64" ? "natives-macos-arm64" : "natives-macos"
}
javafx {
version = "21"
modules = [ 'javafx.controls', 'javafx.graphics', 'javafx.web', 'javafx.swing' ]
}
// 排除冲突的日志和旧版 FX
configurations {
all*.exclude group: 'org.openjfx', module: 'javafx'
all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
dependencies {
// === 测试框架 ===
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
// === 开发工具 ===
developmentOnly 'org.springframework.boot:spring-boot-devtools'
// === JnaFileChooser & Swing ===
implementation 'com.github.steos.jnafilechooser:jnafilechooser-api:1.1.2'
implementation 'com.github.steos.jnafilechooser:jnafilechooser-win32:1.1.2'
implementation 'org.swinglabs:swingx:1.6.1'
// === 本地库文件 ===
implementation fileTree(dir: 'libs', include: ['*.jar'])
// === AI (DJL) ===
implementation platform('ai.djl:bom:0.35.0')
implementation 'ai.djl:api'
implementation 'ai.djl:model-zoo'
implementation 'ai.djl.pytorch:pytorch-model-zoo:0.35.0'
implementation 'ai.djl.pytorch:pytorch-engine'
implementation 'ai.djl:basicdataset'
implementation 'ai.djl.onnxruntime:onnxruntime-engine'
runtimeOnly 'ai.djl.pytorch:pytorch-native-cpu:2.7.1'
runtimeOnly 'ai.djl.onnxruntime:onnxruntime-native-cpu:1.3.0'
// === 核心工具库 ===
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'org.apache.logging.log4j:log4j-api:2.20.0'
implementation 'org.apache.logging.log4j:log4j-core:2.20.0'
implementation 'org.apache.logging.log4j:log4j-slf4j2-impl:2.20.0'
implementation 'commons-io:commons-io:2.18.0'
implementation 'com.google.guava:guava:31.1-jre'
implementation 'net.java.dev.jna:jna:5.13.0'
implementation 'net.java.dev.jna:jna-platform:5.13.0'
implementation 'org.apache.commons:commons-math3:3.6.1'
implementation 'org.apache.commons:commons-compress:1.23.0'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2'
// === 字节码操作 ===
implementation 'org.ow2.asm:asm:9.7.1'
implementation 'org.ow2.asm:asm-commons:9.7.1'
implementation 'org.ow2.asm:asm-analysis:9.7.1'
implementation 'org.ow2.asm:asm-util:9.7.1'
implementation 'org.ow2.asm:asm-tree:9.7.1'
implementation 'net.bytebuddy:byte-buddy:1.17.6'
// === 反编译工具 ===
implementation 'org.bitbucket.mstrobel:procyon-core:0.6.0'
implementation 'org.bitbucket.mstrobel:procyon-compilertools:0.6.0'
implementation 'org.benf:cfr:0.152'
// === Java 解析与分析 ===
implementation 'com.github.javaparser:javaparser-core:3.25.1'
implementation 'com.github.javaparser:javaparser-symbol-solver-core:3.25.9'
// === 文本处理 ===
implementation 'org.commonmark:commonmark:0.24.0'
implementation 'org.commonjava.googlecode.markdown4j:markdown4j:2.2-cj-1.1'
implementation 'com.vladsch.flexmark:flexmark:0.64.8'
// === Web 和网络 ===
implementation 'org.jsoup:jsoup:1.17.2'
implementation 'org.json:json:20231013'
implementation 'org.openjfx:javafx-web:21'
// === UI 框架 ===
implementation 'com.formdev:flatlaf:3.2.1'
implementation 'com.formdev:flatlaf-extras:3.2.1'
implementation 'com.formdev:flatlaf-intellij-themes:3.2.1'
implementation 'io.github.vincenzopalazzo:material-ui-swing:1.1.2'
implementation 'org.fxmisc.richtext:richtextfx:0.11.0'
implementation 'org.controlsfx:controlsfx:11.1.2'
implementation 'com.dlsc.formsfx:formsfx-core:11.6.0'
implementation 'com.dustinredmond.fxtrayicon:FXTrayIcon:4.0.1'
// === 代码编辑器 ===
implementation 'com.fifesoft:rsyntaxtextarea:3.5.4'
implementation 'com.fifesoft:rstaui:3.3.1'
implementation 'com.fifesoft:languagesupport:3.3.0'
implementation 'com.fifesoft:autocomplete:3.3.2'
// === LWJGL 图形引擎 ===
def lwjglVersion = "3.3.6"
implementation "org.lwjgl:lwjgl:$lwjglVersion"
implementation "org.lwjgl:lwjgl-stb:$lwjglVersion"
implementation "org.lwjgl:lwjgl-glfw:$lwjglVersion"
implementation "org.lwjgl:lwjgl-opengl:$lwjglVersion"
implementation "org.lwjgl:lwjgl-jawt:$lwjglVersion"
implementation("com.github.LWJGLX:lwjgl3-awt:bc8daf521a") {
exclude group: "org.lwjgl"
}
// LWJGL Natives
runtimeOnly "org.lwjgl:lwjgl:$lwjglVersion:$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-glfw:$lwjglVersion:$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-opengl:$lwjglVersion:$lwjglNatives"
runtimeOnly "org.lwjgl:lwjgl-stb:$lwjglVersion:$lwjglNatives"
// 其他图形库
implementation 'com.badlogicgames.gdx:gdx:1.12.1'
implementation 'org.joml:joml:1.10.7'
implementation 'com.kitfox.svg:svg-salamander:1.0'
implementation 'net.sourceforge.plantuml:plantuml:8059'
implementation 'com.twelvemonkeys.imageio:imageio-psd:3.12.0'
// === 图像处理 ===
implementation 'com.madgag:animated-gif-lib:1.4'
implementation 'org.bytedeco:javacv-platform:1.5.7'
implementation 'org.bytedeco:javacpp-platform:1.5.7'
// === 编程语言支持 ===
implementation 'org.python:jython-standalone:2.7.3'
implementation 'org.graalvm.python:python-embedding:24.2.1'
// === 系统交互 ===
implementation 'com.github.kwhat:jnativehook:2.2.2'
implementation 'com.1stleg:jnativehook:2.1.0'
// === 数据库 ===
runtimeOnly 'com.mysql:mysql-connector-j'
implementation 'mysql:mysql-connector-java:8.0.33'
implementation 'com.h2database:h2:2.2.220'
implementation 'org.xerial:sqlite-jdbc:3.41.2.1'
implementation 'org.postgresql:postgresql:42.6.0'
// === 伪终端与测试容器 ===
implementation 'org.jetbrains.pty4j:pty4j:0.12.13'
implementation 'org.testcontainers:testcontainers:1.19.0'
// === 音频处理 ===
implementation 'jflac:jflac:1.3'
implementation 'com.github.axet:TarsosDSP:2.4'
implementation 'com.googlecode.soundlibs:mp3spi:1.9.5-1'
implementation 'com.googlecode.soundlibs:vorbisspi:1.0.3-2'
implementation 'com.googlecode.soundlibs:jorbis:0.0.17-2'
// === 语音识别 ===
implementation 'com.alphacephei:vosk:0.3.45'
// === 浏览器引擎 ===
implementation 'me.friwi:jcefmaven:122.1.10'
// === 中文处理 ===
implementation 'com.belerweb:pinyin4j:2.5.1'
// === 安全认证 ===
implementation 'cn.dev33:sa-token-spring-boot-starter:1.44.0'
implementation 'org.casbin:casdoor-java-sdk:1.37.0'
}
// 自动生成构建信息文件
tasks.register('generateBuildProperties') {
group = 'build'
doLast {
def resDir = file("src/main/resources/build")
resDir.mkdirs()
def propertiesFile = new File(resDir, 'build.properties')
propertiesFile.text = """# Auto-generated build information
version=${project.version}
buildTimestamp=${LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME)}
buildSystem=${OperatingSystem.current().isWindows() ? "WINDOWS" : "UNIX"}
"""
}
}
compileJava.dependsOn generateBuildProperties
// 复制依赖包到构建目录
tasks.register('copyDependencies', Copy) {
group = "build"
from configurations.runtimeClasspath
into "$buildDir/libs/libs"
}
tasks.jar {
dependsOn copyDependencies
archiveBaseName.set("${rootProject.name}")
manifest {
attributes(
'Main-Class': 'com.axis.innovators.box.Main',
'Class-Path': configurations.runtimeClasspath.files.collect { "libs/${it.name}" }.join(' ')
)
}
}
application {
mainClass = 'com.axis.innovators.box.Main'
}
tasks.register('runBoxClient', JavaExec) {
group = "run"
classpath = sourceSets.main.runtimeClasspath
mainClass = "com.axis.innovators.box.Main"
jvmArgs = [
"-Dfile.encoding=UTF-8",
"-Djava.system.class.loader=com.axis.innovators.box.plugins.BoxClassLoader",
// --- 1. 提升启动速度的关键 ---
"-XX:TieredStopAtLevel=1", // 最有效的启动优化:只进行一级编译,不进入长时间的深度优化,显著缩短冷启动时间
"-Xverify:none", // 跳过字节码验证(注意:如果插件系统有安全性要求请移除此项)
"-Xshare:auto", // 开启类数据共享 (CDS),减少类加载时间
// --- 2. 限制内存占用 ---
"-Xms128m", // 初始堆大小根据你的应用复杂度可调128m 对普通桌面应用足够)
"-Xmx512m", // 最大堆限制,防止 Java 无节制占用系统内存
"-XX:MaxMetaspaceSize=128m", // 限制元空间(存放类信息),防止非堆内存泄露
"-XX:+UseStringDeduplication", // G1回收器下开启字符串去重可节省 10%-20% 的内存占用
// --- 3. 优化 UI 响应与 GC ---
"-XX:+UseG1GC", // 使用 G1 垃圾回收器,适合有界面交互的程序,减少停顿
"-XX:MaxGCPauseMillis=50", // 目标 GC 停顿时间 50ms保证 UI 动画不卡顿
// --- 4. 针对 Swing 的渲染优化 (可选) ---
"-Dsun.java2d.opengl=true", // 在支持的系统上启用 OpenGL 硬件加速(通常能让 UI 更流畅)
"-Dapple.laf.useScreenMenuBar=true" // 如果是 macOS将菜单栏移至屏幕顶部
]
}