How to collect jars with sources from projects in dependencies list? - gradle

I have a simple multi-project build:
root
|___ a
|___ b
|___ c
build.gradle in root project:
subprojects {
task jarSources(type: Jar, dependsOn: classes) {
classifier = 'source'
from sourceSets.main.java, sourceSets.main.resources
}
}
build.gradle in project a:
dependencies {
compile project(':b')
compile project(':c')
}
task archiveDependencySources(type: Zip) {
...
}
Task archiveDependencySources is intended to collect all jars with sources from projects on which project a depends. Is there a standard way to do this job?
So far I found the solution that looks a bit ugly:
def allJarSourcesTasks = []
for (def dep : configurations.compile.dependencies)
if (dep.hasProperty('dependencyProject'))
allJarSourcesTasks << dep.dependencyProject.tasks['jarSources']
archiveDependencySources.dependsOn allJarSourcesTasks
archiveDependencySources.from allJarSourcesTasks

This might work (not sure about the null argument):
allJarSourcesTasks = configurations.compile.getTaskDependencyFromProjectDependency(true, "jarSources").getDependencies(null)
For dependsOn, .getDependencies(null) could be omitted, but I believe it is needed for from.

Related

how does gradle resolve conflicting dependency versions

Say I have 3 modules with 3 different build.gradle property files.
Module A v1 has the following entries in build.gradle
ATLAS_VERSION = 1
Module B v1 has the following entries in build.gradle
ATLAS_VERSION = 2
MODULE_A_VERSION = 1
Module C v1 has the following entries in its build.gradle
ATLAS_VERSION = 3
MODULE_B_VERSION = 1
So my question is: what ATLAS version will be resolved during runtime?
According to this Gradle documentation Managing Transitive Dependencies, in case you don't specify any specific constraints for transitive dependencies resolution, the highest version of ATLAS modules should be selected:
When Gradle attempts to resolve a dependency to a module version, all dependency declarations with version, all transitive dependencies and all dependency constraints for that module are taken into consideration. The highest version that matches all conditions is selected.
You can quickly test this behavior with simple multi-project build below:
settings.gradle
rootProject.name = 'demo'
include "A", "B", "C"
build.gradle
subprojects{
apply plugin: "java"
repositories{
mavenCentral()
}
}
project(':A') {
dependencies{
implementation 'commons-io:commons-io:1.2'
}
}
project(':B') {
dependencies{
implementation project(":A")
implementation 'commons-io:commons-io:2.0'
}
}
project(':C') {
dependencies{
implementation project(":B")
implementation 'commons-io:commons-io:2.6'
}
}
You can then check which version of commons-io has been selected, which is 2.6 :
./gradlew C:dependencies
runtimeClasspath - Runtime classpath of source set 'main'.
+--- project :B
| +--- project :A
| | \--- commons-io:commons-io:1.2 -> 2.6
| \--- commons-io:commons-io:2.0 -> 2.6
\--- commons-io:commons-io:2.6

Antlr4 - generate grammar source for more language in gradle

In my project I have to generate the grammar sources for more than one language (Java, Javascript and Python) using gradle.
I'm using the antlr plugin, so I have the following rows in my build.gradle file:
apply plugin: 'antlr'
generateGrammarSource {
def languageFlag = project.hasProperty('Language') ?project.property('Language') : 'Python2'
arguments = ['-Dlanguage=' + languageFlag]
def pythonOutputDirectory = "python/engine_lib/kpi_attributes"
switch (languageFlag) {
case "Java":
outputDirectory = file("../../../../XSpotterGUI/sviluppo/src/com/xech/xspotter4/grammars/kpiattributes")
arguments += ['-package', 'com.xech.xspotter4.grammars.kpiattributes']
break
case "JavaScript":
outputDirectory = file("../../../../XSpotterGUI/sviluppo/WebContent/xspotter4/js/xech/grammars/kpiattributes")
break
case "Python2":
outputDirectory = file(pythonOutputDirectory)
break
}
description = 'Generates Java sources from Antlr4 grammars.'
maxHeapSize = "64m"
sourceSets.main.antlr.srcDirs = ['.']
includes = ['KpiAttributes.g4']
doLast {
if (languageFlag.equals("Python2")) {
File file = new File("$pythonOutputDirectory/__init__.py")
file.write ""
}
}
}
I omitted the rows regarding repositories, dependencies and so on.
In this way I'm able to call gradle three times:
./gradlew generateGrammarSource -PLanguage=Java
./gradlew generateGrammarSource -PLanguage=Python2
./gradlew generateGrammarSource -PLanguage=JavaScript
But I have not been able to create a task 'generateAllGrammarSources' in order to call gradlew only ONE time and generate all sources

Gradle configure task based on subproject property

