How to run custom task after gradle assemble command - spring-boot

Actually I have these lines as part of my build.gradle file (using gradle 7.3.3)
processResources {
dependsOn "buildAngular"
}
task buildAngular(type: Exec) {
...
my task "buildAngular" runs automaticly on ./gradlew assemble its perfect and exactly what I want. But it runs on ./gradlew test too.
How I can make it run only on ./gradlew assemble?

As already mentioned in my comment on the question:
Since tests require a complete runtime classpath and since processResources produces parts of that classpath, it wouldn’t make sense to not run it when test is run.
If you only want to run buildAngular as part of assemble, then simply make the former a dependency of the latter:
assemble {
dependsOn 'buildAngular'
}

Related

Execute builds only for some gradle modules in project

I have a project utilities with a build.gradle. utilities has some modules named util, util2, util3, ...
In a task I want to execute first :util2:build and :util5:build. But I do not know how to write such a task. This fails:
task executePreBuild() {
:util2:build
:util5:build
}
In commandline
gradlew clean :util2:build :util5:build
can be executed. But this is not my purporse.
I want to execute
gradlew clean executePreBuild someOtherTask build
This can be done by using dependsOn:
task executePreBuild {
dependsOn ":util2:build"
dependsOn ":util5:build"
}

How to get the exact copy of original Run task in Gradle?

Does anybody know what the simplest way to register several run tasks that represent exact copy of original one with changed app’s arguments ? Or may be how to supply run task with an optional argument that would represent app’s arguments.
Basically, I want my build to contain some pre-defined options how to run an application and don’t want to declare new JavaExec that requires its manual configuring while I have already had ready-to-go run task supplied by default.
gradle run --args='--mode=middle' ---> gradle run || gradle runDefault || gradle run default
gradle run --args='--mode=greed' ---> gradle runGreed || gradle run greed
gradle run --args='--mode=lavish' ---> gradle runLavish || gradle run lavish
As for now I came up only with the option that suggests implementing my own JavaExec_Custom task class with supplied arguments property. And it seems to be too complex and boilerplating as for my goal.
You can create tasks that modify the configuration of the actual run task in a doFirst or doLast closure:
// Groovy-DSL
task runGreed {
doFirst {
run.args '--mode=greed'
}
finalizedBy run
}
// Kotlin-DSL
register("runGreed") {
doFirst {
run.get().args = listOf("--mode=greed")
}
finalizedBy(run)
}
You can use a property for the args
task run(type: JavaExec) {
args property('run.args').split(' ')
...
}
Usage
gradle run -Prun.args='foo bar baz'

skip a task in gradle, but only if the task exists

I have a complex multi sub-project gradle project with a little kotlin multiplatform inside and a fex custom gradle plugin.
My issue is , when I want to run the build by skipping some tasks(mostly test), not all the project have the same test task name : for some it's call jsTest, for other nodeJsTest, for other jvmTest.
So when I call gradle build -x jsTest -x nodeJsTest I have error because sometime some of the tasks to skip don't exist.
How can I skip the task, and ignore-it if it don't exist?
You can modify your Gradle file to do something like (Kotlin DSL):
tasks.named("build") {
dependsOn.removeIf { it.toString().contains("flakyTest") }
}
Otherwise you will need to aggregate your tasks to what you want specifically either by doing ./gradlew myTask anotherTask anotherOne andAnotherOne or create a task that dependsOn all the tasks you want.
Instead of making your test tasks to depend on build directly, you can create generic test task task testType in the middle- build triggers task testType and then it triggers jsTest or whatever relevant test task you create in that module.
Now you can safely run gradle build -x testType.
For example (in your .gradle file):
task jsTest { ... }
task testType { dependsOn jsTest }
build.finalizedBy(testType)
Do the same for the rest of your test tasks files, you can also create task testType globaly if you want the solution to be cleaner.

Chained Gradle task cannot read file generated by previous task

I have gradle task, taskA, which when run will generate a html file. Then taskB will try opening that file. When I chain these like:
./gradlew taskA taskB
Then taskB cannot see the generated file. Incidentally IntelliJ is open and does not see the file at the same time.
However if I run the commands separately, e.g.
./gradlew taskA
./gradlew taskB
Then taskB can see the file fine. Do you know how I might chain the commands with the effect of running them separately? I have tried using clean at the start of taskB but it does not help.
The way you chain task in Gradle is by making taskA depend on taskB.
You can do it as follows:
apply plugin: 'base'
def file = project.file('shared-file.txt')
task taskA {
outputs.file(file)
doLast {
// Create the file
file.text = "Hello world!"
}
}
task taskB(dependsOn: taskA) {
inputs.file(file)
doLast {
// Print file content
println file.text
}
}
To clean up the file you can run cleanTaskA which will cleanup all outputs that taskA has defined. Or if you want to add the cleanup to the generic clean task then add clean.dependsOn(cleanTaskA, cleanTaskB).
The way I got it to work was by cd'ing back into the current directory.
This can be achieved with the following command, when you are in the correct directory:
cd .
This forces gradle to pick up any new files immediately.

gradle copy file task not working in build

I am new to gradle, I want copy the jar file generated by gradlew build to another dir.
task myCopyTask(type: Copy) {
from "build/libs/gs.jar"
into "D:/bin/gs"
}
I add above task to the build.gradle which belong to gs module which will generate gs.jar.
The problem is the command gradlew build will not do the copy and this task indeed executed(I add println in myCopyTask). However, the command gradlew myCopyTask works.
First I thought maybe the copy task running too early, so I change it to
task myCopyTask(type: Copy) {
doLast {
from "build/libs/gs.jar"
into "D:/bin/gs"
}
}
This is not working even by gradlew myCopyTask. Only first version can work by command gradlew myCopyTask, the terminal will show: 1 actionable task: 1 executed
What is the problem?
You haven't wired the task into Gradle's DAG so currently it will only executed when you do gradlew myCopyTask
You'll probably do something like
apply plugin: 'base' // adds build and assemble lifecycle tasks
task myJarTask(type:Jar) {...}
task myCopyTask(type: Copy) {
dependsOn myJarTask
...
}
assemble.dependsOn myCopyTask
See https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:task_dependencies

Resources