Is it possible to launch Gradle tasks from Ant? - ant

I am researching replacements for Ant. I've looked at Gant and Gradle.
Is it possible to kick off a Gradle task from Ant? This is possible in Gant with a taskdef.
<taskdef
name = "gant"
classname = "org.codehaus.gant.ant.Gant"
classpathref = "classpath"
/>
<gant />
Is there something similar Gradle? I'm eager to start migrating from Ant to Gradle, but we have a large Ant infrastructure and any Gradle build scripts I create need to be callable from Ant.
Thanks!

Create a macrodef for gradle, call it just like any other task. Here is the setup and an example...
<!-- Gradle path stuff -->
<property environment="env" />
<condition property="gradle.executable" value="${env.GRADLE_HOME}/bin/gradle.bat" else="${env.GRADLE_HOME}/bin/gradle">
<os family="windows" />
</condition>
<!-- Macro def, gives us an ant 'gradle' task-->
<macrodef name="gradle">
<attribute name="task" />
<sequential>
<exec executable="${gradle.executable}" dir="." failonerror="true">
<arg value="#{task}" />
</exec>
</sequential>
</macrodef>
Example of using the macro def
<!-- Example, call grade with new macro -->
<target name="example">
<gradle task="build" />
</target>

Instead of switching build technology, why not use a combination of ivy and groovy to extend the capabilities of your existing ant builds?
An example is the following posting:
Parse HTML using with an Ant Script
BTW I'm a big fan of Gradle, however, like you I have to live with and support a large ANT legacy :-)

Actually I want to do the same thing and where implemented by calling a sh file and then the sh was calling the gradle but it was too much around the bush and finally the following code made it to work cool..
Hope this will help you..
<property environment="env" />
<property name="gradle.wrapper.executable" location="${env.GRADLE_HOME}/bin/gradle" />
<target name="dependencies-report" description="Creates a text file report of the depdency tree">
<exec executable="${gradle.wrapper.executable}" dir=".">
<arg value="dependencyReport" />
</exec>
</target>

Gradle doesn't offer an Ant task to run a Gradle build from Ant. What you could do is to invoke a Gradle command (like gradle build) from Ant.
In terms of Ant integration, Gradle offers two features: Importing Ant builds, and reusing Ant tasks.
Gradle is very different from Gant. Gradle is an entire new build system; Gant is a thin layer above Ant.

Related

Generating HTML report of testcases results in SoapUI

I have an testsuite of API testing in SOAP UI.
I want an HTML report of testcases results. I am using basic SOAP UI version. Give me a solution apart from SOAP UI Pro.
Yes, it is possible to generate Junit Style HTML reports using SoapUI Opensource Edition as well.
All you need to do is the execution of tests has to be done
use Apache-Ant software, more details on installing and configuring here
write build script
Here is the sample build script(build.xml):
Note that modify the SOAPUI_HOME(or define environment variable), soapui project file path, results directory path according to your environment.
<project basedir="." default="testreport" name="ant script for testing soapui project">
<property environment="env"/>
<property name="soapui.project" value="/app/demo-soapui-project.xml"/>
<property name="results.dir" value="/tmp/results"/>
<property name="reports.dir" value="${results.dir}/Reports"/>
<property name="html.dir" value="${reports.dir}/html"/>
<target name="execute.project">
<exec dir="${env.SOAPUI_HOME}" executable="testrunner.sh">
<arg line="-raj -f ${results.dir} ${soapui.project}" />
</exec>
</target>
<target name="testreport" depends="execute.project">
<mkdir dir="${reports.dir}"/>
<junitreport todir="${reports.dir}">
<fileset dir="${results.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${html.dir}" />
</junitreport>
</target>
</project>
and execute following command (run soapui project and generate report):
ant
There is also simple way (i.e., every thing configured and readily available envrionment) if you are willing to use this docker image.
Short video also available there on how to.

