【问题标题】:Java gradle build not recompiling source filesJava gradle build不重新编译源文件
【发布时间】:2020-03-01 23:25:32
【问题描述】:

我有一个 gradle java 项目。我最近将我的源文件重新组织到了 dub 文件夹中。

原来我所有的源文件都在这个文件夹里……

project_root/src/main/java

现在我在...中有类 Foo.java

project_root/src/main/java/folderA

我在...中有类 Bar.java

project_root/src/main/java/folderB

Bar 依赖于 Foo。当我更改类 Foo 时,类 Bar 不会重新编译。因为 Foo 变了,类 Bar 应该有编译错误,但是没有命中。

我的build.gradle文件如下...

apply plugin: 'java'

// Use maven repository
repositories {
    mavenCentral()
    google()
}

dependencies {
    implementation 'org.eclipse.jetty:jetty-servlet:9.4.19.v20190610'
    implementation 'org.eclipse.jetty:jetty-client:9.4.19.v20190610'
    implementation 'mysql:mysql-connector-java:8.0.16'
    implementation 'org.apache.commons:commons-dbcp2:2.7.0'
    implementation 'org.jdbi:jdbi3-core:3.10.1'
    implementation 'com.google.firebase:firebase-admin:6.11.0'
    compile 'com.google.apis:google-api-services-oauth2:v1-rev155-1.25.0'
    // implementation 'com.google.gms:google-services:4.3.3'

    // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'
    compile group: 'org.reflections', name: 'reflections', version: '0.9.11'

    compile project('chesslib')
    runtime files('../../chesslib/build/libs/chesslib-1.1.12.jar')
}

task runServer(type: JavaExec) {
   classpath = sourceSets.main.runtimeClasspath 
   main = 'ChessServer'
} 

task runClient(type: JavaExec) {
   classpath = sourceSets.main.runtimeClasspath 
   main = 'ChessClient'
   standardInput = System.in // without this, using Scanner on System.in won't work
}

task runCLITestApp(type: JavaExec) {
   classpath = sourceSets.main.runtimeClasspath 
   main = 'CLITestApp'
   standardInput = System.in
}

【问题讨论】:

    标签: java gradle build


    【解决方案1】:

    您遇到了 Gradle 中增量编译的限制,如 the documentation 中所述:

    如果包声明和源文件的目录结构不匹配(例如包 foo 与位置 bar/MyClass.java),那么增量编译会产生损坏的输出。可能会重新编译错误的类,并且输出中可能有剩余的类文件。

    所以推荐使用classic包到文件夹结构或者禁用增量编译:

    compileJava {
      options.incremental = false
    }
    

    【讨论】:

      【解决方案2】:

      “当我更改类 Foo 时,类 Bar 不会重新编译。” 不会自动重新编译,需要重新编译项目

      【讨论】:

      • 您是否建议使用 gradle/java 一个简单的构建命令是不够的?我使用过的所有其他构建系统(如 cmake 和 msbuild)都会检测依赖项何时发生变化,并编译所有必需的文件。
      • 我正在使用 gradlew build
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多