I have a setup where the maven-publish plugin is applied to the root project and publications are defined for sub-projects (say a, b, and c). I need to publish projects a, b, c (in that order), when project a is published.
I tried to establish a finalizedBy relationship between the publish tasks but the “downstream” tasks are always reported as being UP-TO-DATE and not executed. I also tried to force UP-TO-DATE to false in projects b and c, with no success.
Only the artifacts of a are published, though, even when running it with --rerun-tasks. Is it a limitation of the maven-publish plugin or am I taking the wrong approach? I get similar results when trying to establish the dependency with dependsOn. I am using Gradle 5.5.1
The publish task is not the task doing the actual publication. The task sending artifacts to the repository is named publish<PublicationName>To<RepositoryName>. The publish task has only a dependency on that task. It is the same as the assemble task: it is not the one that assembles (compile sources or archive binaries), it is simply a shortcut.
If you decide to ignore these warnings and couple your tasks, then, maybe one of the most harmless way to do it would be:
rootProject/build.gradle:
/* In production code, I'd rather apply a convention plugin defined in buildSrc to subprojects
to avoid coupling, but for the sake of brevity I'll use the subprojects idiom */
subprojects {
apply plugin: 'java-library'
apply plugin: 'maven-publish'
publishing {
repositories {
maven {
url = "file:///${project.rootProject.buildDir}/repo"
name = "drive"
}
}
publications {
jar(MavenPublication) {
from components.java
}
}
}
}
Then to make sure that publications of foo happens before publications of bar, in bar/build.gradle:
tasks.withType(PublishToMavenRepository).configureEach { t ->
t.mustRunAfter(provider { project(':foo').tasks.withType(PublishToMavenRepository) })
}
Should you value having uncoupled projects at configuration and runtime, then a third party tool ordering the task execution will be necessary like a shell script, or a Jenkins pipeline, for instance:
node {
stage('publishing a') {
sh 'gradle :a:publish'
}
stage('publishing b') {
sh 'gradle :b:publish'
}
stage('publishing c') {
sh 'gradle :c:publish'
}
}