Gradle root project depends on subprojects build - gradle

How can I mention subproject should build before root project.
in settings.gradle
rootProject.name = 'loginmodule'
include 'servicebundle'
include 'webbundle'
include 'webredirectbundle'
When I try this build dependson subprojects:build it is giving error like circular dependency.
Currently in my root project build.gradle is bundling all subprojects like below
task createESA(type: Zip, dependsOn: generateSubSystemFile) {
subprojects.each { dependsOn("${it.name}:build") }
from subprojects.collect { "${it.buildDir}/libs" }
from (subsystemFile) {
into 'OSGI-INF'
}
from ('resources/OSGI-INF') {
into 'OSGI-INF'
}
baseName project.name
extension 'esa'
}
build.finalizedBy createESA
I am using gradle clean build to build the project.
Is there any better way to do that ?? I just want to build all subprojects first before root project build.

Have your createESA task depend on subprojects*.build, it'll say that task can't run until all of the build tasks in all of the subprojects have run. Then, declare that the root project's build task depends on createESA.
task createESA(type: Zip, dependsOn: subprojects*.build) {
// etc...
}
build.dependsOn createESA

Related

Specifying common plugin for all the subprojects in Gradle Multi Project Build

I have an empty root project and one subproject
rootProject.name = "worder"
include("worder-core")
I want to make kotlin.jvm plugin to be common for all subprojects, build.gradle.kts(worder):
plugins {
id("org.jetbrains.kotlin.jvm") version "1.3.61" apply false
id("idea")
}
subprojects {
apply(plugin = "kotlin")
}
And then I'm trying to add dependencies to subproject, build.gradle.kts(worder-core):
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.3.3")
}
Okay, lets test it:
cd worder
gradle tasks // OK
cd worder-core
gradle tasks // FAILED -> Unresolved reference: implementation
So, why do I have worder-core project configuration failed ? IDEA doesn't complain at all.
full root build script
full subproject build sript

Gradle copy local jar into cache

I need some help understanding why my build.gradle does not copy locally referred jar into its cache. On Windows, I expected to see jars in C:\Users\myusername .gradle and indeed other JARs are there.
I can't import this JAR in IntelliJ as it does not find it.
Side question: did anybody figure out how to debug Gradle's DSL? I set up remote debugging and IntelliJ never hits my breakpoint. It does not the have the checkbox on it so I guess IDE does not think it is code.
Few things are not making sense to me:
1. gradle dependencies --configuration compile
\--- com.google.guava:guava:23.5-jre
+--- com.google.code.findbugs:jsr305:1.3.9
....
MY_JAR_1.0.1.jar is not present.
2. gradle copyDependencies - this task finds and copies it.
task copyDependencies(type: Copy) {
from configurations.compile
into 'dependencies'
}
3. This task also finds my jar.
task listcompile << {
configurations.compile.each { File file -> println file.name }
}
Why does #1 not find it?
apply plugin: 'java'
repositories {
mavenCentral()
//flatDir {
// dirs 'customlibs'
//}
}
dependencies {
compile group: 'com.google.guava', name: 'guava', version: '23.5-jre'
compile files('customlibs/MY_JAR_1.0.1.jar')
}
task listcompile << {
configurations.compile.each { File file -> println file.name }
}
// copy all dependencies into this folder
task copyDependencies(type: Copy) {
from configurations.compile
into 'dependencies'
}
It seems that Gradle's dependencies task will only display modules that have a defined group:name:version coordinate. You are adding a file directly, with no associated identifier.
What you can do instead is add a flat directory repository and declare your custom JAR as a normal dependency, like so:
repositories {
flatDir {
dirs 'customlibs'
}
}
dependencies {
compile ':MY_JAR:1.0.1'
}
You will need to rename the JAR so that it matches the naming convention for Maven/Gradle, i.e. MY_NAME-1.0.1.jar (replacing the last underscore with a hyphen).

Collect build artifacts from subprojects into RPM with Gradle

