Grails 3.0.9 gradle new task with custom environment - grails

I'm trying to create a new task in build.gradle for running "check" with a custom environment called "integration".
I've tried several things and I've been able to run the check task from my custom task but I can't find the way to set the environment in int.
This is the task that I have right now in build.gradle. The comments are for things that I've tried but all fail because the property doesn't exist.
I've also tried setting the task as type:GrailsTask, but with no luck
task integrationCheck(){
dependsOn "check"
//env = "integration"
//grailsEnv = "integration"
//tasks.check.env = "integration"
//check.env = "integration"
//project.property("grailsEnv")
}
Edit: per #vinay-prajapati suggestion
Have tried with this:
task integrationCheck << {
systemProperty 'spring.profiles.active', 'integration'
}
And it gives this error:
Execution failed for task ':integrationCheck'.
> Could not find method systemProperty() for arguments [spring.profiles.active, integration] on root project 'my-project'

Related

Creating Jenkins pipeline using a multi-project gradle build

I have a multi-project Gradle build working locally.
There is a parent gradle file, build.gradle
The settings.gradle file assigns the projects to their respective gradle build files:
include 'load'
include 'loadRemote'
project(':loadRemote').buildFileName = 'remoteLoad_build.gradle'
project(':load').buildFileName = 'load_build.gradle'
rootProject.name = 'EquipLoad'
The build.gradle parent file calls a buildAll command to build the 2 projects from the command line locally.
I created a Jenkins file to build both projects. But the Jenkins pipeline does not recognize the specific project tasks.
These are the tasks for the buildAll command
gradle.projectsEvaluated {
task compileAll (dependsOn: [project(':loadRemote').remoteLoadCleanCompileStage]) {
compileAll.finalizedBy project(':load').loadCleanCompileStage
}
task packageAll (dependsOn: [project(':loadRemote').remoteLoadPackage]) {
packageAll.finalizedBy project(':load').loadPackage
}
task buildAll (dependsOn: [compileAll]) {
buildAll.finalizedBy packageAll
}
}
The error in Jenkins is that it does not recognize the task project(':loadRemote').remoteLoadCleanCompileStage
How can I identify a multi-build project in Jenkins?
Do I have to add the settings.gradle file?
UPDATE
I thought that the different build files could not be located in the project so I added this to the settings.gradle file
rootProject.name = 'EquipLoad'
include 'load'
project(':load').projectDir = new File(settingsDir, rootProject.rootDir.getAbsolutePath() + "/Load")
project(':load').buildFileName = 'load_build.gradle'
include 'loadRemote'
project(':loadRemote').projectDir = new File(settingsDir, rootProject.rootDir.getAbsolutePath() + "/LoadRemote")
project(':loadRemote').buildFileName = 'remoteLoad_build.gradle'
The error is still the same, the build.gradle file (parent) does not recognize the dependency task project(':loadRemote').remoteLoadCleanCompileStage
Looking at the debug statements, the child build gradle files are found and identified:
Evaluating project ':loadRemote' using build file '/var/.../loadRemote/remoteLoad_build.gradle'.
The same text is shown for the load build file.
Yet the tasks within these gradle build files are not recognized in the parent build.gradle file.
The problem was a simple case sensitive mistake.
I named the folders: Load and LoadRemote. But identified them in the gradle scripts as ':load' and ':loadRemote'. By changing the script text to ':Load' and ':LoadRemote' fixed my problem.

Add command to Grails build process

