Question

I have a multi project, and I want to upload some of the subprojects artifact to the maven repository. For now, I wrote the following code into the main build.gradle:

task sourcesJar(type: Jar, dependsOn: classes) { ... }

project(':subProjName1') {
   apply plugin: 'java'
   apply plugin: 'maven'

   configurations {
       subProjName1Archives
   }

   uploadSubProjName1Archives {
       repositories {
           mavenDeployer {
               repository(url: "file:///$rootDir/mvn-repo/")
           }
       }
   }

   artifacts {
       subProjName1Archives jar
       subProjName1Archives sourcesJar
   }
}

project(':subProjName2') { ... }
...
project(':subProjNameN') { ... }

And do following for upload archives:

gradlew.bat uploadSubProjName1Archives
gradlew.bat uploadSubProjName2Archives
...
gradlew.bat uploadSubProjNameNArchives

It's doing what I want, but how can I generalize it into one task in the main build.gradle?

Was it helpful?

Solution

If you put the above code into a subprojects { .. } block in the root build script, you can invoke all tasks at once with gradle uploadMyConfiguration. (Only) if you have a concrete need for a single task (e.g. because another task depends on all artifacts being uploaded), you can add a further lifecycle task:

task uploadAll {
    dependsOn { subprojects.uploadMyConfiguration }
}

PS: Unless you have a good reason not to, you can reuse the existing archives configuration and uploadArchives task. The archives configuration already contains the Jar produced by the jar task, so you just have to add the sources Jar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top