官網 repository 連結
※
buildscript { ScriptHandler sh ->
sh.repositories { RepositoryHandler rh ->
rh.jcenter()
rh.mavenLocal()
rh.mavenCentral()
rh.maven { MavenArtifactRepository mar ->
mar.url "http://repo.mycompany.com/maven2"
mar.credentials { PasswordCredentials pc ->
pc.username "user"
pc.password "password"
}
}
}
sh.dependencies {
classpath group: 'junit', name: 'junit', version: '4.12'
}
}
※這是還不熟練的寫法,可以將閉包參數去掉,因為已經有預設 import 了
※
buildscript {
repositories {
jcenter()
mavenLocal()
mavenCentral()
maven {
url "http://repo.mycompany.com/maven2"
credentials {
username "user"
password "password"
}
}
}
dependencies {
classpath project(':subGradle1') // 依賴其他專案
classpath fileTree(includes: ['*.jar', '*.war'], dir: 'libs')
}
}
※在 buildscript 同層,也有 repositories 和 dependencies
寫在 buildscript 裡是針對寫 gradle 時用的 jar 包; 寫在 buildscript 同層是針專案要用的 jar 包
Could not find method xxx() for arguments:不寫 classpath 會出的錯
Cannot resolve external dependency xxx because no repositories are defined:在buildscript 裡沒有repositories 出現的錯
※依賴裡,舊版的
https://docs.gradle.org/4.10.3/userguide/building_java_projects.html
https://docs.gradle.org/5.6.4/userguide/building_java_projects.html
https://docs.gradle.org/current/userguide/java_library_plugin.html
4~6 版
compileOnly
implementation (取代 compile)
runtimeOnly (取代 runtime)
testCompileOnly
testImplementation
testRuntimeOnly
只有第 6 版有
api
※依賴衝突
maven 的依賴是最短路徑優先,路徑一樣就是先寫的優先
gradle 則是最新的版本優先
以 hibernate 3.6.3 為例,有以下三種方法
configurations.all {
resolutionStrategy {
failOnVersionConflict() // 不要自動處理版本衝突
}
}
configurations.all {
resolutionStrategy {
force 'org.slf4j:slf4j-api:1.7.24' // 強制使用某一個版本
}
}
dependencies {
implementation (group: 'org.hibernate', name: 'hibernate-core', version: '3.6.3.Final') {
exclude group:"org.slf4j" , module:"slf4j-api" // 排除依賴後,還要自己在下載
// transitive=false // 所有依賴都不要,debug 用
}
}
沒有留言:
張貼留言