Refactor duplicated code in gradle task "type: Copy" - gradle

A gradle project contains several similar task type:Copy and some of them require additional checks.
task copyPackage1(type: Copy) {
from buildPackage1
into "/pkgs/"
eachFile {
if (it.relativePath.getFile(destinationDir).exists()) {
throw new GradleException("Probably version was no updated. File exists: " + it)
}
}
}
...
task copyPackage2(type: Copy) {
from buildPackage2
into "/pkgs/"
eachFile {
if (it.relativePath.getFile(destinationDir).exists()) {
throw new GradleException("Probably version was no updated. File exists: " + it)
}
}
}
How it is possible to refactor duplicated checks and specify same target directory for all similar tasks (but not all Copy tasks)?

You could either implement this with a custom Gradle plugin (as suggested in this similar question on Gradle forum), or use simple Groovy method to create and configure your tasks, as follows:
// define a "task creator" method
ext.createCopyToPkgTask = { String taskName , String fromDir ->
return project.tasks.create(taskName, Copy.class){
from fromDir
into "/pkgs"
eachFile {
if (it.relativePath.getFile(destinationDir).exists()) {
throw new GradleException("Probably version was no updated. File exists: " + it)
}
}
}
}
/* declare your similar tasks using the creator method above */
createCopyToPkgTask("copyPackage1","buildPackage1")
createCopyToPkgTask("copyPackage2","buildPackage2")
// ...

Related

xjc, generate java classes from xsd (using URL) in gradle 7.2

I use plugin com.github.bjornvester.xjc to generate java classes from xsd:
xjc {
xjcVersion.set("2.3.3")
outputJavaDir = file("${buildDir}/generated-sources/jaxb")
ext.downloaded = file("$buildDir/xjc/downloaded/schema2.wsdl")
doFirst {
mkdir downloaded.parentFile
downloaded.text = new URL("http://www.example.com/foo.xsd").text}
groups {
register("schema1") {
xsdFiles = files(xsdDir.file("${projectDir}/src/main/resources/wsdl/schema1.wsdl"))
defaultPackage.set("pl.com.project.schema1")
}
register("schema2") {
xsdFiles = files(downloaded)
defaultPackage.set("pl.com.project.schema2")
}
}
}
And I got an error in line "xjc {" :
In my previous attempt I incorrectly assumed that xjc was a task. After looking at the github page I can see that "xjc" is an extension object, not a task
So try this:
tasks.register('downloadXsd') {
ext.xsd = file("$buildDir/downloadXsd/foo.xsd")
outputs.file xsd // important!!! configures the task outputs
doLast {
mkdir xsd.parentFile
xsd.text = new URL("http://www.example.com/foo.xsd").text
}
}
xjc {
...
groups {
register("schema1") {
// assuming the plugin is written properly, this should configure a task dependency
xsdFiles = files(tasks.named('downloadXsd'))
...
}
...
}
}
You could improve this using the download task to download the xsd which shows progress of the download and also has caching options

refer to Gradle task which is defined later in build

