我有以下檔案夾結構:
-bin
-build/build.gradle(gradle 腳本)
-lib/[*.jar](專案正在使用的庫)
-src/folder/folder/[*.java](專案的源代碼)
build.gradle 腳本的以下內容:
plugins {
id 'java'
id 'groovy'
}
buildDir = new File('../bin')
sourceCompatibility = JavaVersion.VERSION_1_8
sourceSets {
main {
java {
allJava.srcDirs = [ '../src/folder/folder/ ']
compileClasspath = fileTree('../bin')
}
}
}
repositories {
flatDir {
dirs '../lib'
}
}
dependencies {
implementation fileTree('../lib')
}
tasks.register('javac', JavaCompile) {
println 'Call javac'
source.forEach { e -> println e}
classpath = sourceSets.main.compileClasspath
destinationDirectory = file('../bin')
source sourceSets.main.allJava.srcDirs
includes.add('*.java')
sourceCompatibility = JavaVersion.VERSION_1_8
}
運行時gradle javac 出現錯誤: error: cannot find symbol import com...
檔案清楚地說:
dependencies {
.
.
.
//putting all jars from 'libs' onto compile classpath
implementation fileTree('libs')
}
我正在使用 Gradle 7.3.1
uj5u.com熱心網友回復:
請允許我先給你一些一般性的建議。我強烈推薦使用 Kotlin DSL 而不是 Groovy DSL。您可以立即在構建腳本中獲得強型別代碼和更好的 IDE 支持。
此外,您還應該考慮將您的專案布局更改為更像大多數其他 Java 專案,尤其是不要使用libs目錄,而是在存盤庫中使用普通依賴項,然后自動處理傳遞依賴項等等。
但是要回答您的實際問題,這是您想要的 Groovy DSL 中的構建完整構建腳本:
plugins {
id 'java'
}
buildDir = '../bin'
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(8))
}
}
sourceSets {
main {
java {
srcDirs = ['../src/folder/folder']
}
}
}
dependencies {
implementation fileTree('../lib')
}
這是匹配的 Kotlin DSL 版本:
plugins {
java
}
setBuildDir("../bin")
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(8))
}
}
sourceSets {
main {
java {
setSrcDirs(listOf("../src/folder/folder"))
}
}
}
dependencies {
implementation(fileTree("../lib"))
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/387121.html
上一篇:Gradle-將任務輸出寫入檔案