Download jars from nexus using ant build tool as done automatically in Maven

I have a build.xml(ant based) which requires some jar from nexus to get copied in existing lib folder. i.e when it builds it should copy the jar from nexus with some version defined & then copy in lib & do compilation.
like happen in maven we define the artifact & its version . If changed will automatically download it from maven repo.
how can i do this in ant based builds?
experts pls advice.
I have taken the example listed in this thread one step further and created a macrodef to clean things up a bit for re-use. See below for downloading two artifacts from nexus (one snapshot, one release).
<project>
<target name="get-all">
<mkdir dir="lib" />
<nexus-get
groupId="foo.bar"
artifactId="some-artifact"
version="1.0.28"
repo="releases"
extension="jar"
dest="lib"
/>
<nexus-get
groupId="foo.bar"
artifactId="another-artifact"
version="1.0.0-SNAPSHOT"
repo="snapshots"
extension="jar"
dest="lib"
/>
</target>
<macrodef name="nexus-get">
<attribute name="groupId"/>
<attribute name="artifactId"/>
<attribute name="version"/>
<attribute name="repo"/>
<attribute name="extension"/>
<attribute name="dest"/>
<sequential>
<get src="http://my-nexus:9999/nexus/service/local/artifact/maven/redirect?r=#{repo}&g=#{groupId}&a=#{artifactId}&v=#{version}&e=#{extension}" dest="#{dest}/#{artifactId}.#{extension}" usetimestamp="true" />
</sequential>
</macrodef>
You would probably be interested in Ivy. It is a sub-project of Ant for dependency management. It is perfect for your situation because it can read Maven repositories and provides Ant tasks for downloading the published artifacts, constructing class paths from them, etc. It supports your use case of getting the most recent version of a dependency if you configure it to ask for the "latest.release" revision of the module.
Although there are surely specific ways to combine ant and maven the simplest thing (if you know the nexus URL and your artifact parameters to construct the download URL) would be just to use the ant Get task.
<project name="MyProject" default="resolveDependencies" basedir=".">
<target name="resolveDependencies">
<mkdir dir="lib" />
<get src="http://search.maven.org/remotecontent?filepath=log4j/log4j/1.2.9/log4j-1.2.9.jar" dest="lib/log4j-1.2.9.jar" usetimestamp="true" />
</target>
</project>
Perhaps use the Maven Ant Tasks.
As shown on http://maven.apache.org/ant-tasks/examples/dependencies.html
Can list dependencies in ant, and also do things like copy them
I think the section Using FileSets and the Version Mapper covers your need
You can use is filesetId, which will give you a fileset reference that can be used to copy files into a particular location. For example, to populate WEB-INF/lib with your dependencies you could use the following:
<artifact:dependencies filesetId="dependency.fileset" useScope="runtime">
<dependency groupId="junit" artifactId="junit" version="3.8.2" scope="test"/>
</artifact:dependencies>
<copy todir="${webapp.output}/WEB-INF/lib">
<fileset refid="dependency.fileset" />
<!-- This mapper strips off all leading directory information -->
<mapper type="flatten" />
</copy>

Is there a way to call the ant 'ant' target with '-lib' option

