How can i edit path of a jar in ant - ant

I would like to replace path of an existing jar based on OS .
some thing like below:
in windows : C:/apps/workspace/libs/rpm.jar
in unix : /user-id/projectname/libs/rpm.jar
Is there a way to remove C:/apps/workspace/libs from C:/apps/workspace/libs/rpm.jar .
editing my Question to :
Thanks Liv, But i have lot of libraries like this. right now i am maintaning a text file called "build.start.properties" with all libraries some ting like this
/gwt/X/2.1.0/gwt-servlet.jar
/gwt/X/2.1.0/gwt-user.jar
/gwt/X/2.1.0/gwt-dev.jar
/gwt/X/2.1.0/gwt-soyc-vis.jar
/log4j/X/1.2.15/log4j-1.2.15.jar
/GWT_LOG/X/3.0.3/gwt-log-3.0.3.jar
/GWT_MATH/X/2.1/gwt-math-2.1.jar
/GWT_MATH/X/2.1/gwt-math-server-2.1.jar
/GWT_Commons_Logging/X/0.3/GWT-commons-logging/gwt-commons-logging-0.3.jar
/GWT_Commons_Logging/X/0.3/GWT-commons-logging/gwt-commons-logging-service-0.3.jar
And loading them to classptah using below target
<loadfile property="jars.list.property" srcfile="mybuild/build.start.properties">
<filterchain>
<expandproperties />
<striplinecomments>
<comment value="#" />
</striplinecomments>
<tokenfilter>
<ignoreblank />
</tokenfilter>
<prefixlines prefix="," />
<striplinebreaks />
</filterchain>
</loadfile>
<filelist id="build.libs" dir="" files="${jars.list.property}" />
<pathconvert targetos="unix" property="build_unix.libs" refid="build.libs">
<map from="C:" to="${unix.xenv}" />
<map from="" to="${unix.xenv}" />
</pathconvert>
<pathconvert targetos="windows" property="build_windows.libs" refid="build.libs">
<map from="C:" to="${windows.xenv}" />
<map from="" to="${windows.xenv}" />
</pathconvert>
<path id="build.classpath.id">
<pathelement path="${build_windows.libs}" />
<pathelement path="${build_unix.libs}" />
</path>
<echo message="Build Libraries classpath: ${toString:build.classpath.id}" />
</target>
from the above target build.classpath.id looks like
/gwt/X/2.1.0/gwt-servlet.jar:/gwt/X/2.1.0/gwt-user.jar:/gwt/X/2.1.0/gwt-dev.jar:/gwt/X/2.1.0/gwt-soyc-vis.jar:/log4j/X/1.2.15/log4j-1.2.15.jar:/GWT_LOG/X/3.0.3/gwt-log-3.0.3.jar:GWT_MATH/X/2.1/gwt-math-2.1.jar:/GWT_MATH/X/2.1/gwt-math-server-2.1.jar:/GWT_Commons_Logging/X/0.3/GWT-commons-logging/gwt-commons-logging-0.3.jar:/GWT_Commons_Logging/X/0.3/GWT-commons-logging/gwt-commons-logging-service-0.3.jar
When i work on unix I have to pic only jar names from file "build.start.properties" and update path like this
/WebContent/WEB_INF/lib/gwt-servlet.jar:/WebContent/WEB_INF/lib/gwt-user.jar:/WebContent/WEB_INF/lib/gwt-dev.jar:/WebContent/WEB_INF/lib/gwt-soyc-vis.jar:/WebContent/WEB_INF/lib/log4j-1.2.15.jar:/WebContent/WEB_INF/lib/gwt-log-3.0.3.jar:/WebContent/WEB_INF/lib/gwt-math-2.1.jar:/WebContent/WEB_INF/lib/gwt-math-server-2.1.jar:/WebContent/WEB_INF/lib/gwt-commons-logging-0.3.jar:/WebContent/WEB_INF/lib/gwt-commons-logging-service-0.3.jar

Always use relative paths, that way you wont' be relying on library being at given location and on underlying OS.
Although this does not answer what you exactly asked for, but the suggestion will help you in the long run. Also, if possible, use Ivy + Ant (or Maven) to manage dependency.

