Gradle: change property of task from a plugin - gradle

This is about as basic as it could be... but I haven't found the answer either here or at gradle.org or generally. NB Gradle 4.4.1, Groovy 2.4 (used by Eclipse-Gradle) or 2.6 (command line). Java 8.
In my build.gradle I have the application plugin and have set the main class:
apply plugin: 'application'
mainClassName = "core.ConsoleHandler"
... including this plugin adds 2 "dependency tasks" (if that's the right term: i.e. build is now "dependent" on them): taskZip and taskTar.
Here we see that taskZip is of type Zip... and looking up the doc for this subclass of Task here we see that one of the properties of such a Task is destinationDir... i.e. where the .zip file ends up.
All I want to do is set this to a specific directory.
I've tried (in build.gradle) things like
task application.taskZip {
destinationDir = 'D:/bobble'
}
and
taskZip {
destinationDir = 'D:/bobble'
}
and
destinationDir = 'D:/bobble'
These all produce fatal errors. What should I be doing?

It's distZip not taskZip
apply plugin: 'application'
distZip {
destinationDir = file('D:/bobble')
}

destinationDir takes a File parameter
Try something like that :
tasks.withType(taskZip) {
destinationDir = file("D:/bobble")
}

Related

Gradle DSL method not found: 'destination()' after update to Gradle 5.2.1

After updating to Gradle 5.2.1 my build is failing with this error:
Gradle DSL method not found: 'destination()'
I figured out that this error has something todo with my analysis.gradle
My analysis.gradle looks like that
apply plugin: 'checkstyle'
apply plugin: 'pmd'
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.7.7.201606060606"
}
check.dependsOn 'checkstyle', 'pmd', 'lint'
task checkstyle(type: Checkstyle) {
println "----- checkstyle -----"
configFile file(projectDir.getAbsolutePath() + '/analysis/checkstyle-ruleset.xml')
source 'src'
source '../domain/src'
source '../util/src'
include '**/*.java'
exclude '**/gen/**'
exclude '**/java-gen/**'
exclude '**/androidTest/**'
exclude '**/test/**'
ignoreFailures = true
classpath = files()
reports {
xml {
destination buildDir.absolutePath + "/outputs/reports/checkstyle_report.xml"
}
}
}
I think I have to replace the destination flag but I have no idea how to replace it.
Before Gradle 5.0 the method setDestination(Object file) was already deprecated, see here : setDestination(Object file)
In Gradle 5.x this method has been removed, you must now use setDestination(File file) which takes a File parameter (see setDestination(File file) )
So you need to change your code into:
reports {
xml {
destination file("$buildDir/outputs/reports/checkstyle_report.xml")
}
}
All adjustment was done in my quality.gradle. Check config folder for quality.gradle file and change all usage of
destination "$reportsDir/pmd/pmd.xml"
to
destination file("$reportsDir/pmd/pmd.html")

Not able to extend a Gradle task

I'm new with Gradle (we're switching from SBT) and using it to build apps made with Play Framework.
I need to add some filtering on the resources before Gradle processes them (I would like to inject some build properties into the configuration to make them available from the code).
I've managed to "extend" the java processResources task, but, for some reason, I cannot do the same with play processPlayBinaryPlayResources.
processPlayBinaryPlayResources {
filter ReplaceTokens, tokens: [
"applicationVersion": version
]
}
Even this doesn't work :
def playVersion = "2.6.20"
def scalaVersion = "2.12"
def javaVersion = "1.8"
apply plugin: "java"
apply plugin: "idea"
apply plugin: "play"
model {
components {
play {
platform play: playVersion, scala: scalaVersion, java: javaVersion
injectedRoutesGenerator = true
}
}
}
processPlayBinaryPlayResources {
doLast {
println("ok")
}
}
dependencies {
compile "io.vavr:vavr:0.9.2"
compile "org.imgscalr:imgscalr-lib:4.2"
compile "com.typesafe.play:play-guice_${scalaVersion}:2.6.20"
compile "com.typesafe.akka:akka-http_${scalaVersion}:10.1.5"
compile "com.typesafe.play:filters-helpers_${scalaVersion}:2.6.20"
compile "ch.qos.logback:logback-classic:1.2.3"
}
It yields :
> Could not find method processPlayBinaryPlayResources() for arguments [build_6grwx7eowye82rdqpu4qlinur$_run_closure2#582d9dbd] on root project 'myproject' of type org.gradle.api.Project.
Any idea why ?
Your assumption of finding a task processPlayBinaryPlayResources is based on that, that the java plugin automatically adds a processResources task for all source set as process<sourceset_name>Resources . This happens only when a source set is added using java pugins sourceSets method which, in this case PlayBinaryPlay is not. The play plugin uses its own DSL to configure source sets.
Therefore when you try extending processPlayBinaryPlayResources it does not happen as no such tasks by that name exists in the first place and hence while delegating it to Project, you end up with this
Could not find method processPlayBinaryPlayResources() for arguments [build_6grwx7eowye82rdqpu4qlinur$_run_closure2#582d9dbd] on root project 'myproject' of type org.gradle.api.Project.
Lastly, I would like to add that the processPlayBinaryPlayResources task is not added by the play plugin.

Can Gradle generate a POM in such a way that it doesn't unnecessarily touch the output file?

We're creating a POM file like so:
task createPom {
doLast {
pom {
project {
groupId project.group
artifactId project.name
version project.ext.pomVersion
}
}.writeTo("${buildDir}/pom.xml")
}
}
This writeTo eagerly writes to the file even if no changes have occurred, which causes the jar to change, so later expensive tasks in the build (indexing, signing, building installers) all have to run as well.
I thought about writing to a temp file and diffing the two files somehow, but it seems like I'd have to write a lot of boilerplate just to do that, so I'm wondering whether there is a proper way to go about it.
What you need to do is to configure task's inputs and outputs which are used to determine whether your task is up-to-date or not. Here, how it should be done:
apply plugin: 'maven'
ext {
pomVersion = '3.1.4.5'
}
group = 'lolgroup'
task createPom {
outputs.file "${buildDir}/pom.xml"
inputs.property('group', project.group)
inputs.property('name', project.name)
inputs.property('pomVersion', project.ext.pomVersion)
doLast {
pom {
project {
groupId inputs.properties['group']
artifactId inputs.properties['name']
version inputs.properties['pomVersion']
}
}.writeTo("${buildDir}/pom.xml")
}
}
Here you can find a demo.

Customize gradle script for custom output folder and exe name

I am using Gradle to build my project.Script is working fine and it generates output file using task "installApp". The problem here is output folder is same as the project folder name and the executable file also creates with project name. for e.g. my project name is com.eclipse.spring.shell so the output of gradle script creates folder "com.eclipse.spring.shell" and name of the executable is also "com.eclipse.spring.shell".
SO my question is can i customize the output folder name as well executable file name in the script?
my gradle script looks like mentioned below:
description = '
Command Line Interface'
apply plugin: 'base'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
dependencies {
compile project(':com.eclipse.spring.Framework'), project(':com.eclipse.spring.User'), project(':com.eclipse.spring.Manager'), project(':com.eclipse.spring.Test')
}
mainClassName = "com.eclipse.spring.Shell.RvsBootstrap"
defaultTasks 'installApp'
run {
standardInput = System.in
}
task wrapper(type: Wrapper) {
description = 'Generates gradlew[.bat] scripts'
gradleVersion = '1.2'
}
You can set the output folder name under installApp and the applicationName under startScripts.
Example:
installApp {
destinationDir = file('build/install/'+'YourFolderName')
}
startScripts {
applicationName = 'YourApplicationName'
}

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