Task of Type Copy not executed - gradle

I am facing an issue executing my task of type Copy,:
Skipping the task as it has no source files
I get if I run in the debug mode.
My Plugin.groovy class (where the call to the plugin task in made )
Task task = project.tasks.create("makeJarPlugin", MakeJarPluginTask.class)
task.dependsOn("clearDistPlugin", "build")
My MakeJarPluginTask.grrovy
class MakeJarPluginTask extends Copy {
#TaskAction
def makeJar(){
logger.lifecycle("creating a jar *********************")
delete('dist/')
from('build/intermediates/bundles/release')
into('dist/')
include('classes.jar')
def jarName = new VersionName().getNameWithVersion() + '.jar'
rename('classes.jar', jarName)
}
}
Now, I execute this task in my android studio project using
gradlew makeJarPlugin --info
It gives me the output:
Skipping task ':network:makeJar1' as it has no source files.
makeJar1 UP-TO-DATE
There is something wrong with the type Copy as in the same way I execute my delete task and it executes. Any pointers!

It seems that this answer may be helpful.
Hint: you need to configure the task. Otherwise it won't be executed since the whole configuration is done in makeJar which is too late since this is an action.
Instead of using copy you can also try:
class MakeJarPluginTask extends DefaultTask {
#TaskAction
def makeJar() {
logger.lifecycle("creating a jar *********************")
delete('dist/')
project.copy {
from('build/intermediates/bundles/release')
into('dist/')
include('classes.jar')
def jarName = new VersionName().getNameWithVersion() + '.jar'
rename('classes.jar', jarName)
}
}
}

Related

Set the properties in the configuration phase of a task in another task

I have spent the last few hours trying to find a solution for my requirement, without luck:
I have a task that has to run some logic in a certain path:
task run(type: MyPlugin) {
pathForPlugin = myPath //Defined as a property in another gradle file
}
I want to set the "pathForPlugin" property dynamically in another task because it has to be read from some configuration file.
task initPaths(type: PathFinder) {
configurationFile = 'C:\\myConfig.conf'
}
The myConfig.conf would look like this:
pathForPlugin = 'C:\\Correct\\Path'
The problem is that "initPaths" has to run before the configuration phase of "run".
I have tried several approaches for this (GradleBuild task, dependsOn, Using Properties in the Plugin for "Lazy Configuration") but every approach only takes effect in the Execution phase leading to the "pathForPlugin" always staying at its default value.
Is there some way i can realize this or should i look for another solution outside of the gradle build?
I found a solution for the problem:
Instead of defining a task "initPaths" i directly used the java class "Pathfinder" in the build script:
import mypackage.PathFinder;
new PathFinder(project).run()
You only have to make sure that this part is above the definition of the task where the properties are used.
I admit this is a bit of a "hacky" solution but it works fine for my requirement.
you can do like this:
ext {
myPath //use it as a global variable that you can set and get from different gradle tasks and files
}
task firstTask {
doLast {
ext.myPath = "your path"
}
}
task run(type: MyPlugin) {
doFirst { //executed on runtime not on task definition
pathForPlugin = ext.myPath //Defined as a property in another gradle file
}
}
//example 2 - create run task dynamic
task initPath {
doLast {
tasks.create(name: "run", type: MyPlugin) {
pathForPlugin = ext.myPath
}
}
}

TaskInternal.execute Migration

In a spring boot application in the build.gradle
task loadDbConfigFromSpringProfile {
def activeProfileProperties = new Properties()
file("src/main/resources/application.properties").withInputStream {
activeProfileProperties.load(it)
}
def profileProperties = new Properties()
file("src/main/resources/application-" + activeProfileProperties.getProperty("spring.profiles.active") + ".properties").withInputStream {
profileProperties.load(it)
}
active_db_url = profileProperties.getProperty("spring.datasource.url")
}
loadDbConfigFromSpringProfile.execute()
When i run application with gradle 4.4, I get
The TaskInternal.execute() method has been deprecated and is scheduled
to be removed in Gradle 5.0.
I started to create a class who extends extends DefaultTask but I'm not sure if it's the easier way to fix issue.
If you remove the call to loadDbConfigFromSpringProfile.execute(), your code will have the same effect.
You do not even need the task definition, putting the following in your build file has the same effect:
def activeProfileProperties = new Properties()
file("src/main/resources/application.properties").withInputStream {
activeProfileProperties.load(it)
}
def profileProperties = new Properties()
file("src/main/resources/application-" + activeProfileProperties.getProperty("spring.profiles.active") + ".properties").withInputStream {
profileProperties.load(it)
}
active_db_url = profileProperties.getProperty("spring.datasource.url")
Here is why: The task you are defining has no actions. Actions can be added to a task by using doLast or doFirst as shown in the hello world example.
That also means that everything which is in the body of your task is executed at configuration time - since it is supposed to be configuring the task.
Therefore, loadDbConfigFromSpringProfile.execute() has no effect - the task is skipped since it has no actions.
you can replace by finalizedBy()
https://discuss.gradle.org/t/how-to-call-task-execution-from-outside-task/25971