I'm developing an ant script which is calling another ant script using the <ant> task. This ant script is an installer a Java product and is to be used by our customers, who will have ant installed separately.
The script being called uses the antlr task <antlr:ant-antlr3>. To do this I must place the ant-antlr3.jar file in the ant lib directory, as well as adding antlr-3.2.jar to the classpath.
But I don't want to have this dependency of having ant-antl3.jar file in the client's own installed version of ant.
Is there a way of providing the equivalent to ant's command-line '-lib' option to specify other paths for jars to be added to antlib using the <ant> task itself?
I've taken a look at the online docs and there doesn't seem to be a way.
Thanks
I believe the accepted way to do this is to manually set up your classpath in the build file rather than implicitly including it via the global ant lib directory. i.e.
<path id="master-classpath">
<fileset dir="${lib}" />
<fileset file="${findbugs-base}/lib/annotations.jar" />
<pathelement location="${build-classes}" />
</path>
You can then use this path element in any task that can accept classpath args such as javac
<javac
destdir="${out}"
source="1.5"
target="1.5"
debug="true">
<src path="${src}" />
<classpath refid="master-classpath" />
</javac>
This way, the global ant set up isn't a dependency, and you can specify any files you might need for any build, as specifically as you need to (down to a given call or target).
Obviously, this is all to be carried out in the build file you're calling from the clients' build file. This way, when you call out to yours, the classpath will be set up exactly as you desire.
Another far less idiomatic possibility would be to literally shell out with the Exec Task and call ant that way. Obviously, with the provision of the Ant task, the developers of ant don't recommend you doing that. It is an option, nonetheless.
Tim's answer gives most of the story, but in order to run Ant and set JVM options, you'd need to invoke it via the java task.
There is an example of running this way in the Ant docs, here slightly modified to include -lib:
<java
classname="org.apache.tools.ant.launch.Launcher"
fork="true"
failonerror="true"
dir="${sub.builddir}"
timeout="4000000"
taskname="startAnt"
>
<classpath>
<pathelement location="${ant.home}/lib/ant-launcher.jar"/>
</classpath>
<arg value="-lib"/>
<arg value="${path.to.your.antlr.jar}"/>
<arg value="-buildfile"/>
<arg file="${sub.buildfile}"/>
<arg value="${sub.target}"/>
</java>

How can I process multiple xsd schemas using jaxb and the Ant xjc task?

I'm using jaxb to generate java object class from xml schemas within an Ant script like so:
<!-- JAXB compiler task definition -->
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask"
classpathref="master-classpath"/>
<!-- Generates the source code from the ff.xsd schema using jaxb -->
<target name="option-generate" description="Generates the source code">
<mkdir dir="${generated-src.dir}/${option.dir}"/>
<xjc schema="${config.dir}/ff.xsd" destdir="${generated-src.dir}"
package="${option.package.name}">
<arg value="-Xcommons-lang" />
<arg value="-Xcommons-lang:ToStringStyle=SHORT_PREFIX_STYLE" />
<produces dir="${generated-src.dir}" includes="**/*.java" />
</xjc>
</target>
Now, this works brilliantly for one schema (ff.xsd in this example). How can I process several schemas (i.e. several xsd files)?
I tried having a separate ant task per schema, but somehow, this doesn't work as Ant process the first task and then says that the "files are up to date" for the following schemas!
<target name="process-resources" description="Process resources">
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask"/>
<xjc destdir="${basedir}/target/generated-sources/jaxb"
extension="true">
<schema dir="src/main/xsd"
includes="JaxbBindings.xsd,CoreTypes.xsd"/>
</xjc>
</target>
<target name="generate-jaxb-code">
<java classname="com.sun.tools.internal.xjc.XJCFacade">
<arg value="-p" />
<arg value="com.example"/>
<arg value="xsd/sample.xsd" />
</java>
</target>
works with the JAXB that is part of JDK 6 seems that the ANT task only ships with the downloadable JAXB but since JAXB is part of the JDK its probably not a good idea to take the latest release of the JAXB and add to the classpath of the JDK since that means you probably need to mess around with classloader settings, to pickup the downloaded version rather than the version within the JDK.
You can also just include the other xsd files in your main xsd file, using a command like the following:
<xs:include schemaLocation="path/to/secondschema.xsd"/>

How do I pass an argument to an Ant task?

