I am new to Ant scripts.
below is description of requirement
in my workspace, there are various projects and I have to have my project work on RAD and eclips
I'd suggest using Apache ivy for managing complex classpaths. It externalizes your build dependencies into a separate ivy.xml file.
Secondly, ivy can automatically download such dependencies, reducing the size of your project under source control.
Finally, this solution at first glance might appear horribly complex. It's advantage is that it's compatible with other build technologies such as Maven.
Ivy uses "configurations" to manage logical groupings of jars.
In this example the code compiles against the SLF4J api jars, but at run-time use different logging implementations:
<ivy-module version="2.0">
<info organisation="com.myspotontheweb" module="demo"/>
<configurations>
<conf name="compile" description="Required to compile application"/>
<conf name="runtime.simple" description="Runtime environment with minimal logging" extends="compile"/>
<conf name="runtime.complex" description="Runtime environment with logback enabled" extends="compile"/>
<conf name="test" description="Required for test only" extends="runtime.simple"/>
<conf name="build" description="ANT tasks used by build"/>
</configurations>
<dependencies>
<!-- compile dependencies -->
<dependency org="org.slf4j" name="slf4j-api" rev="1.6.4" conf="compile->default"/>
<!-- simple runtime dependencies -->
<dependency org="org.slf4j" name="slf4j-simple" rev="1.6.4" conf="runtime.simple->default"/>
<!-- complex runtime dependencies -->
<dependency org="ch.qos.logback" name="logback-classic" rev="1.0.3" conf="runtime.complex->default"/>
<!-- test dependencies -->
<dependency org="junit" name="junit" rev="4.10" conf="test->default"/>
<!-- Build dependencies -->
<dependency org="org.codehaus.groovy" name="groovy-all" rev="1.8.6" conf="build->default"/>
</dependencies>
</ivy-module>
Notes:
The ivy ANT tasks are imported as an antlib. The ivy cachepath task is used to turn an ivy managed configuration into normal ANT paths and the ivy report task produces a dependency report.
<project name="demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">
<target name="init">
<ivy:resolve/>
<ivy:report todir='${ivy.reports.dir}' graph='false' xml='false'/>
<ivy:cachepath pathid="compile.path" conf="compile"/>
<ivy:cachepath pathid="runtime.simple.path" conf="runtime.simple"/>
<ivy:cachepath pathid="runtime.complex.path" conf="runtime.complex"/>
<ivy:cachepath pathid="test.path" conf="test"/>
<ivy:cachepath pathid="build.path" conf="build"/>
</target>
..
..
The ivy retrieve task is used to populate a directory during your application's packaging phase:
<target name="war">
<ivy:retrieve pattern="${build.dir}/libs/[artifact].[ext]" conf="runtime.complex"/>
<war destfile="myapp.war" webxml="src/metadata/myapp.xml">
<fileset dir="${src.dir}/html/myapp"/>
<fileset dir="${src.dir}/jsp/myapp"/>
<lib dir="${build.dir}/libs"/>
<classes dir="${build.dir}/classes"/>
</war>
</target>
An Eclipse plugin for ivy is available.
It's also possible to generate IDE configuration files using an embedded groovy task. Following is an Eclipse example:
<target name="eclipse">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="build.path"/>
<ivy:cachefileset setid="libfiles" conf="compile"/>
<groovy>
<arg value="${src.dir}"/>
<arg value="${build.dir}/classes"/>
import groovy.xml.MarkupBuilder
//
// Generate the project file
//
project.log("Creating .project")
new File(".project").withWriter { writer ->
def xml = new MarkupBuilder(writer)
xml.projectDescription() {
name(project.name)
comment()
projects()
buildSpec() {
buildCommand() {
name("org.eclipse.jdt.core.javabuilder")
arguments()
}
}
natures() {
nature("org.eclipse.jdt.core.javanature")
}
}
}
//
// Generate the classpath file
//
// The "lib" classpathentry fields are populated using the ivy artifact report
//
project.log("Creating .classpath")
new File(".classpath").withWriter { writer ->
def xml = new MarkupBuilder(writer)
xml.classpath() {
classpathentry(kind:"src", path:args[0])
classpathentry(kind:"output", path:args[1])
classpathentry(kind:"con", path:"org.eclipse.jdt.launching.JRE_CONTAINER")
project.references.libfiles.each {
classpathentry(kind:"lib", path:it)
}
}
}
</groovy>
</target>
I would like to share the approach that I finally implemented.
There were classpath
, settings
and some project config xmls
that were dependent on runtime.
In each project we created a runtime_classpah
& runtime_settings
and configxml_runtime
version of each file.
Created a target
in ant
that takes in runtime
as param ,itrates over each project & copies contents of classpath_runtime
to classpath
,setting_runtime to settings
.
And a target that overrites configxml
with contents of configxml_runtime