sorting as well as removing duplicacy - ant

<?xml version="1.0"?>
<project name="sortlist11" default="sortlist11">
<taskdef resource="net/sf/antcontrib/antcontrib.properties" />
<property name="my.list" value="z,y,x,w,v,u,t" />
<property name="my.list1" `value="5,3,6,1,8,4,6" `/>
<target name="sortlist11">
<sortlist property="my.sorted.list" value="${my.list}" delimiter="," />
<sortlist property="my.sorted.list1" value="${my.list1}" delimiter="," />
<echo message="${my.sorted.list}" />
<echo message="${my.sorted.list1}" />
</target>
</project>
here second echo print 1,3,4,5,6,6,8 but how can i remove redundancy?

Every language running in JVM via Bean Scripting Framework may be used in ant with full access to the ant api. Here's a solution with Groovy for your problem =
<project>
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<property name="my.list" value="z,y,x,w,v,u,t"/>
<property name="my.list1" value="5,3,6,1,8,4,6"/>
<groovy>
properties.'my.sorted.list' = properties.'my.list'.split(',').sort().toString()
properties.'my.sorted.list1' = properties.'my.list1'.split(',').toList().unique().sort().toString()
</groovy>
<echo>
$${my.sorted.list} => ${my.sorted.list}
$${my.sorted.list1} => ${my.sorted.list1}
</echo>
</project>

Related

Ant creates jar files in wrong destination

Below is my build.xml file for ant.
<?xml version="1.0" encoding="UTF-8"?>
<project name="Struts2ProductsDemo" default="jar">
<property name="src.dir" location="src" />
<property name="build.dir" location="L:\build" />
<property name="project.name" value="Struts2ProductsDemo" />
<target name="clean">
<delete dir="${build.dir}" />
</target>
<target name="makedir">
<mkdir dir="${build.dir}" />
<mkdir dir="${build.dir}/classes" />
</target>
<target name="compile" depends="clean,makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}/classes" />
</target>
<target name="jar" depends="compile">
<jar destfile="${build.jar}/jars/${project.name}.jar" basedir="${build.dir}/classes" />
</target>
The jar files are created in: C:\Users\San\EclipseWS\Struts2ProductsDemo\${build.jar}\jars\Struts2ProductsDemo.jar
I am expecting the jar to be here: ${build.jar}/jars/${project.name}.jar
Property build.jar is never set. You have to define it. For example, you may add this to your build:
<property name="build.jar" location="target"/>

Ant Script Loop through property file and replace values dynamically

