Is it possible to throw an error if no arguments are provided to Gradle? - gradle

My gradle setup accepts arguments during runtime which are checked in a shell script that Gradle Exec task calls. However, in order to reach that point, Gradle deals with dependencies and spends a good amount of time before the end script is executed, which would then throw an error if no arguments are passed.
Build task in gradle looks like below:
task buildAll(type: Exec) {
environment['PROJECT_ROOT'] = "${projectDir}"
workingDir rootProject.projectDir.path
executable rootProject.projectDir.path + "/script.ksh"
if (project.hasProperty('ARGS')) {
args(ARGS.split(','))
}
}
Gradle is called as follows:
./gradlew build -PARGS="-arg1,value1,-arg2,value2,-arg3,-arg4,value4"
I intend to check for -arg2 and if it is not provided, I would like the gradle build to fail with some sort of usage displayed. Is this possible?

You can add an if block at the very beginning of the script:
if (!project.hasProperty('lol')) {
throw new GradleException("'lol' property is required!")
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.guava:guava:18.0'
}
}
however it will not prevent gradle from resolving the dependencies of the script itself - buildscript block will be evaluated first.

Related

findByName() method returns null for gradle plugin task

I am using maven-publish plugin. I have a publication called myMedia. When I try executing
tasks.findByName("publishMyMediaPublicationToSnapshotRepository"), it returns null. Why does it return null if there is a task by that name that maven-publish plugin created.
I'm not able to get around this issue. Any help is greatly appreciated.
EDIT 1 : START
I tried Francisco Mateo's suggestion to use the afterEvaluate method, but this too didn't seem to work for me.
However, while trying to figure out the actual cause I came across one interesting observation. I found that the when I call tasks.findByName("publishMyMediaPublicationToSnapshotRepository") from within a task, then it works perfectly fine and prints out the task, i.e. printTaskName runs as expected :
task printTaskName{
doLast{
println "task name found is :--- "+tasks.findByName("publishMyMediaPublicationToSnapshotRepository")
}
}
Now originally in my project's build.gradle I have a publishSnapshot task followed by ordering logic using mustRunAfter method:
task publishSnapshot (dependsOn: ['createMyMediaArchive','publishMyMediaPublicationToSnapshotRepository'])
tasks.findByName("publishMyMediaPublicationToSnapshotRepository").mustRunAfter tasks.findByName("createMyMediaArchive")
publishSnapshot.mustRunAfter tasks.findByName("publishMyMediaPublicationToSnapshotRepository")
executing the publishSnapshot task gives below error:
Cannot invoke method mustRunAfter() on null object
tasks.findByName method doesn't seem to work here as it returns null.
Am I missing out on some basics?
EDIT 1 : END
EDIT 2 : START
The below build.gradle is an edited version of what I actually have. I am not allowed to share the script. But the script is identical in structure to what I have. The rest of build script is as it is as far as the structure and syntax is concerned, just the names changed. Gradle version is 3.5.1
build.gradle
import java.time.LocalDate;
import java.time.format.DateTimeFormatter
import java.text.SimpleDateFormat
import Tasks.PublishArtifacts
import org.gradle.api.GradleException
buildscript {
repositories {
abcRelease()
}
dependencies {
classpath "org.codehaus.groovy.modules.http-builder:http-builder:0.7"
classpath "commons-collections:commons-collections:3.2.1"
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.9.10"
}
}
repositories {
mavenCentral()
abcSnapshot()
xyzRelease()
}
apply plugin: 'maven-publish'
apply plugin: 'base'
apply plugin:'java'
apply from: "$rootDir/repositories.gradle"
def getMediaVersion(){
project.mediaVersion
}
configurations{
releaseMedia
}
publishing{
publications {
myMedia(MavenPublication) {
artifactId project.artifactId
artifact "$project.artifactId"+"-"+getMediaVersion()+".tgz"
version getMediaVersion()
}
}
repositories project.artifactoryRepositories
}
task publishSnapshot (dependsOn: ['createMyMediaArchive','publishMyMediaPublicationToSnapshotRepository'])
tasks.findByName("publishMyMediaPublicationToSnapshotRepository").mustRunAfter tasks.findByName("createMyMediaArchive")
publishSnapshot.mustRunAfter tasks.findByName("publishMyMediaPublicationToSnapshotRepository")
afterEvaluate { evaluated ->
evaluated.tasks.findByName("publishMyMediaPublicationToSnapshotRepository").configure {
println "My Media name --------- "+it.name
}
}
task printTaskName{
doLast{
println "task name found is :--- "+tasks.findByName("publishMyMediaPublicationToSnapshotRepository") // This returns task name as exected
}
}
task createMyMediaArchive{
doLast{
//code for creating media
}
}
EDIT 2 : END
EDIT 3: START
If I run the tasks individually, i.e. first running task createMyMediaArchive and then running task publishSnapshot ,where the taskpublishSnapshot depends on
task publishHelmMediaPublicationToSnapshotRepository, ie.
task publishSnapshot (dependsOn: ['publishHelmMediaPublicationToSnapshotRepository'])
then both the tasks run successfully. This proves that gradle does have the task publishHelmMediaPublicationToSnapshotRepository pre-configured.
Also gradlew tasks --all prints out this task.
This behavior is making things look more confusing to me.
EDIT 3 : END
A lot of the configuration performed by the maven-publish plugin is done lazily.
Wrapping in afterEvaluate should work.
afterEvaluate { evaluated ->
evaluated.tasks.findByName("publishMyMediaPublicationToSnapshotRepository").configure {
println it.name
}
}