I'm trying to configure a Zip task based on one of the property inside sub-projects, but the property is not yet accessible at the time of configuring the task. For instance, I want to exclude all my projects that has toexclude = true from my zip file. So, the build.gradle of the sub-projects that I want to exclude starts with this:
ext.toexclude = true;
...
And my main build.gradle has this task:
task zipContent (type: Zip){
def excludedProjects = allprojects.findAll{Project p -> p.toexclude == true}.collect{it.name}
println excludedProjects
destinationDir = "/some/path"
baseName = "myFile.zip"
exclude excludedProjects
from "/some/other/path"
}
The problem is that excludedProjects is always empty. Indeed, when I am executing the task, I can see []. I believe this is due to the fact that the property that I set in the subproject's build.gradle is not available at the moment the task is configured. As a proof, if I replace the first line of the task by this:
def excludedProjects = allprojects.collect{it.name}
The task prints out all of my project's name, and the zip contains nothing (which means the problem is in the p.toexclude == true).
Also, if I try this:
task zipContent (type: Zip){
def excludedProjects = []
doFirst{
excludedProjects = allprojects.findAll{Project p -> p.toexclude == true}.collect{it.name}
println "IN DOFIRST"
println excludedProjects
}
println "IN TASK CONFIG"
println excludedProjects
destinationDir = "/some/path"
baseName = "myFile.zip"
exclude excludedProjects
from "/some/other/path"
}
The task prints out IN TASK CONFIG followed by an empty array, then IN DOFIRST with the array containing only the subprojects that I set ext.toexclude == true.
So, is there a way to get the properties of the sub-projects at configuration time?
Well, the crucial question is: At which point of the build is all necessary information available?
Since we want to know each project in the build, where the extra property toexclude is set to true and it is possible (and by design) that the property is set via the build script, we need each build script to be evaluated.
Now, we have two options:
By default, subprojects are evaluated after the parent (root) project. To ensure the evaluation of each project, we need to wait for the point of the build, where all projects are evaluated. Gradle provides a listener for that point:
gradle.addListener(new BuildAdapter() {
#Override
void projectsEvaluated(Gradle gradle) {
tasks.getByPath('zipContent').with {
exclude allprojects.findAll { it.toexclude }.collect{ it.name }
}
}
})
Gradle provides the method evaluationDependsOnChildren(), to turn the evaluation order around. It may be possible to use your original approach by calling this method before querying the excluded projects. Since this method only applies on child projects, you may try to call evaluationDependsOn(String) for each project in the build to also apply for 'sibling' projects. Since this solution breaks Gradle default behavior, it may have undesired side effects.
Just define excludedProjects outside the task
def excludedProjects = allprojects.findAll{Project p -> p.toexclude == true}.collect{it.name}
task zipContent (type: Zip){
destinationDir = file("/some/path")
baseName = "myFile.zip"
exclude excludedProjects
from "/some/other/path"
}
You can call evaluationDependsOnChildren() in the root project so that child projects are evaluated before the root
Eg
evaluationDependsOnChildren()
task zipContent (type: Zip) { ... }
Another option is to use an afterEvaluate { ... } closure to delay evaluation
Eg:
afterEvaluate {
task zipContent (type: Zip) { ... }
}

How to construct test dependency graph with gradle

I work on a large project (several hundred modules, each with tests) and would like to construct a test dependency graph using gradle dependencies.
For example, suppose I have the following modules and dependencies:
core <----- thing1 <----- thing1a
<----- thing2
If I run gradle thing1:dependencies it will tell me that thing1 dependsOn core. Instead, I would like to know what modules depend on thing1 so I can run the tests for thing1 and all dependant modules whenever I change thing1. In the example above, the dependent modules would be thing1 and thing1a
Hopefully there is a simple way to do this in gradle (constructing a test dependency graph seems like a pretty common thing to do), but I have not been able to find anything yet.
Using this gist (which I didn't write) as an inspiration, consider this in the root build.gradle:
subprojects { subproject ->
task dependencyReport {
doLast {
def target = subproject.name
println "-> ${target}"
rootProject.childProjects.each { item ->
def from = item.value
from.configurations
.compile
.dependencies
.matching { it in ProjectDependency }
.each { to ->
if (to.name == target) {
println "-> ${from.name}"
}
}
}
}
}
}
an example run using a project structure as you describe:
$ gradle thing1:dependencyReport
:thing1:dependencyReport
-> thing1
-> thing1a

How to call a task in another sub project build file with parameters

I'm working on creating a multi project build file using Gradle. Many sub projects need to execute a task which exists in another sub project by passing in certain parameters. How can this be achieved in Gradle?
for example :
root project
- project B : task X
- project A : task Y (param m, param n)
I need projectB.taskX to call projectA.taskY(m,n)
Update:
Sub-Project A has a task of type JavaExec which needs an input parameter to the location of the properties file
task generateCode(dependsOn:['classes','build'], type: JavaExec) {
main = 'jjrom.ObjectGen'
classpath = sourceSets.main.runtimeClasspath
args 'arg1', 'arg2', file(propertiesFilePath).path
}
Now, there are 10 sub projects, all of which need to call this task 'generateCode' with a parameter that contains the location to the properties file. Also, this task should be executed before building each sub-project which can be achieved using dependsOn.
My java project code organisation:
trunk/
projA/src/java/../ObjectGen.java
projB/src/java/../properties.xml
projC/src/java/../properties.xml
projD/src/java/../properties.xml
....
A task cannot call another task. Instead, the way to solve this problem is to add a generateCode task to all ten subprojects. You can do this from the root build script with code similar to the following:
subprojects {
apply plugin: 'java'
configurations {
codegen
}
dependencies {
// A contains the code for the code generator
codegen project(':A')
}
task generateCode(type: JavaExec) {
main = 'jjrom.ObjectGen'
classpath = configurations.codegen
args 'arg1', 'arg2'
}
compileJava.dependsOn(generateCode)
}
If there is no general pattern as to where the properties file is located, this information can be added in the subprojects' build scripts:
generateCode {
args file('relative/path/to/properties/file')
}

Resources