I am using the grails-cdn-asset-pipline plugin. I've gone through the installation and configuration steps on GitHub and I reach the usage section which says
Add this command to your build process (usually before war generation and deployment).
// If all the settings are defined in your Config.groovy
grails asset-cdn-push
// Or
grails asset-cdn-push --provider=S3 --directory=my-bucket --gzip=true --storage-path=some-prefix --expires=365 --region=eu-west-1 --access-key=$MY_S3_ACCESS_KEY --secret-key=$MY_S3_SECRET_KEY
Where in my project do I put this command?
Is it something that I can do within the context of my project, or do I need to keep it separate in another build process and run it in an environment like Jenkins?
In _Events.groovy, I tried to invoke the script in the eventCreateWarStart, but I am having no luck there. (Code taken from this question)
eventCreateWarStart = { warName, stagingDir ->
def pluginManager = PluginManagerHolder.pluginManager
def plugin = pluginManager.getGrailsPlugin("cdn-asset-pipline")
def pluginDir = plugin.descriptor.file.parentFile
Map<String, String> env = System.getenv()
final processBuilder = new ProcessBuilder()
processBuilder.directory(new File("${cdnAssetPipelinePluginDir}/scripts"))
processBuilder.command([env['GRAILS_HOME']+"/bin/grails","cdn-asset-push"])
println processBuilder.directory()
Process proc = processBuilder.start()
proc.consumeProcessOutput(out, err)
proc.waitFor()
}
This link explains the run-script functionality which was merged into Grails 1.3.6. But I ran into the same problem of not knowing where to run it automatically.

Gradle can't execute an Ant target

I have a gradle task that has a dependsOn and then needs to execute an Ant task called runcukes. Gradle won't call the ant task, however:
ant.importBuild 'build.xml'
task runCukes(dependsOn: restoreSchema) {
runcukes
}
Running gradle with -d shows that gradle doesn't recognize the Ant target runcukes:
Finished executing task ':restoreSchema'
17:28:37.506 [LIFECYCLE] [org.gradle.TaskExecutionLogger] :runCukes
17:28:37.506 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter ]
Starting to execute task ':runCukes'
17:28:37.507 [INFO] [org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter ]
Skipping task ':runCukes' as it has no actions.
17:28:37.507 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter
Gradle doesn't recognize that it has to execute the ant target runcukes.
The gradle docs don't show anything as complicated as a task that has a dependsOn that then executes an ant task. (They only show "Hello World" examples as dependencies.)
What do I have to do to get Gradle to execute an Ant task after executing a dependsOn?
Thanks.
Just putting the Ant task name into the configuration block of a task won't execute it. This should do what you want if I understood your requirements correctly:
ant.importBuild 'build.xml'
runcukes.dependsOn restoreSchema
task runCukes(dependsOn: runcukes)

gradle - multiple module project - ant wrong execution dir

I've a multiple module projecy managed by gradle. The directory structure is as follows:
monitoring
client
server
When I invoke 'gradle war' on monitoring level I obtain the following exeception:
"monitoring/js does not exist."
Which comes from client's build.gradle:
task copyJs << {
'mkdir src/main/webapp/js'.execute()
def ant = new groovy.util.AntBuilder()
ant.copy(todir: 'src/main/webapp/js') {
fileset(dir: 'js') {
include(name: '**/*.js')
}
}
}
The exception occurs because the mentioned task is executed on the root level of the project. How to change it to be executed on the appropriate (client) level? How to change the basedir for the ant task that is used?
Another option would be to use a copy task:
task copyJs(type:Copy){
into('src/main/webapp/js')
from('js') {
include '**/*.js'
}
}
This has the benefit, that the output dir is automatically created if it does not yet exists. Another benefit of using the copy task instead of the copy operation as in the answer above is, that the copy task supports incremental build execution (up-to-date checks).
regards,
René
Should be done as explained here
task copyJs << {
file('src/main/webapp/js').mkdir()
copy {
into 'src/main/webapp/js'
from('js') {
include '**/*.js'
}
}
}

How to adjust and execute Ant task from BeanShell script?

I have large jar task in my ant build file. But I don't want to execute it immediately. I want to add some filesets to the task and only than execute it. I don't want to define the jar task completely in the script. How to do that?
I've tried following approach from the script:
project.addBuildListener(new BuildListener() {
...
void taskStarted(BuildEvent event){
Jar j1 = (Jar)event.getTask();
...//adjustments here
}
});
But it gives: "java.lang.ClassCastException: Cannot cast org.apache.tools.ant.UnknownElement to org.apache.tools.ant.taskdefs.Jar"

Resources