Our build has some generic logic to create tasks from configuration data. There are also several Gradle files on whose execution order I do not want to depend.
Now I need to add a task dependency to a task without knowing if that task will be defined before my part of the build script runs (at configuration time) or after my part.
[Edit: as #canillas writes I can say myTask.dependsOn "otherTask" and "lazily" depend on yet undefined "otherTask", but I need it the other way around.]
Since I can't write tasks["otherTask"].dependsOn myNewTask before "otherTask" is defined, I made the following helper function:
void safelyDoWithTask(String taskName, Closure func) {
def task = tasks.findByName(taskName)
if (task != null) {
func(task)
} else {
project.tasks.whenTaskAdded { task_ ->
if (taskName.equals(task_.getName())) {
func(task_)
}
}
}
}
safelyDoWithTask('otherTask', { it.dependsOn myNewTask })
Now I wonder if there is more canonical way to achieve this?
Consider the following:
// section 1
def dynamicDependencies = [:]
dynamicDependencies['otherTask'] = 'myNewTask'
// section 2
afterEvaluate { project ->
// taskA dependsOn taskB
dynamicDependencies.each { taskA, taskB ->
def task = project.tasks.findByName(taskA)
if (task) { task.dependsOn "${taskB}" }
}
}
// section 3
task myNewTask() {
doLast {
println 'myNewTask !'
}
}
task otherTask() {
doLast {
println 'otherTask !'
}
}
The gist is:
section 1 defines our dependency info in a custom map
section 2 uses the afterEvaluate hook to process the map
because the above is decoupled from the task definitions, we can simply put them in section 3 (or wherever)

Gradle Copy Task Is UP-TO-DATE After Manually Deleting a File

I am forced to use Gradle 2.3 in this project.
I am trying to copy a set of dependencies from a custom configuration to a specific dir.
If I delete one of the files manually, Gradle still marks the task as UP-TO-DATE and I end up with an incomplete set of files.
task copyFiles(type: Copy) {
from configurations.zips
into 'zip-dir'
configurations.zips.allDependencies.each {
rename "-${it.version}", ''
}
}
This works as expected in v4.0.2 though.
To work around it I am counting files in that dir.
task copyFiles(type: Copy) {
outputs.upToDateWhen {
def count = new File('zip-dir').listFiles().count { it.name ==~ /.*zip/ }
count == configurations.zips.files.size()
}
from configurations.zips
into 'zip-dir'
configurations.zips.allDependencies.each {
rename "-${it.version}", ''
}
}
Which issue and version of gradle was this fixed in and is there a better workaround than what I have so far?
You can just run it always with
outputs.upToDateWhen { false }
or not use a type Copy for your task and
task copyFiles {
doLast {
copy {
from configurations.zips
into 'zip-dir'
configurations.zips.allDependencies.each {
rename "-${it.version}", ''
}
}
}
}
Note that this is a workaround not the solution

How to use a parameter in gradle copy task in the destination folder?

Given the following task in gradle, the dev would start a new module by:
gradle initModule -PmoduleName=derp
task initModule(type: Copy) {
description "Initialize an empty module based on the template. Usage: gradle initModule -P moduleName=derp"
onlyIf { project.hasProperty("moduleName") }
from "$rootDir/modules/template"
into "$rootDir/modules/$moduleName"
}
I am unable to run gradle since moduleName is not defined during evaluation, although I was hoping that "onlyIf" would do so.
To solve this I assign it to a locally defined variable in a guard block:
def modName = ""
if (!project.hasProperty("moduleName")) {
logger.error("Invalid moduleName : It can't be")
} else {
modName = moduleName
}
and finally use the new variable to survive the configuration phase.
Is this the right way to do this? It just doesn't feel right.
Additionally if I was using a rule to make this a tad more elegant:
tasks.addRule("Pattern: initModule_<mod name>") { String taskName ->
if (taskName.startsWith("initModule_")) {
def params = taskName.split('_');
task(taskName) {
doLast {
project.ext.moduleName = params.tail().head()
tasks.initModule.execute()
}
}
}
}
The moduleName is not passed around (even if I change it to finalizedBy).
Any help is appreciated.
As you already figured out the property is not available during the configuration phase.
But can postpone the look-up to the execution phase by using
into "$rootDir/modules/" + project.property("moduleName")
instead of
into "$rootDir/modules/$moduleName"

Gradle - publish artifacts

I want to publish artifacts to ivy repository but it doesn't work. I read this article and after read created this sample build:
task ivyPublishTest << {
def buildDir = new File("build")
buildDir.mkdirs()
def fileToPublish = new File("build/file.abc")
fileToPublish.write("asdasdasd")
}
artifacts {
archives(ivyPublishTest.fileToPublish) {
name 'gradle-test-artifact'
builtBy ivyPublishTest
}
}
uploadArchives {
repositories {
ivy {
url "http://my.ivy.repo/ivyrep/shared"
}
}
}
Of course the problem is that it doesn't work. I get this error Could not find property 'fileToPublish' on task ':ivyPublishTest'
In Groovy, def creates a local variable, which is lexically scoped. Therefore, fileToPublish is not visible outside the task action. Additionally, configuration has to be done in the configuration phase (i.e. the declaration and assignment of fileToPublish in your task action would come too late). Here is a correct solution:
task ivyPublishTest {
// configuration (always evaluated)
def buildDir = new File("build")
ext.fileToPublish = new File("build/file.abc")
doLast {
// execution (only evaluated if and when the task executes)
buildDir.mkdirs()
fileToPublish.write("asdasdasd")
}
}
artifacts {
// configuration (always evaluated)
archives(ivyPublishTest.fileToPublish) {
name 'gradle-test-artifact'
builtBy ivyPublishTest
}
}
ext.fileToPublish = ... declares an extra property, a new property attached to an existing object that's visible everywhere the object (task in this case) is visible. You can read more about extra properties here in the Gradle User Guide.

Resources