I'm not very good with Ant, but we're using it as a build tool. Right now, we can run "ant test" and it'll run through all the unit tests.
However, I'd love to be able to do something like ant test some_module and have it accept some_module as a parameter, and only test that.
I haven't been able to find how to pass command line args to Ant - any ideas?
One solution might be as follows. (I have a project that does this.)
Have a separate target similar to test with a fileset that restricts the test to one class only. Then pass the name of that class using -D at the ant command line:
ant -Dtest.module=MyClassUnderTest single_test
In the build.xml (highly reduced):
<target name="single_test" depends="compile" description="Run one unit test">
<junit>
<batchtest>
<fileset dir="${test.dir}" includes="**/${test.module}.class" />
</batchtest>
</junit>
</target>
You can also define a property with an optional default value that can be replaced via command line, e.g.
<target name="test">
<property name="moduleName" value="default-module" />
<echo message="Testing Module: ${moduleName}"/>
....
</target>
and run it as:
ant test -DmoduleName=ModuleX
What about using some conditional in your test target and the specifying -Dcondition=true?
<target name="test" depends="_test, _test_if_true>
...
</target>
<target name="_test_if_true" if="condition">
...
</target>
<target name="_test" unless="condition">
...
</target>
Adapted a bit from the ant faq.
You can define a property on commandline when invoking ant:
ant -Dtest.module=mymodulename
Then you can use it as any other ant property:
...
<fileset dir="${test.dir}" includes="**/${test.module}.class" />
...
Have a look at Ant's manual.
I tried the solutions posted here for the very same original question. Yes just use ant -D<arg_name>. THe -D is a "keyword" I guess. I'm no ant expert and have not read the manuals in detail. Then inside the ant XML files can be accessed like: ${arg_name}
For instance you can have an argument name like: arg.myarg, so in XML ${arg.myarg}.
Ant really doesn't have parameters_ for the build file. I can think of a few ways to do this:
Use a special target to specify the tests. You can use the <for/> task from AntContrib to allow you to specify multiple tests. You'll need to download the Ant-Contrib jar file. I recommend placing it inside your project under the `${basedir}/antlib/antcontrib" directory. That way, when others checkout your project, they get the needed Ant-Contrib jar file.
<property name="antlib.dir" value="${basedir}/antlib"/>
<property name="antcontrib.dir" value="${antlib}/antcontrib"/>
<!-- Set up the ant contrib tasks for your use -->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<fileset dir="${antcontrib.dir}"/>
</classpath>
</taskdef>
<target name="select-test"
description="Select the tests to run"
depends="test-compile"
if="junit-tests">
<for parameter="module"
list="${junit-tests}"
delimiter=" ">
<sequential>
<junit
fork="true"
...>
<batchtest todir="$target/unit-tests">
<fileset dir="${test.destdir}">
<include name="**/#{module}.class"/>
</fileset>
</junit>
</sequential>
</for>
</target>
You cab now run multiple tests like this:
$ ant -D"test-one test-two test-three" select-test
You could try this to access one target at a time. Add these lines to your build.xml file :
<project name="whatever" default="default">
<input message="Please select module:" addproperty="mod" />
<target name="default" depends="${mod}/>
...
</project>
This allows you to enter the module you want to execute and execute that itself instead of running the whole build.xml
You might need to make a few more changes to your build.xml for this to work perfectly.
For the arguments , there is Facility called property. You need to set the property. As in ANT plain arguments is taken as target name.
Lest say you have two modules in your project ModuleX and ModuleY where ModuleX has 2 testcases to run and ModuleY with 10 testcases.
You could do something like this :
ant runTestsOnModule -Dtestmodule="ModuleX"
OR to test all modules by calling
ant tests
<target name="runTestsOnModule">
<antCall target="testcase${testmodule}"/>
</target>'
<! -- run single module -->
<target name="runTestsOnModule">
<antCall target="testcase${testmodule}"/>
</target>
<!--run all tests-->
<target name="tests">
<antcall target="testcaseModuleX">
<antcall target="testCaseModuleY">
</target>
<target name="testcaseModuleX">
..run junit task to call 2 testcase
</target>
<target name="testcaseModuleY">
....run junit task to call 10 testcase
</target>

Resources