I got a requirement to loop through some XML files, replace the environment specific values in it and create new set of XML files. The environment specific values are to be taken from property file. I am able to loop through a directory to read all the files and replace some specific value using xmltask as below.
<target name="updateConfig" description="update the configuration" depends="init">
<xmltask todir="${ConfigDestDirectory}" report="false" failwithoutmatch="true">
<fileset dir="${ConfigSourceDirectory}">
<include name="*.xml"/>
</fileset>
<replace path="/:application/:NVPairs/:NameValuePair[:name='Connections/HTTP/HostName']/:value/text()" withXml="localhost"/>
</xmltask>
<echo>Replaced Successfully</echo>
</target>
But I would like to read through a property file and get the path/value from it.
I tried using property selector,property,var as different options for this case and manage to get the path but not the value. Below are the snippet of property file and the target that I am using.
#DEV.properties
HostName.xpath=/:application/:NVPairs/:NameValuePair[:name='Connections/HTTP/HostName']/:value/text()
HostName.value=localhost
<project name="TestBuild" default="ReadPropertyFile" basedir=".">
<target name="init">
<property file="DEV.properties"/>
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpath="${xmltaskPath}"/>
<taskdef resource="net/sf/antcontrib/antlib.xml" classpath="${antcontribPath}"/>
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
</target>
<target name="ReadPropertyFile" description="update the configuration" depends="init">
<property file="DEV.properties" prefix="x"/>
<propertyselector property="propertyList" delimiter="," select="\0" match="([^\.]*)\.xpath" casesensitive="true" distinct="true"/>
<for list="${propertyList}" param="sequence">
<sequential>
<propertyregex property="destproperty" input="#{sequence}" regexp="([^\.]*)\." select="\1" />
<property name="tempname" value="${destproperty}.value" />
<var name="localprop" value="${tempname}"/>
<echo> #{sequence} </echo>
<echo> ${x.#{sequence}} </echo>
<echo>destproperty --> ${destproperty}</echo>
<echo>tempname --> ${tempname}</echo>
<echo> localprop --> ${localprop}</echo>
<echo>${x.${localprop}} </echo> <!--This is not working -->
</sequential>
</for>
</target>
It would be really helpful if you guys can throw some light.
Thanks,
Venkat
Would this work better ?
I think you got yourself confused with the "x." prefix.
<project name="TestBuild" default="ReadPropertyFile" basedir=".">
<target name="init">
<property file="DEV.properties"/>
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask" classpath="${xmltaskPath}"/>
<taskdef resource="net/sf/antcontrib/antlib.xml" classpath="${antcontribPath}"/>
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
</target>
<target name="ReadPropertyFile" description="update the configuration" depends="init">
<property file="DEV.properties" prefix="x"/>
<local name="propertyList"/>
<propertyselector property="propertyList" delimiter="," select="\1" match="x\.([^\.]*)\.xpath" casesensitive="true" distinct="true"/>
<for list="${propertyList}" param="sequence">
<sequential>
<echo> #{sequence} </echo>
<echo> #{sequence}.xpath = ${x.#{sequence}.xpath} </echo>
<echo> #{sequence}.value = ${x.#{sequence}.value} </echo>
</sequential>
</for>
</target>
</project>

JSPC task giving error

apache.jasper.JasperException [jasperc] org.apache.jasper.JasperException: Unrecognized option: -v9. Use -help for help. for a simple build.xml
<property name="message" value="Deploying the .jar file." />
<property name="src" location="source" />
<property name="output" location="bin" />
<target name="main" depends="init, compile">
<echo>
${message}
</echo>
</target>
<target name="init">
<mkdir dir="${output}" />
</target>
<target name="compile">
<jspc srcdir="${src}"
destdir="${output}"
package="org.antbook.jsp"
verbose="9">
<include name="**/*.jsp" />
</jspc>
</target>
in the lib of ant
I put
servlet-api-2.4.jar
jasper-compiler.jar
jasper-runtime.jar
I don t know what append
It looks like -v option has been removed from jasper compiler 5.5.
Using jasper compiler 4.1 worked for me just fine.

Configuring a build file for a .NET solution to be used by Jenkins

I am trying to configure a .build file for a solution which will be built by Jenkins.
The solutions builds properly but Jenkins takes forever to create an installer for the same.
Here is the file currently in use:
<?xml version="1.0"?>
<project name="SampleApp" default="Install" xmlns="http://nant.sf.net/schemas/nant.xsd" >
<property name="basename" value="SampleApp"/>
<property name ="jenkinshome" value ="C:\Program Files\Jenkins\jobs\" />
<property name="OutputPath" value="bin/Release"/>
<property name="Release" value="true"/>
<property name="SampleApp.exe.config" value="Release" />
<property name="SolutionFileName" value = "SampleApp.sln" />
<property name="TargetFramework" value="${framework::get-target-framework()}" />
<target name="clean">
<delete>
<fileset>
<include name="${OutputPath}/${basename}.exe"/>
<include name="${OutputPath}/${basename}.pdb"/>
</fileset>
</delete>
</target>
<target name="build" >
<mkdir dir="${OutputPath}" />
<echo message= "${framework::get-assembly-directory(TargetFramework)}" />
<exec program="C:\WINDOWS\Microsoft.NET\Framework\v3.5\msbuild.exe"
commandline=''
workingdir="." />
</target>
<target name ="Install" depends="build">
<exec program="C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe">
<arg value ="C:\Program Files\Jenkins\jobs\Legacy Data Adapter\workspace\SampleApp\SampleApp.vdproj /rebuild Release">
</arg>
</exec>
</target>
</project>
I am new to Jenkins and the whole concept of CI.
Appreciate any help/suggestions.
got a lot of help from here Anatomy of a build file
Solved.
Regards.

ant build script issue

I have 2 ant build scripts named "build" and "tarne"
Build:
<?xml version="1.0" ?>
<project name="build" default="zip">
<property name="project.name" value="projectName"/>
<property name="version" value="default_version_value"/>
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="lib/build/ant-contrib.jar"/>
</classpath>
</taskdef>
<var name="version2" value="default_version_value"/>
<property name="tmp" value="tmp"/>
<property name="build.dir" location="${tmp}/component/${project.name}"/>
<property name="java.classes" location="${tmp}/component/${project.name}/classes"/>
<property name="weblayout.dir" location="${tmp}/weblayout/resources/${project.name}"/>
<path id="compile.classpath">
<fileset dir="lib" includes="**/*.jar" />
<fileset dir="lib/build" includes="*.zip" />
</path>
<target name="clean">
<delete dir="${tmp}" />
</target>
<target name="init" depends="clean">
<mkdir dir="${java.classes}" />
</target>
<target name="compile" depends="init">
<javac srcdir="src" source="1.5" target="1.5" encoding="utf-8" includes="**/*.java" destdir="${java.classes}" classpathref="compile.classpath" />
</target>
<target name="copy-resources" depends="compile">
//Lots of copying here
</target>
<target name="read.version" description="Parses the hda file for your version number">
<property file="${project.name}.hda" prefix="hda"/>
<propertyregex property="version" input="${hda.version}" regexp="\." replace="-" global="true" override="true"/>
<var name="version2" value="${version}"/>
<echo>${version}</echo>
<echo>${version2}</echo>
</target>
<target name="zip" depends="copy-resources, read.version" description="Package component">
<zip destfile="${project.name}-${version}.zip" basedir="${tmp}" />
<delete dir="${tmp}" />
</target>
</project>
Tarne:
<?xml version="1.0" ?>
<project default="tarne">
<include file="build.xml"/>
<property name="project.name" value="build.project.name"/>
<target name="tarne">
<antcall target="build.read.version" inheritRefs="true"></antcall>
<property name="version" value="build.version"/>
<property name="version2" value="build.version2"/>
<echo>${version}</echo>
<echo>${version2}</echo>
</target>
</project>
And the output I get when I run tarne.xml is:
Buildfile: tarne.xml
tarne:
build.read.version:
[echo] v1-0-1
[echo] v1-0-1
[echo] default_version_value
[echo] default_version_value
Where the first 2 lines (v1-0-1) are from inside the read.version target of build.xml and the next 2 lines are from tarne.xml. The general idea is that I should be able to access the version number in my tarne.xml build script.
Any ideas on what's going wrong?
Antcall does not support what you intend to do:
http://ant.apache.org/manual/Tasks/antcall.html :
The called target(s) are run in a new project; be aware that this means properties, references, etc. set by called targets will not persist back to the calling project.
you could try:
<target name="tarne" depends="build.read.version">
</target>
which would keep the new values.
Try
<property name="version" value="${build.version}"/>
<property name="version2" value="${build.version2}"/>

Resources