Profiling not happening in gradle - spring

I have done below profiling for copying the file from a specific environment directory mentioned during the build command (gradle clean build -x test -Penv=prod) and delete the environment directory after it.
apply plugin: 'war'
apply plugin: 'java'
apply plugin: 'application'
task copyProfile(dependsOn:"deleteProfiles") {
def env = project.hasProperty('env') ? env : 'dev'
sourceSets.main.resources.srcDir "src/main/resources/environment/$env"
}
task deleteProfiles(type:Delete) {
delete "src/main/resources/environment"
}
tasks.clean.dependsOn(tasks.deleteProfiles)
the deleteProfiles task is working, but copyProfile task
sourceSets.main.resources.srcDir "src/main/resources/environment/$env"
i.e. copying files from $env directory and put in src/main/resources is not working. Can any one suggest me why it is not working

You didn't tell the task to copy anything...
Change your task to this:
task copyProfile(dependsOn:"deleteProfiles") {
def env = project.findProperty('env') ?: 'dev'
doLast {
project.copy {
from "src/main/resources/environment/$env"
into sourceSets.main.resources.srcDir
}
}
}
See Project.copy(...)

Related

How do I make a Task depend on the build.gradle itself?

I have the following build.gradle file. Currently the task generateSources runs every time gradle is executed ("BUILD SUCCESSFUL"). I would instead like it to only execute when the build.gradle file itself changes, so that the build is an incremental build ("UP-TO-DATE")
i.e. I want it's "input" to be the "build.gradle" itself.
How do I do this?
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'Main'
version = "1.0"
task generateSources() {
// inputs = ????
// onlyIf ???
outputs.upToDateWhen { true } // in the real code this is a file
doFirst {
println("Hello, World! $project.version")
}
}
compileJava.dependsOn generateSources
(The code above is simplified to the bare minimum. In reality the task generate some files, and they are configured properly in Task.output)
There are two ways as I know, finalizedBy and dependsOn
The finalizedBy means do this after this task
tasks.named("build") { finalizedBy("myTaskName") }
And the dependsOn means that after build do this task
tasks.named("myTaskName") { dependsOn("build") }
More information about tasks on the official docs
It turns out you can just put an input-dependency on the project file object itself with something like inputs.file project.buildFile.
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'Main'
version = "1.0"
task generateSources() {
inputs.file project.buildFile // only rebuild when _this_ build.gradle changes
outputs.upToDateWhen { true } // in the real code this is a file
doFirst {
println("Hello, World! $project.version")
}
}
compileJava.dependsOn generateSources

Gradle: change property of task from a plugin

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

Extracting files downloaded as a dependency in Gradle

I've got a gradle script that goes something like the following:
apply plugin: 'java'
apply plugin: 'maven'
defaultTasks 'build'
ext.basedir = file('.').getAbsolutePath()
repositories{
maven { url "http://package.repo.com:8081/nexus/content/repository
}
configurations.all {
// check for updates every build
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
dependencies {
compile group:'com.repo.wwpd', name:'kernel_utilities', version:'3.0.0', changing:true
}
task copy_dependencies(type: Copy) {
from configurations.compile
into basedir+'\\install'
include '*'
{ FileTree ioTree = fileTree(dir: "C:\\Users\\username\\install") }
ioTree.each { f ->
copy {
from tarTree(resources.gzip(f))
into "C:\\Users\\user\\test"
}
}
}
The goal is to get the dependencies, move them to the install folder, and extract them from the tar files to the test folder.
The problem appears to be that the task is executed before the dependencies are downloaded. So if the files already exist in install it works fine, but if the install folder is empty the result is an empty test folder but a full install folder.
[EDIT - updated with comments Peter N.]
This should be one way to solve your case ; note it contains two tasks, choose the one(s) that fulfill your needs: simple copy VS full extraction
def installDir = "${buildDir}/install"
def extractDir = "${buildDir}/extract"
// task to copy dependencies
task copyDependencies(type: Copy) {
from configurations.compile
into installDir
}
// task to extract dependencies
task extractDependencies(type: Copy) {
from configurations.compile.collect{tarTree (it)}
into extractDir
}

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