私は本当に、特に新しいKotlin DSLとの組み合わせでのGradle 5に感謝したいと思いますが、私は(私の目に)のGradleで実行されている非常に、非常にシンプルで一般的なビルドを取得するために非常に苦労しています。
仕事
Javaライブラリをリリースいくつかと相互依存のサブモジュール Mavenのデフォルトのディレクトリレイアウトで高品質のMavenアーティファクト/リポジトリとしての-ポイント、シンプルなGradleのビルド(すなわちでDRY)。
したがって:傘を定義としてルートプロジェクトを持っている&すべての一般的な構成を(実質的にすべての実際の依存関係を除く)が含まれています。
私の現在の闘争
私はに私の現在の「結果」を移植のGithub上のサンプルプロジェクトとすでにGradleのフォーラムでこの質問をしました。
現在、私は、標準的な提供するために必要なタスクを宣言することができないんだ-sources
と-javadoc
、私の中で成果物を中央のビルドを。
あなたはKotlin DSLベースのソリューションを探しに見つけることができますこれらの三つの「ソリューション」たとえば、まったく(もはや)仕事をしていないマルチモジュールのシナリオには:
- https://stackoverflow.com/a/48070667
- https://stackoverflow.com/a/52596969/1237653
- そして、さえ公式ドキュメントは、「Mavenは公開」のみの単一のモジュールのシナリオに取り組んでいます。
不完全なソリューション(/build.gradle.kts
)
Githubの上に完全な例を参照してください:https://github.com/bentolor/gradle-maven-multimodule-kotlindsl
subprojects {
apply(plugin = "java-library")
apply(plugin = "maven-publish")
group = "de.bentolor.sampleproject"
version = "0.1.0"
repositories {
jcenter()
}
dependencies {
// Dependencies used in EVERY module
"compile"("commons-logging:commons-logging:1.2")
"testImplementation"("junit:junit:4.12")
}
tasks {
// not working
/*register("sourcesJar", Jar::class.java) {
from(sourceSets.main.get().allJava)
classifier = "sources"
}*/
// not working, eiher
/* task<Jar>("sourcesJar") {
from(sourceSets.main.get().allJava)
classifier = "sources"
} */
}
configure<JavaPluginExtension> {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
configure<PublishingExtension> {
publications {
create<MavenPublication>(project.name) {
from(components["java"])
// won't work, beause inaccessible declaration in `tasks{}`-Block
//add("archives", javadocJar)
//add("archives", sourcesJar)
}
}
repositories {
mavenLocal()
}
}
}
例サブモジュール /module2/build.gradle.kts
group = "de.bentolor.sampleproject.module2"
dependencies {
compile(project(":module1"))
}
これを試して:
subprojects {
apply<JavaLibraryPlugin>()
apply<MavenPublishPlugin>()
group = "de.bentolor.sampleproject"
version = "0.1.0"
repositories {
jcenter()
}
dependencies {
val implementation by configurations
val testImplementation by configurations
implementation("commons-logging:commons-logging:1.2")
testImplementation("junit:junit:4.12")
}
// This will work, but as long as these tasks are need only for publishing you can declare them inplace later where you need
// tasks {
// val sourcesJar by creating(Jar::class) {
// val sourceSets: SourceSetContainer by project
// from(sourceSets["main"].allJava)
// classifier = "sources"
// }
// val javadoc by getting(Javadoc::class)
// val javadocJar by creating(Jar::class) {
// from(javadoc)
// classifier = "javadoc"
// }
// }
configure<JavaPluginExtension> {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
configure<PublishingExtension> {
publications {
create<MavenPublication>(project.name) {
from(components["java"])
// If you configured them before
// val sourcesJar by tasks.getting(Jar::class)
// val javadocJar by tasks.getting(Jar::class)
val sourcesJar by tasks.creating(Jar::class) {
val sourceSets: SourceSetContainer by project
from(sourceSets["main"].allJava)
classifier = "sources"
}
val javadocJar by tasks.creating(Jar::class) {
from(tasks.get("javadoc"))
classifier = "javadoc"
}
artifact(sourcesJar)
artifact(javadocJar)
}
}
}
}
いくつかの注意事項:
- なぜ使用は
String
ベースapply
あなたがタイプセーフを行うことができたときに、apply<T>()
? - で刺されになぜ使用呼び出す
dependencies
、あなたはあまりハック、より良いrefactorableである、デリゲートを使用することができます。 - 使用を検討して
implementation
の代わりに、compile
なぜsourceSets
マルチモジュールプロジェクトで作業されていませんか?
あなたはKotlin DSLを使用している場合は、それが適用されたプラグインに基づいてプロジェクトのためのアクセサを生成します。(それらを置くことをお勧めしている理由だと最初のGradleプロセスプラグイン:これは、2つの段階のプロセスだplugins
ブロック)とアクセサを生成し、あなたのコード内でそれらを使用することができます(アクセサがためKotlinの拡張として生成されProject
、NamedDomainObjectContainer
など)。しかし、あなたがサブプロジェクトを設定している場合は2つの問題があります。
- 親プロジェクトの子の前に評価し、その子のための拡張機能は、親には知られていません。
- プラグインのセットは、親に適用され、子供が異なっていると、あなたは親に子どものアクセサを使用する必要があります。
sourceSets
子供たちのためにKotlin DSLによって生成されたアクセサの一つです。そして、それは親にだけ利用できません。あなたはそれを自分で試すことができますのみ適用java
してプラグインsubprojects
。sourceSets
子供のビルドスクリプトで利用可能ではなく、親になります。
これは、あなたが使用できる理由もありjava
、子供ではなく、使用する必要がありconfigure<JavaPluginExtension>
、親にそれを構成するとき。
しかし、あなたはそうでタスク、ソースセット、設定などのドメインオブジェクトの参照を取得するために、デリゲートを使用することができます。