you can use the ant task -- the folliwng is taken from the documentation (http://ant.apache.org/manual/Tasks/condition.html):
<condition property="isMacOsButNotMacOsX">
<and>
<os family="mac"/>
<not>
<os family="unix"/>
</not>
</and>
</condition>

Related

How to validate the parameter is uppercase in Jenkins using ANT

I have a Jenkins build and in that I am providing a build parameter with uppercase value (Build parameter should be uppercase always), by chance if a user is providing a lower case value, the build should gets failed.
Kindly provide inputs on how to check the provided Jenkins build parameter provided is uppercase or a lowercase using ANT. Thanks!
This can be accomplished using Ant's matches condition. A nested regexp is used in the below example to avoid repetition.
<target name="check-uppercase">
<property name="test1" value="lowercase" />
<property name="test2" value="MixedCase" />
<property name="test3" value="UPPERCASE" />
<regexp id="all.uppercase" pattern="^[A-Z]+$" />
<condition property="test1.isUppercase" value="true" else="false">
<matches string="${test1}">
<regexp refid="all.uppercase" />
</matches>
</condition>
<condition property="test2.isUppercase" value="true" else="false">
<matches string="${test2}">
<regexp refid="all.uppercase" />
</matches>
</condition>
<condition property="test3.isUppercase" value="true" else="false">
<matches string="${test3}">
<regexp refid="all.uppercase" />
</matches>
</condition>
<echo message="test1 uppercase? ${test1.isUppercase}" />
<echo message="test2 uppercase? ${test2.isUppercase}" />
<echo message="test3 uppercase? ${test3.isUppercase}" />
</target>

ant: condition command does not find pattern when pattern is a file path

I am using the following code with the idea of finding a file in a directory that is also part of a list in a file:
<loadfile property="ReportFileContent" srcFile="${ReportFile}"/>
<for param="file">
<path>
<fileset dir="${MainDir}" includes="**/**"/>
</path>
<sequential>
<basename file="#{file}" property="#{file}" />
<condition property="found-file${index2}">
<matches pattern="#{file}" string="${ReportFileContent}"/>
</condition>
<if>
<isset property="found-file${index2}"/>
<then>
<echo message=" Found file #{file}" level="warning" />
</then>
<else>
<echo message="Not Found file #{file}" level="warning" />
</else>
</if>
<math result="index2" operand1="${index2}" operation="+" operand2="1" datatype="int" />
</sequential>
</for>
The command is not working though as it is not finding the file that is available in ${ReportFileContent}.
The content of the ReportFileContent property is the following:
c:\___tools\test\file1.txt;2
c:\___tools\test\file2.txt;2
c:\___tools\test\file3.txt;2
Any idea why the condition is not working correctly?
Thanks
Tony

Concatenate using ANT

Im new to ANT:
Im trying to debug an ANT script which is concatenating followed by editing the same file recursively. when I try to run this, 5 out of 10 builds failed with the following error
java.io.FileNotFoundException:{PATH TO FILE}\file.xml (Access is denied)
I tried adding delay before and after each step.still, no luck
Any help here would be appreciated.
<target name="concatxml">
<sleep seconds="1" />
<echo message="Concatenating file..." />
<concat destfile="${location}/file.xml">
<filelist dir="." files="${loc1}/file1.xml,${loc2}/file2.xml" />
</concat>
<echo message="Concatenating completed" />
<sleep seconds="1" />
<echo message="doImportReq.xml is released for concatenating..." />
</target>
<target name="replacevalxml">
<sleep seconds="1" />
<echo message="Replace initiated" />
<replace file="${location}/file.xml" token="#DOMAIN#" value="${dev}" />
<echo message="...replace completed />
<sleep seconds="1" />
</target>
I noticed this:
files="${loc1}/file1.xml,{loc2}/file2.xml"
That should be ${loc2} and not {loc2}. I don't know if that's a problem or not...

ANT script to compile all (css) LESS files in a dir and subdirs with RHINO

I want do compile all *.less scripts in a specific folder and it subdirs with less-rhino-1.1.3.js.
There is an example on github for doing this for a specific file, which works perfect. But I want to do the same for a complete folder. I tried a lot, here is my last try.
It doesn't work, propertyregex seems not to be standard ANT, I don't want to use such things. I am not even sure if this code would work.
<project name="test" default="main" basedir="../../">
<property name="css.dir" location="public/css"/>
<property name="tool.less" location="bin/less/less-rhino-1.1.3.js"/>
<property name="tool.rhino" location="bin/tools/rhino/js.jar"/>
<macrodef name="lessjs">
<attribute name="input" />
<attribute name="output" />
<sequential>
<java jar="${tool.rhino}" fork="true" output="#{output}">
<arg path="${tool.less}"/>
<arg path="#{input}"/>
</java>
<echo>Lessjs: generated #{output}</echo>
</sequential>
</macrodef>
<target name="main">
<echo>compiling less css</echo>
<fileset dir="${css.dir}" id="myfile">
<filename name="**/*.less" />
</fileset>
<property name="lessfilename" refid="myfile"/>
<propertyregex property="cssfilename"
input="${lessfile}"
regexp="^(.*)\.less$"
replace="^\1\.css$"
casesensitive="true" />
<lessjs input="lessfile" output="cssfilename"/>
</target>
</project>
You could use the <fileset> to include all the less files need to be compiled. Later, you could use<mapper> to mark the corresponding detination css file.
<project name="test" default="main" basedir="../../">
<property name="css.dir" location="public/css"/>
<property name="tool.less" location="bin/less/less-rhino-1.1.3.js"/>
<property name="tool.rhino" location="bin/tools/rhino/js.jar"/>
<target name="less" description="Convert LESS to CSS then concatenate and Minify any stylesheets">
<echo message="Converting LESS to CSS..."/>
<!-- Clear the former compiled css files -->
<delete includeemptydirs="true">
<fileset dir="${css.dir}" includes="*.css, **/*.css" defaultexcludes="false"/>
</delete>
<apply dir="${css.dir}" executable="java" parallel="false" failonerror="true">
<!-- Give the input bundle of less files-->
<fileset dir="${css.dir}">
<include name="*.less"/>
</fileset>
<arg value="-jar" />
<arg path="${tool.rhino}" />
<arg path="${tool.less}" />
<srcfile/>
<!-- Output the compiled css file with corresponding name -->
<mapper type="glob" from="*.less" to="${css.dir}/*.css"/>
<targetfile/>
</apply>
</target>
</project>
I was able to piece together a working solution with the help of a couple of SO answers:
ANT script to compile all (css) LESS files in a dir and subdirs with RHINO
How to correctly execute lessc-rhino-1.6.3.js from command line
I had to download LESS 1.7.5 from GitHub and modify the Ant target to look like this. The -f argument and LESS JavaScript was key:
<property name="css.dir" value="WebContent/css"/>
<property name="less.dir" value="less"/>
<property name="tool.rhino.jar" value="test-lib/rhino-1.7R4.jar"/>
<property name="tool.rhino.lessc" value="test-lib/lessc-rhino-1.7.5.js"/>
<property name="tool.rhino.less" value="test-lib/less-rhino-1.7.5.js"/>
<target name="compile-less" description="compile css using LESS">
<apply dir="${css.dir}" executable="java" parallel="false" failonerror="true">
<fileset dir="${less.dir}">
<include name="styles.less"/>
</fileset>
<arg value="-jar"/>
<arg path="${tool.rhino.jar}"/>
<arg value="-f"/>
<arg path="${tool.rhino.less}"/>
<arg path="${tool.rhino.lessc}"/>
<srcfile/>
<mapper type="glob" from="*.less" to="${css.dir}/*.css"/>
<targetfile/>
</apply>
</target>
If anyone else is coming to this question recently, as I did, they may find that the less-rhino-1.1.3.js file given in the other answers does not work with the latest version of Rhino (which for me, as of now, is 1.7R4 from MDN). But the 1.4.0 version does, which can be obtained from Github here. So the relevant snippet from my build.xml, using these later versions, is shown. Note that I'm only compiling a single .less file to a single .css file, so no iteration or mappers are used (but obviously you can get those from the other answers). Other tweaks I made were to provide the output file as the final arg to less instead of capturing output from the Ant forked process, and to remove the dependency on ant-contrib stuff (not needed for the simple one-file case).
<property name="tool.rhino" value="build/lesscss/rhino1_7R4/js.jar" />
<property name="tool.less" value="build/lesscss/less-rhino-1.4.0.js" />
<property name="single-input-lesscss-file" value="/path/to/my/style.less" />
<property name="single-output-css-file" value="/output/my/style.css" />
<target name="compileLessCss" description="Compile the single less file to css">
<sequential>
<java jar="${tool.rhino}" fork="true">
<arg path="${tool.less}" />
<arg path="${single-input-lesscss-file}" />
<arg path="${single-output-css-file}" />
</java>
</sequential>
</target>
If maven is an option for you, you could try wro4j-maven-plugin or wro4j-runner (which is a command line utility).
Using one of these, all you have do is to create an resource model descriptor (wro.xml):
<groups xmlns="http://www.isdc.ro/wro">
<group name="g1">
<css>/path/to/*.less</css>
</group>
</groups>
The rest will be handled by the wro4j library. No need to carry about how rhino works or other details.
Disclaimer: I'm working on wro4j project
I had the same issue. I developed a solution using ant-contrib. It expects all of your .less files to be in one flat directory and to be moved to another flat directory. It will change the file extension to .css in the process.
<property name="tool.rhino" value="/rhino/js.jar" />
<property name="tool.less" value="src/js/less-rhino-1.1.3.js" />
<property name="tool.ant-contrib" value="/ant-contrib/ant-contrib-1.0b3-1.0b3.jar" />
<property name="less-files-dir" value="src/css/" />
<property name="css-files-dir" value="build/css/" />
<target name="compilecss" depends="setup-ant-contrib-taskdef, get-less-files-in-dir" description="DO THIS THING">
<for list="${less-files-to-convert}" param="file-name" trim="true" delimiter=",">
<sequential>
<propertyregex property="file-name-without-extension"
input="#{file-name}"
regexp="(.*)\..*"
select="\1"
override="yes" />
<java jar="${tool.rhino}" fork="true" output="${css-files-dir}${file-name-without-extension}.css">
<arg path="${tool.less}" />
<arg path="${less-files-dir}#{file-name}" />
</java>
<echo>Lessjs: generated ${css-files-dir}${file-name-without-extension}.css</echo>
</sequential>
</for>
</target>
<target name="check-for-ant-contrib">
<condition property="ant-contrib-available">
<and>
<available file="${tool.ant-contrib}"/>
</and>
</condition>
<fail unless="ant-contrib-available" message="Ant-Contrib is not available."/>
</target>
<target name="setup-ant-contrib-taskdef" depends="check-for-ant-contrib">
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<path location="${tool.ant-contrib}" />
</classpath>
</taskdef>
</target>
<target name="get-less-files-in-dir">
<var name="files-list" value="" />
<for param="file">
<path>
<fileset dir="${less-files-dir}" includes="**/*.less" />
</path>
<sequential>
<propertyregex property="file-name-and-relative-path"
input="#{file}"
regexp=".*\\(.*)"
select="\1"
override="yes" />
<echo>file name: ${file-name-and-relative-path}</echo>
<if>
<equals arg1="${files-list}" arg2="" />
<then>
<var name="files-list" value="${file-name-and-relative-path}" />
</then>
<else>
<var name="files-list" value="${files-list},${file-name-and-relative-path}" />
</else>
</if>
</sequential>
</for>
<property name="less-files-to-convert" value="${files-list}" />
<echo>files to convert: ${less-files-to-convert}</echo>
</target>
I was unable to get this to run using a JDK 1.6 since the javascript stuff has been incorporated to the JDK. The JDK does have a jrunscript executable in the distribution but when I try to run the less-rhino.js file it fails to recognize any readFile() function. Has anyone looked into that. Otherwise I may be giving the lesscss-engine a shot and enhancing it to understand filesets.

updating relative path of jar while working on different OS

I would like to replace path of an existing jar based on OS .
Right now i am maintaning a text file called "build.start.properties" with all libraries some ting like this
/gwt/X/2.1.0/gwt-servlet.jar
/gwt/X/2.1.0/gwt-user.jar
/gwt/X/2.1.0/gwt-dev.jar
/gwt/X/2.1.0/gwt-soyc-vis.jar
/log4j/X/1.2.15/log4j-1.2.15.jar
/GWT_LOG/X/3.0.3/gwt-log-3.0.3.jar
/GWT_MATH/X/2.1/gwt-math-2.1.jar
/GWT_MATH/X/2.1/gwt-math-server-2.1.jar
/GWT_Commons_Logging/X/0.3/GWT-commons-logging/gwt-commons-logging-0.3.jar
/GWT_Commons_Logging/X/0.3/GWT-commons-logging/gwt-commons-logging-service-0.3.jar
And loading them to classptah using below target
<loadfile property="jars.list.property" srcfile="mybuild/build.start.properties">
<filterchain>
<expandproperties />
<striplinecomments>
<comment value="#" />
</striplinecomments>
<tokenfilter>
<ignoreblank />
</tokenfilter>
<prefixlines prefix="," />
<striplinebreaks />
</filterchain>
</loadfile>
<filelist id="build.libs" dir="" files="${jars.list.property}" />
<pathconvert targetos="unix" property="build_unix.libs" refid="build.libs">
<map from="C:" to="${unix.xenv}" />
<map from="" to="${unix.xenv}" />
</pathconvert>
<pathconvert targetos="windows" property="build_windows.libs" refid="build.libs">
<map from="C:" to="${windows.xenv}" />
<map from="" to="${windows.xenv}" />
</pathconvert>
<path id="build.classpath.id">
<pathelement path="${build_windows.libs}" />
<pathelement path="${build_unix.libs}" />
</path>
<echo message="Build Libraries classpath: ${toString:build.classpath.id}" />
</target>
from the above target build.classpath.id looks like
/gwt/X/2.1.0/gwt-servlet.jar:/gwt/X/2.1.0/gwt-user.jar:/gwt/X/2.1.0/gwt-dev.jar:/gwt/X/2.1.0/gwt-soyc-vis.jar:/log4j/X/1.2.15/log4j-1.2.15.jar:/GWT_LOG/X/3.0.3/gwt-log-3.0.3.jar:GWT_MATH/X/2.1/gwt-math-2.1.jar:/GWT_MATH/X/2.1/gwt-math-server-2.1.jar:/GWT_Commons_Logging/X/0.3/GWT-commons-logging/gwt-commons-logging-0.3.jar:/GWT_Commons_Logging/X/0.3/GWT-commons-logging/gwt-commons-logging-service-0.3.jar
When i work on unix I have to pic only jar names from file "build.start.properties" and update path like this
/WebContent/WEB_INF/lib/gwt-servlet.jar:/WebContent/WEB_INF/lib/gwt-user.jar:/WebContent/WEB_INF/lib/gwt-dev.jar:/WebContent/WEB_INF/lib/gwt-soyc-vis.jar:/WebContent/WEB_INF/lib/log4j-1.2.15.jar:/WebContent/WEB_INF/lib/gwt-log-3.0.3.jar:/WebContent/WEB_INF/lib/gwt-math-2.1.jar:/WebContent/WEB_INF/lib/gwt-math-server-2.1.jar:/WebContent/WEB_INF/lib/gwt-commons-logging-0.3.jar:/WebContent/WEB_INF/lib/gwt-commons-logging-service-0.3.jar
Use the conditions
<condition property="isWindows">
<os family="windows" />
</condition>
<condition property="isUnix">
<os family="unix" />
</condition>
On the targets then u can use
<target name="path_if_unix" if="isUnix">
<map from="C:" to="${unix.xenv}" />
<map from="" to="${unix.xenv}" />
</target>

Resources