Browse Source

优化gradle配置

johnclot69 2 months ago
parent
commit
ad79e53cec
3 changed files with 40 additions and 19 deletions
  1. 4 7
      build.gradle
  2. 26 4
      incubator-center/build.gradle
  3. 10 8
      incubator-game/build.gradle

+ 4 - 7
build.gradle

@@ -10,6 +10,8 @@ subprojects {
     }
 
     repositories {
+        // 优先使用本地 Maven 缓存
+        mavenLocal()
         // 阿里云公共仓库
         maven { url "https://maven.aliyun.com/repository/public" }
         // 阿里云 Gradle 插件仓库
@@ -18,16 +20,11 @@ subprojects {
         mavenCentral()
     }
 
-    // 排除不必要的依赖,恢复配置
-//    configurations {
-//        all {
-//            exclude group: 'ch.qos.logback'
-//        }
-//    }
-
     // 配置编译选项
     tasks.withType(JavaCompile).configureEach {
         options.encoding = 'UTF-8'
+        options.incremental = true // 启用增量编译
+        options.compilerArgs += ['-Xlint:unchecked', '-Xlint:deprecation']
     }
 
     // 复制依赖任务,使用更现代的 `runtimeClasspath`

+ 26 - 4
incubator-center/build.gradle

@@ -7,13 +7,26 @@ plugins {
 group = 'com.incubator.center'
 version = '1.0-SNAPSHOT'
 
-//repositories {
-//    mavenCentral()  // 使用中央仓库
-//}
+sourceSets {
+    main {
+        resources {
+            srcDirs += 'conf'
+        }
+    }
+}
 
 application {
     // 使用新的方法设置主类,适应 Gradle 7.0 及以上版本
     mainClass.set('com.incubator.center.CenterServerStart')
+
+    // 添加 JVM 参数,指定 conf 目录下的配置文件路径
+    applicationDefaultJvmArgs = [
+            "-Dlog4j.configurationFile=conf/log4j2.xml",
+            "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005", // 启用远程调试
+            "-XX:+UseG1GC",                   // 使用 G1 GC
+            "-XX:MaxGCPauseMillis=200",       // 控制 GC 停顿时间
+            "-XX:+HeapDumpOnOutOfMemoryError" // 发生 OOM 时生成堆转储
+    ]
 }
 
 // 禁用默认的 JAR 任务,因为我们使用 Shadow JAR
@@ -33,11 +46,20 @@ tasks.shadowJar {
 
     // 添加自定义清单
     manifest {
-        attributes 'Main-Class': application.mainClass.get()  // 使用动态设置的主类
+        // 使用动态设置的主类
+        attributes 'Main-Class': application.mainClass.get()
+    }
+
+    // 将 conf 目录包含到最终的 JAR 文件中
+    from('conf') {
+        into('conf') // 指定 JAR 内的路径
     }
 
     // 可选:排除不必要的文件(例如签名文件)
     exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA'
+
+    // 避免重复打包无关的文件
+    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
 }
 
 // 版本号集中管理

+ 10 - 8
incubator-game/build.gradle

@@ -7,10 +7,6 @@ plugins {
 group = 'com.incubator.game'
 version = '1.0-SNAPSHOT'
 
-//repositories {
-//    mavenCentral()
-//}
-
 sourceSets {
     main {
         resources {
@@ -25,7 +21,11 @@ application {
 
     // 添加 JVM 参数,指定 conf 目录下的配置文件路径
     applicationDefaultJvmArgs = [
-            "-Dlog4j.configurationFile=conf/log4j2.xml"
+            "-Dlog4j.configurationFile=conf/log4j2.xml",
+            "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005", // 启用远程调试
+            "-XX:+UseG1GC",                   // 使用 G1 GC
+            "-XX:MaxGCPauseMillis=200",       // 控制 GC 停顿时间
+            "-XX:+HeapDumpOnOutOfMemoryError" // 发生 OOM 时生成堆转储
     ]
 }
 
@@ -46,9 +46,8 @@ tasks.shadowJar {
 
     // 添加自定义清单
     manifest {
-        attributes(
-                'Main-Class': application.mainClass.get() // 使用动态设置的主类
-        )
+        // 使用动态设置的主类
+        attributes 'Main-Class': application.mainClass.get()
     }
 
     // 将 conf 目录包含到最终的 JAR 文件中
@@ -58,6 +57,9 @@ tasks.shadowJar {
 
     // 可选:排除不必要的依赖
     exclude 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA'
+
+    // 避免重复打包无关的文件
+    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
 }
 
 // 依赖版本集中管理