[英]1 Eclipse project, 2 artifacts with Gradle
我想為新項目創建結構,並打算使用Gradle進行構建。 我已經知道,如果將源代碼和測試放在一個項目中,例如MoreUnit插件將輕松處理它,並在需要的地方為我的類創建測試。
但是,當我的項目由彼此依賴的幾個子項目組成時,它會產生一些尷尬的依賴關系問題;確切地說,當我想在項目A的測試中使用一些通用代碼,然后在項目BI的測試中重用它時,必須做一些變通辦法,例如
project(':B') {
// ...
dependencies {
// ...
if (noEclipseTask) {
testCompile project(':A').sourceSets.test.output
}
}
}
有時還存在一些評估問題,因此必須引入另一種技巧:
project(':B') {
evaluationDependsOn(':A')
}
將其拆分為2個獨立的項目可以解決該問題,但隨后MoreUnit不再能夠跟蹤應在何處創建新的測試文件,並標記已測試的方法。 我沒有在MoreUnit配置中找到任何可以修復的內容,因此嘗試從Gradle端進行修復。
我們可以安排事情,以便我可以以maven的方式安排幾個子項目,源代碼和測試( project/src/java
, project/test/java
),但是測試和源代碼會創建單獨的工件嗎? 如果我解決了錯誤的問題,那么我該如何解決正確的問題呢?
您可以為常見的創建一些testenv
jar:
sourceSets {
testenv {
compileClasspath += main.output
runtimeClasspath += main.output
}
}
configurations {
testenvCompile {
extendsFrom runtime
}
testCompile {
extendsFrom testenvRuntime
}
testenvDefault {
extendsFrom testenvRuntime
}
}
和
task testenvJar(type: Jar, group: 'build', description: 'Assembles a jar archive containing the testenv classes.') {
from sourceSets.testenv.output
appendix = 'testenv'
// add artifacts to testenvRuntime as task 'jar' does automatically (see JavaPlugin#configureArchivesAndComponent:106 and http://www.gradle.org/docs/current/userguide/java_plugin.html, "Figure 23.2. Java plugin - dependency configurations")
configurations.testenvRuntime.artifacts.add new org.gradle.api.internal.artifacts.publish.ArchivePublishArtifact(testenvJar)
}
task testenvSourcesJar(type: Jar, group: 'build', description: 'Assembles a jar archive containing all testenv sources.') {
from sourceSets.testenv.allSource
appendix = 'testenv'
classifier = 'sources'
}
artifacts {
archives testenvJar
archives testenvSourcesJar
}
並在您依賴的項目中使用它
testCompile project(path: ':common', configuration: 'testenvDefault')
我希望這有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.