Gradle Task . "(type: Copy)" and <doLast> can't both work

task simpleTask{
print("simpleTask is reach");
}
task copySomeFile(type: Copy){
print("copySomeFile is reach");
from baseProjectPath;
into toProjectPath;
appendXML();
}
def appendXML(){
//modify a.txt
}
//i just want to run "simpleTask" only, but when "gradle simpleTask", the task"copySomeFile" will be run also ! I know beacuse gradle initialization.
but if write like this
task copySomeFile(type: Copy)<<{
}
the "copySomeFile" will not work.
it seems like "(type: Copy)" can't work with the "<<" or "doLast{}"?
i just want "--gradle simpleTask" "--gradle copySomeFile" can run alone.
You have to read about Gradle build lifecycle.
There are 2 phases you should note - Configuration and Execution. All tasks are always been configured on every build, but only some of them are really executed as the Execution phase.
What you see is that copySomeFile task was configured during the configuration phase. It doesn't copy anything, but it has to be configured. And everything within a tasks closure is task's configuration, that is why you see results of the print("copySomeFile is reach"); in the output.
<< or doLast are used to run something at the Execution phase, but your task of type Copy will not be configured if you place all it's configuration into doLast section or add << to the task definition - that is the reason why copy doesn't work.
Yeh, i got it. How much I appreciate both of you. SHARE THE CODE:
task simpleTask {
print("\nsimpleTask is configured"); // executed during the configuration plase, always
doLast {
print("\nsimpleTask is executed"); // executed during the execution plase, only if the simpleTask is executed
}
}
task copySomeFile(type: Copy) {
print("\ncopySomeFile is configured"); // executed always,执行其他任务时,此代码也会执行
from "D:/a.txt";// not executed. 执行其他任务时,此代码不会执行
into "D:/b.txt";// not executed. 执行其他任务时,此代码不会执行
doLast {
appendXML(); //only this task executed, the appendXML executed. 只有此task执行时,才会执行.比如(gradle copySomeFile);
}
}
def appendXML(){
print("\nappendXML");
}

Gradle: Delete files from directory, based on Filelist

I'm currently trying to migrate my old ant system to gradle. I'm pretty new to gradle and still learning, so maybe this is something trivial I just overlooked.
I got a file, called delete.list which contains a list of files I want to delete.
This is my code so far:
task deleteLib(type:DeleteFiles) {
deleteList = file("${buildDir}/delete.list")
}
class DeleteFiles extends DefaultTask {
#SkipWhenEmpty
#InputFile
File deleteList
DeleteFiles()
{
description = 'Deletes Libs from Integris zip'
}
#TaskAction
void delete(){
def lines = deleteList.readLines()
lines.each {
delete fileTree(dir: "${project.buildDir}", include: "${it}")
}
}
}
delete.list:
lib/java/activation.jar
lib/java/pdfbox*.jar
lib/java/fontbox*.jar
lib/java/xmpbox*.jar
lib/java/jempbox*.jar
lib/java/iText*.jar
lib/java/itext*.jar
lib/java/jakarta-poi.jar
lib/java/commons-net*.jar
lib/java/jfreechart*.jar
lib/java/jcommon*.jar
lib/java/dom4j*.jar
lib/java/xmlbeans*.jar
lib/java/jaxen*.jar
lib/java/avalon-framework*.jar
lib/java/batik-all*.jar
After googling a bit I found this solution, as my _delete.list may not exist during building phase.
My current problem is that gradle seems to have a problem with the fileTree method:
* What went wrong:
Execution failed for task ':deleteLib'.
> Could not find method fileTree() for arguments [{dir=C:\entwicklung\Testumgebung\testProjectGradle\build, include=lib/java/activation.jar}] on task ':deleteLib' of type DeleteFiles.
Have somebody an idea what I'm missing here?
Since both methods are defined on Project and groovy looks for the method defined in task you need to explicitly invoke the methods on project instance:
project.with {
delete fileTree(dir: "${project.buildDir}", include: "${it}")
}

How to create a dependency between a task and an extension?

I have an Android gradle script as follows:
preBuild.dependsOn "editManfest"
task editManfest(type: Copy) {
// copies and edits the AndroidManifest.xml
}
import com.android.builder.core.DefaultManifestParser
def extension = android {
def manifestParser = new DefaultManifestParser()
def manifestVersion = manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile)
...
However the code in the extension is attempting to access the manifest file before the editManifest task has completed.
What can I do to ensure the task is completed before the extension's code attempts to access the manifest?
Your running into execution vs configuration time issue. The editManfest task will always execute after configuration configuring the android extension .
Can you add a
doLast {
def manifestParser = new DefaultManifestParser()
def manifestVersion = manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile)
}
to your editManfest task?

Resources