How to adjust and execute Ant task from BeanShell script? - ant

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"

Related

Copy subfolder with groovy in Jenkins

I am trying to script a groovy script which copies a complete folder with all subfolder and jobs to the actual folder, where the script is executed.
Here you can see how my folderstructure looks like.
--> Templ
|-->Folder
|-->Folder
|-->Subfolder
|-->Subsubfolder
|-->Subfolder
|-->Folder
-->Execution 2020
|-->Copyscript
I tried with different Plug-Ins like Jobcopy Builder.
Finally I tried with groovy scrips but nothing seems to work.
the simplest way to use AntBuilder
def ant = new AntBuilder()
ant.copy(todir: myDir) {
fileset(dir: "src/test") {
include(name: "**/*.java")
}
}
example taken from here
http://docs.groovy-lang.org/latest/html/documentation/ant-builder.html
to see all parameters of ant copy command see documentation:
https://ant.apache.org/manual/Tasks/copy.html

Grails 3.0.9 gradle new task with custom environment

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'

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'
}
}
}

Classpath for ant plugins when using ANTBuilder from Gradle

I have a build.gradle file which loads PMD (downloading it from upstream Maven), and then loads an Ant build.xml file which requires PMD:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'pmd:pmd:4.2.5'
}
}
ant.importBuild 'shared-build.xml'
However, the Ant import fails:
taskdef class net.sourceforge.pmd.ant.PMDTask cannot be found
using the classloader AntClassLoader[]
at org.apache.tools.ant.ProjectHelper.addLocationToBuildException(ProjectHelper.java:551)
[...]
at org.gradle.api.internal.project.DefaultAntBuilder.importBuild(DefaultAntBuilder.groovy:76)
How can Gradle's ant integration be instructed to make this available?
There's no straighforward way to do it, as Gradle does not offer any API support for this. So you need to hack it some way.
For example, you can do something like this, right before calling ant.importBuild
org.apache.tools.ant.Project.class.classLoader.addURL( file('libs/somelib.jar').toURI().toURL() )
Alternatively you can call the addURL() method with the paths you get through the Gradle's dependency resolution (again, this should be executed before the call to ant.importBuild).
configurations { someconf }
dependencies { someconf "org.eclipse.jdt:ecj:3.6.1" }
def antClassLoader = org.apache.tools.ant.Project.class.classLoader
configurations.someconf.each { File f ->
antClassLoader.addURL(f.toURI().toURL())
}
Of course, another solution would be to have the classpath defined inside your build.xml file so you won't have to do anything from Gradle.
See some input here http://gradle.1045684.n5.nabble.com/How-to-add-to-classpath-for-ant-importBuild-td3268631.html

Resources