I am pretty new in using Gradle, and I have some troubles. I'm trying to import ant script into gradle and then run some ant targets. Then I cal gradle task in Jenkins.
So far what I have:
task MyAnt<< {
ant.importBuild 'build.xml'
def antTargetsNames = ant.references.get("ant.targets").collect { it.name }
println "\nAnt Targets: ${antTargetsNames}\n"
call Ant traget, e.g. compileAnt
}
Basically I print all targets now, but I do not know how to call ant target within gradle task.
Is it possible to do?
ant.importBuild 'build.xml' has to happen outside the task action. The result is that you'll get a Gradle task for each Ant target, which can be executed from the command line. Tasks can't execute other tasks but can depend on them (e.g. myTask.dependsOn(someAntTarget), again outside a task action. For more information, check the Gradle User Guide and the samples in the gradle-all distribution.
Related
I've heard you should type command
grails war
to build your project. I've thought to this point that Gradle is responsible for building the app in Grails. I've been doing the latter with conviction that my app is built. So what's the difference between
grails war
and
gradle build
?
Is it just that grails war is gradle build + create the war file?
It is not that simple to compare Grails and Gradle. Gradle is a build tool, while Grails is a web application framework.
However, Grails provides a command line tool, that's described in the docs:
Grails incorporates the powerful build system Gant, which is a Groovy wrapper around Apache Ant.
So, Grails does not use Gradle.
The basic usage of the grails command looks the following:
grails [environment]* [command name]
Where especially the command name parameter must be one out of predefined values. You can find the documentation on the war command here.
The basic usage of the gradle command looks the following:
gradle [option...] [task...]
The listed task parameters can be names of tasks created either in the build.gradle script or by plugins. All mentioned tasks and their respective task dependencies will be executed. If you use the Gradle War Plugin, it will generate a war task, which will also (transitively) be added as a task dependency of the build task. So whenever you call gradle build, a WAR file will be created. You can also call this task directly via gradle war.
EDIT
I just learned that Grails can or even does use Gradle beginning at a certain version. I even found a list on which Grails command calls which Gradle task. According to this list, calling grails war is equivalent to calling gradle assemble. The assemble task directly depends on the war task.
gradle build is a Gradle lifecycle task which usually consists of other tasks required to build a software like compileJava and other lifecycle tasks like assemble and check.
In case of Grails it delegates build to Gradle and to war task and it doesn't include check lifecycle during which unit tests will be executed.
I have created one gradle project for Geb-Spock. In this project I have created scripts in Groovy class.
I would like to execute those scripts from command line with the help of gradle commands. So that I can achieve Jenkins - Gradle - Geb integration.
Can you please help me to get the command which can execute the gradle-groovy scripts from windows command line. Thanks for your help on this.
Maybe so. If I understood correctly, you want run it like:
gradle myScript
You define a Task in gradle and import your groovy class into build.gradle like:
import com.MyScript
task myScript(){
new MyScript().run()
}
You also need a dependecy for your script (look her). e.g.:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath files('path/to/myScript/lib')
}
}
Also take a look how to compile the code for buildscript classpath if you need: https://docs.gradle.org/current/userguide/organizing_build_logic.html#sec:build_sources
I'm not really sure that you need all that. If you just want to run some Geb-Spock Test in Gradle take a look in https://github.com/geb/geb-example-gradle
If a plugin defines a series of tasks, is it possible to inject a dependency into these, such that the dependency is called before the plugin-defined task is executed?
The native-artifacts plugin defines buildNar (and buildNarxxx, where xxx is the platform config) tasks. It also defines extractNarDepsxxx (where xxx is the platform config for the Nar to be built). extractNarDeps is not called before builder, so the build fails as required dependencies are not downloaded prior to the attempted build.
How do I inject extractNarDepsxxx as a dependency into buildNarxxx?
Ok. Consider the following example:
apply plugin: 'java'
task someTask
task anotherTask
tasks.classes.mustRunAfter(anotherTask)
tasks.build.dependsOn(someTask)
There's a single plugin applied java and two custom tasks someTask and anotherTask.
Task build (taken from java plugin) dependsOn someTask. It means that when You run gradle build this task will be executed.
Task classes mustRunAfter anotherTask. So when You type gradle build anotherTask, anotherTask will run before build.
Try it. An ask further questions when needed.
I have this Java project there I import an Ant build.xml file with some tasks, like this:
ant.importBuild 'build.xml'
task myTaskA(dependsOn: ':Modules:MyModule:assemble') << {
// do stuff here...
}
compileJava.dependsOn(myTaskA)
configure(jar) {
include 'classes.dex'
}
jar.dependsOn(antCompile)
The task antCompile comes from the Ant build.xml script. However, for some reason, this task is being called at start up when invoke gradlew assemble, it doesn't even wait for the jar task to start.
Also, the antCompile task is defined as the following target in build.xml:
<target name="antCompile" depends="-setup">
</target>
That Ant target, -compile is always the first task to be executed when I invoke gradlew assemble. This doesn't make any sense. That task is never invoked anywhere, it's only a dependency of antCompile. Why is it being executed?
This is, obviously, not what I want... How can I prevent such behaviors?
Seems to work as expected. The build script makes jar depend on antCompile, which according to your words depends on -compile. assemble depends on jar, so executing gradle assembmle should run -compile first.
In any case, it should be said that ant.importBuild has known limitations, and can result in differences in behavior compared to running the Ant build directly. Also you'll lose many of Gradle's advantages when not describing the build in terms of Gradle's own abstractions. Therefore I recommend to port the build to Gradle, rather than using ant.importBuild (which isn't used that often in the real world). Note that it's perfectly fine to reuse Ant tasks in cases where Gradle doesn't provide any equivalent.
I'm trying to Gradle-ize our build by using Gradle to execute the Ant build. I'm using the java plugin so I can set source/target and I'm using ant.importBuild 'build.xml'. When I execute Gradle, I get the error above. I understand that both Ant and Gradle have these targets/tasks in common: clean, jar, javadoc, test. One option is to change the Ant target names in build.xml, but I'm hoping there's an easier way as I have a lot of projects and build files. I found this "wrapper" solution (http://issues.gradle.org/browse/GRADLE-771), but this did not work for me. How can I solve this?
Your options are:
Do not apply the plugin to the same project that imports the Ant build.
Rename the conflicting targets in the Ant build script.
You can rename all the ant targets:
ant.importBuild('build.xml') { String oldTargetName ->
return 'ant_' + oldTargetName
}