Run gradle task multiple times

I have a gradle project containing two modules in subdirectories. The directory structure is as below.
root
module1
build.gradle
module2
build.gradle
build.gradle
settings.gradle
The top level settings.gradle includes the two modules. The top level build.gradle contains the following.
task runScript(type: Exec) {
workingDir 'scripts'
commandLine 'python3', 'myscript.py'
}
project(':module1') {
evaluationDependsOn(':module1')
final test = tasks.findByName('test')
test.dependsOn(runScript)
}
project(':module2') {
evaluationDependsOn(':module2')
final test = tasks.findByName('test')
test.dependsOn(runScript)
}
Task runScript sets the database to a known state and must be run before each module test task.
When I run the test task my script only executes once. How can I ensure it executes multiple times?
$ ./gradlew test
... some output
:runScript
RUNNING MY SCRIPT
:module1:test
RUNNING MODULE1 TESTS
... some output
:module2:test
RUNNING MODULE2 TESTS
Things I Tried
I tried adding outputs.upToDateWhen {false} to the task runScript so Gradle never thinks it is up to date. This didn't make any difference; I assume because the task still only occurs once in the task graph?
I tried replacing the lines containing dependsOn for each module with test.doFirst {runScript.execute()}. This changes when the task gets executed but does not result in multiple executions.
$ ./gradlew test
... some output
:module1:test
RUNNING MY SCRIPT
RUNNING MODULE1 TESTS
... some output
:module2:test
RUNNING MODULE2 TESTS
I tried creating a new task for each module. This works but it's duplicating code.
project(':module1') {
evaluationDependsOn(':module1')
final test = tasks.findByName('test')
task runScript(type: Exec) {
workingDir '../scripts'
commandLine 'python3', 'myscript.py'
}
test.dependsOn(runScript)
}
project(':module2') {
evaluationDependsOn(':module2')
final test = tasks.findByName('test')
task runScript(type: Exec) {
workingDir '../scripts'
commandLine 'python3', 'myscript.py'
}
test.dependsOn(runScript)
}
If your script is necessary for each run of each Test task, simply assure its execution before each Test task. Why even use a task then?
subprojects {
tasks.withType(Test) {
doFirst {
exec {
workingDir 'scripts'
commandLine 'python3', 'myscript.py'
}
}
}
}

Gradle dependency does not work as my expectation

