gradle copy file task not working in build - gradle

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

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"
}

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: execute tasks sequentially in some task

I have a library, which contains 3 lib modules and 1 example module. Before deploy task I want to execute some other tasks. In command line it looks like this: ./gradlew -x:example:clean -x:example:check -x:example:uploadArchives clean check :androidLib:assembleRelease uploadArchives.
I want to write gradle task to execute all tasks sequentially for all modules besides example module. That I can do: ./gradlew deployAll. How can i do it?
I try do this:
task deployAll {
doLast {
subprojects {
if(it.plugins.withType(com.android.build.gradle.AppPlugin)) return
it.tasks.getByName('clean').execute()
it.tasks.getByName('check').execute()
...
}
}
}
But execute() is deprecated and it execute only first task and ignore any.
You can use dependsOn inside of your gradle tasks to make sure your tasks run in the correct order
task task1{
dependsOn task2
//Task one code
}
task2{
dependsOn task3
//task 3 code
}
task3{
//task3 code
}
so in this example if you call task1, first task 3 will be executed, then task2 and then finally task one, but you need only call task1.
You can make another task and set other tasks its dependencies
task deployAll {
dependsOn tasks.getByName('clean')
dependsOn(tasks.getByName('check'))
}
To ensure the order add put this somewhere
tasks.getByName('check').mustRunAfter(tasks.getByName('clean'))

Gradle tasks dependency

Q: Can one task be dependent on another task with specific argument? If the answer is yes then how to accomplish that?
I opened Gradle for myself recently, and just got curious about next:
Let's say I'd like to have one of the build's subfolders to be copied each time I execute the copy task.
task copy (type: Copy) {
dependsOn build
from '[fromName]'
into '[intoName]'
}
In my case, I'd like to copy folder build/reports/profile, but the thing is that this folder is created by execution of the build --profile task.
So, is there any chance to accomplish the following:
task copy (...) {
dependsOn build --profile
from ...
into ...
}
Btw, plugin project-report doesn't create this folder, to be dependent on its task.
Note: It's not as important to have it done, I'm just interested if it is possible at all.
You can't pass arguments with dependsOn to a task directly but you can create a custom Exec task and pass arguments via the command line interpreter (cmd) using the gradle wrapper like this:
task buildWithArgument(type: Exec) {
commandLine 'cmd', '/c', 'gradlew.bat build --profile'
}
task copy (type: Copy) {
dependsOn buildWithArgument
from '[fromName]'
into '[intoName]'
}

Gradle : UP-TO-DATE task

I have task 'compileProfileObj' which mainly has to create a jar file.
Even in fresh environment setup, gradle logs outputs 'UP-TO-DATE' for this.
task compileProfileObj(type: JavaCompile, dependsOn: [generateArtifacts]) {
classpath = configurations.deployment
source fileTree(
dir: "${objDir}/app",
includes: ['com/main/**'])
destinationDir file("${proDir}/profileobjects-biz.jar")
doLast {
copy {
from "${objDir}"
include '*.xml'
exclude 'server.xml'
into "${proDir}/profileobjects-biz.jar/META-INF"
}
copy {
from "${objDir}/profileobjects"
into "${proDir}/profileobjects-biz.jar/META-INF"
}
}
}
I understand the gradle doesnot executed tasks in some cases if the tasks has successfully executed in previous build runs.
But in my case even in 1st run gradle doesn't execute this task and prints 'UP-TO-DATE'
Googling didnt help me much here.
Any suggestions in what scenarios this might happen will be really helpful.

Resources