I have the following project structure:
Root
+---Sub1
|
+---Sub2
Sub1 project produces JAR, Sub2 creates WAR.
Root project goal is to produce RPM that contains both the Sub1 and Sub2 output.
I'm trying to use ospackage Gradle plugin to build RPM. Here is part of root build.gradle:
ospackage {
from subprojects.collect { it.tasks.withType(Zip) } into 'lib'
}
It works but for Sub2 both JAR and WAR are created, which is undesirable.
What configuration should be applied in order to collect appropriate jars and wars into RPM?
UPDATE
I'm searching for some generic solution, i.e. iteration over subprojects is preferable to specifying behaviour for each subproject.
The solution is to add WAR plugin in Sub2 project's build.gradle:
apply plugin: 'war'
make buildRpm task dependent on all subprojects build tasks in the root build file:
buildRpm.dependsOn getTasksByName('build', true)
and finally configure ospackage plugin to collect the artifacts into the target RPM:
ospackage {
from(subprojects.collect { "${it.buildDir}/libs" }) {
into 'bin'
}
}

How to simplify this Gradle command line?

I am new to Gradle and have gotten a fairly sophisticated build working with it so far (runs tests, uses CodeNarc, generates API docs, generates a sources JAR, etc.). The command line for a "full build" (that is, to execute all the major tasks that the CI server should be running), I need to enter the following Gradle command at the shell:
gradle clean build check groovydoc sourcesJar createPom dist -Pversion=<version>
Where <version> is the version I want to build.
I would like to condense this and add an alias so that all of the above can be accomplished with something simple, like:
gradle full-build
Is this possible? If so, how?
My build.gradle
apply plugin: 'groovy'
apply plugin: 'maven'
apply plugin: 'codenarc'
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.3'
testCompile 'junit:junit:4.11'
}
groovydoc
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourcesJar
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
task dist(type: Zip, dependsOn: build) {
classifier = 'buildreport'
from('build/test-results') {
include '*.xml'
into 'tests'
}
from('build/reports/codenarc') {
into 'reports'
}
from('build/docs') {
into 'api'
}
from(sourcesJar) {
into 'source'
}
from('build/libs') {
exclude '*-sources.jar'
into 'bin'
}
}
task createPom << {
pom {
project {
groupId "me"
artifactId "myapp"
version version
}
}.writeTo("build/libs/myapp-${version}.pom")
}
In the above build invocation, at least check and sourcesJar can be omitted already. (build depends on check, and from(sourcesJar) tells Gradle that dist depends on sourcesJar.) By adding further task dependencies (e.g. build.dependsOn dist), you can cut it down to gradle clean build -Pversion=.... dist should not depend on build but on the specific tasks that produce the artifacts that go into the zip (e.g. groovydoc and createPom).
Reducing gradle clean build to gradle fullBuild is more difficult, as Gradle doesn't currently have a first-class concept of "aliases", and adding build.dependsOn(clean) is usually not desirable. One way out is to implement your own aliasing mechanism by manipulating gradle.startParameter.taskNames.

Gradle Release System Task - Calling uploadArchives on subprojects

In order to release my system, I am creating a releaser project that calls uploadArchives for a number of other projects.
In my project I have the following files:
settings.gradle
include '../proj-a', '../proj-b'
build.gradle
task releaseSystem() {
// what goes here?
}
What should the contents of the releaseSystem task be such that I can call gradle releaseSystem and have it run uploadArchives for each of my sub projects?
I've tried a number of options, but none have been successful thus far.
Thank you for any insights you can provide.
ANSWER
I'm continually impressed with the graceful solutions gradle provides to my problems. Here's my final solution (as Peter pointed out below):
settings.gradle
include 'proj-a', 'proj-b'
project (':proj-a').projectDir = new File(settingsDir, '../proj-a')
project (':proj-b').projectDir = new File(settingsDir, '../proj-b')
build.gradle
task releaseSystem() {
dependsOn {
[
subprojects.build,
subprojects.uploadArchives
]
}
}
Note, that since my repository is a maven repository, when I originally included '../proj-a' in my settings.gradle, it produced poms with artifactId ../proj-a which was invalid. I had to change my settings.gradle to the format above for the system to put the poms together correctly and for the uploadArchives task to complete successfully.
Assuming all subprojects have an uploadArchives task:
task releaseSystem {
dependsOn { subprojects.uploadArchives }
}
Note that this won't run the subprojects' tests.

Resources