I have two sub projects subproject1 and subproject2. I'd like to add some classes from subproject2 to subproject1 and get the subproject1.jar. Below is my gradle file:
task copyClasses (dependsOn: [ ':subproject1:clean', ':subproject1:classes']) {
println "copyClasses "
doLast {
Task study = tasks.getByPath(':subproject1:jar')
study.doFirst {
copy {
println "copy ... "
println sourceSets.main.output.classesDir
println project(':subproject1').sourceSets.main.output.classesDir
from sourceSets.main.output.classesDir
into project(':subproject1').sourceSets.main.output.classesDir
}
}
}
}
task jarUpdated (dependsOn: [ clean, classes, copyClasses, ':subproject1:jar']) {
doLast {
println "jarUpdated"
}
}
But I got the build sequence as below:
$ gradle jarUpdated
copyClasses
:subproject1:compileJava
:subproject1:processResources UP-TO-DATE
:subproject1:classes
:subproject1:jar
:subproject2:compileJava
:subproject2:processResources UP-TO-DATE
:subproject2:classes
:subproject2:clean
:subproject1:clean
:subproject2:copyClasses
Calling Task.doFirst(Closure) after task execution has started has been deprecated and is scheduled to be removed in Gradle 2.0. Check the configuration of task ':subproject1:jar'.
:subproject2:jarUpdated
jarUpdated
BUILD SUCCESSFUL
My expectation is:
$ gradle jarUpdated
:subproject2:clean
:subproject2:compileJava
:subproject2:processResources UP-TO-DATE
:subproject2:classes
:subproject1:clean
:subproject1:compileJava
:subproject1:processResources UP-TO-DATE
:subproject2:copyClasses
copyClasses
copy ...
:subproject1:jar
:subproject2:jarUpdated
jarUpdated
BUILD SUCCESSFUL
Would you please suggest or point out what I missed? Thanks a lot!
The "easiest" way to do exactly what you asked for is probably something like this in your subproject1 build file.
jar {
from tasks.getByPath(':subproject2:compileJava')
}
However this is a very simplistic approach with a LOT of caveats, for example
Subproject 1 can not compile against subproject 2 classes
Any dependencies of Subproject 2 will not be included
etc
I would actually advise declaring subproject2 as a dependency of subproject1 and using one of the plugins that Peter suggested.

Why my dependency is printed twice?

I have the following fairly simple build.gradle build script:
repositories {
mavenCentral()
}
configurations { libs }
dependencies {
libs 'org.hibernate:hibernate-core:4.3.5.Final'
}
configurations.libs.files { println it }
When I run it with gradlew build (I'm using Gradle 1.12, latest at the moment) I get the following:
DefaultExternalModuleDependency{group='org.hibernate', name='hibernate-core', version='4.3.5.Final', configuration='default'}
DefaultExternalModuleDependency{group='org.hibernate', name='hibernate-core', version='4.3.5.Final', configuration='default'}
These seem to be the same dependencies, but I don't get it why there are 2 of them when I have added just a single one.
Does anyone know why? Did I do something wrong? Or is there something I don't understand?
The Configuration#files method expects a predicate. (For API details, see Configuration in the Gradle Build Language Reference.) If you instead pass it a closure containing a println statement, (seemingly) strange things will happen. You probably want something like the following instead:
// configurations should only be resolved in
// the execution phase, so let's declare a task
task printLibs {
doLast {
configurations.libs.each { println it }
}
}

Gradle war ignores transitive dependencies when using 'configurations.runtime.asPath' in custom task

I'm facing behavior that I can't explain, using gradle 1.10 I have:
settings.gradle:
include('lib1', 'lib2', 'web')
build.gradle:
subprojects {
apply plugin: 'java'
}
project(':web') {
apply plugin: 'war'
dependencies {
compile project(':lib1')
}
task myTask(type: JavaExec, dependsOn: 'compileJava') {
main = "some.thirdparty.Class"
args "--searchPath", configurations.runtime.asPath
}
}
project(':lib1') {
dependencies {
compile project(':lib2')
}
}
project(':lib2') {
}
When I run gradle clean war I only have lib1.jar in war/build/libs/web.war/WEB-INF/lib.
To make WEB-INF/lib contain both lib1.jar and lib2.jar I have to:
move project('web') block to the end of the file
update configurations.runtime.asPath to configurations.runtime (but I need to provide class path as a path, so it is not a solution)
I read the build lifecycle description, tried to compare --debug outputs but that didn't help.
Why is this happening? And what would be a good solution to provide the module runtime class path as a path in JavaExec task please?
asPath resolves the configuration, but resolution will only work correctly if it happens at execution time rather than configuration time (in particular in the presence of project dependencies). Try to wrap the args line with doFirst { ... }.

Resources