I have a properties file:
custom.properties
the content of this properties file is:
id=sf2j2345kkklljhlaasfsdfafsf543
name = SOME_NAME
I'd like to change the value of the id to "aaa", that's change the content of the file to:
id=aaa
name = SOME_NAME
I tried:
<target name="change-id">
<replaceregexp file="custom.properties" match="^id=.*" replace="id=aaa" encoding="UTF-8" />
</target>
I run command ant change-id
The build is successful, But it does not work, the content of my properties file is unchanged. Why?
For editing propertyfiles use the Propertyfile Task
f.e. :
<propertyfile file="some.properties">
<entry key="id" value="aaa"/>
</propertyfile>
Related
I have following ant configuration:
<project name="pcebuild" basedir="." default="updateDatabase" xmlns:liquibase="antlib:liquibase.integration.ant" >
<taskdef resource="liquibase/integration/ant/antlib.xml" uri="antlib:liquibase.integration.ant">
<classpath path="c:\Users\artur.skrzydlo\Documents\liquibase-3.3.2-bin\liquibase.jar"/>
</taskdef>
<property name="liquiChangeLogFile" value="${basedir}/liquibase/db.changelog-master.xml"/>
<property name="db.driver" value="oracle.jdbc.OracleDriver"/>
<property name="db.url" value="jdbc:oracle:thin:#websph:1521:XE"/>
<target name="updateDatabase" description="Updates database with new changes using Liquibase">
<liquibase:updateDatabase changeLogFile="${liquiChangeLogFile}" >
<liquibase:database driver="${db.driver}" url="${db.url}" user="${db.user}" password="${db.pasword}"/>
</liquibase:updateDatabase>
</target>
</project>
After running this task I get an error :
Class not found: oracle.jdbc.OracleDriver
According to documentation :
driver The fully qualified class name of the JDBC driver.
I suppose that this error may rise because there is no place where I place classpath to my ojdbc.jar file. I am able to run this update command from command line, however there I can specify "classpath" argument which point to my ojdbc.jar file. And I don's see any place in this ant task definition where could i place it such a path. How can I do this ? What am I doing wrong ?
In your <liquibase:updateDatabase> tag you can have a classpathref attribute. So I have something like this:
<path id="driver.classpath">
<filelist files="${classpath}" />
</path>
...
<liquibase:updateDatabase
databaseref="main-schema"
changelogfile="${changeLogFile}"
classpathref="driver.classpath"
logLevel="debug"
>
...
And ${classpath} is an Ant property, set in a properties file:
classpath: /Users/me/place/lib/classes12.jar
I am using Input tasks to collect specific property values and I want to concatenate those into one property value that references my properties file.
I can generate the format of the property but at runtime it is treated as a string and not a property reference.
Example properties file:
# build.properties
# Some Server Credentials
west.1.server = TaPwxOsa
west.2.server = DQmCIizF
east.1.server = ZCTgqq9A
Example build file:
<property file="build.properties"/>
<target name="login">
<input message="Enter Location:" addproperty="loc" />
<input message="Enter Sandbox:" addproperty="box" />
<property name="token" value="\$\{${loc}.${box}.server}" />
<echo message="${token}"/>
</target>
When I call login and provide "west" and "1" for the input values, echo will print ${west.1.server} but it will not retrieve the property value from the properties file.
If I hardcode the property value in the message:
<echo message="${west.1.server}"/>
then Ant will dutifully retrieve the string from the properties file.
How can I get Ant to accept the dynamically generated property value and treat it as a property to be retrieved from the properties file?
The props antlib provides support for this but as far as I know there's no binary release available yet so you have to build it from source.
An alternative approach would be to use a macrodef:
<macrodef name="setToken">
<attribute name="loc"/>
<attribute name="box"/>
<sequential>
<property name="token" value="${#{loc}.#{box}.server}" />
</sequential>
</macrodef>
<setToken loc="${loc}" box="${box}"/>
Additional example using the Props antlib.
Needs Ant >= 1.8.0 (works fine with latest Ant version 1.9.4)
and Props antlib binaries.
The current build.xml in official Props antlib GIT Repository (or here) doesn't work out of the box :
BUILD FAILED
Target "compile" does not exist in the project "props".
Get the sources of props antlib and unpack in filesystem.
Get the sources of antlibs-common and unpack contents to ../ant-antlibs-props-master/common
Run ant antlib for building the jar :
[jar] Building jar: c:\area51\ant-antlibs-props-master\build\lib\ant-props-1.0Alpha.jar
Otherwise get the binaries from MVNRepository or here
The examples in ../antunit are quite helpful.
For nested properties look in nested-test.xml
Put the ant-props.jar on ant classpath.
<project xmlns:props="antlib:org.apache.ant.props">
<!-- Activate Props antlib -->
<propertyhelper>
<props:nested/>
</propertyhelper>
<property file="build.properties"/>
<input message="Enter Location:" addproperty="loc" />
<input message="Enter Sandbox:" addproperty="box" />
<property name="token" value="${${loc}.${box}.server}"/>
<echo message="${token}"/>
</project>
output :
Buildfile: c:\area51\ant\tryme.xml
[input] Enter Location:
west
[input] Enter Sandbox:
1
[echo] TaPwxOsa
BUILD SUCCESSFUL
Total time: 4 seconds
Solution is :
Consider problem is this, where you want to achieve this :
<property name="prop" value="${${anotherprop}}"/> (double expanding the property)?
You can use javascript:
<script language="javascript">
propname = project.getProperty("anotherprop");
project.setNewProperty("prop", propname);
</script>
I gave it a try and this is working for me.
I have a local.properties file, which contains only one value:
student.id=100
I created the following Ant script to increase the value of student.id by 1 :
<target name="increase-id">
<!-- student.id is increased by 1, no problem -->
<propertyfile file="local.properties">
<entry key="student.id" type="int" operation="+" value="1" />
</propertyfile>
<!--The following echo always show me "Student ID: 1, why?"-->
<echo message="Student ID: ${student.id}"/>
</target>
Every time after I run command ant increase-id , the value of student.id in local.properties file is increased by 1. No problem here. But, the <echo> message always show me Student ID: 1
Why?
This one works for me:
<project name="increase.test" default="increase-id">
<target name="increase-id">
<!-- documentation says that this task is for editing property files -->
<propertyfile file="local.properties">
<entry key="student.id" type="int" operation="+" value="1" />
</propertyfile>
<!-- so you should load this property after edit -->
<property file="local.properties" />
<echo message="Student ID: ${student.id}"/>
</target>
</project>
By the way don't use relative paths. Instead of this always use absolute paths. If you want to load property file which is in the same directory as build.xml you can use <dirname/> task.
<dirname property="build.dir" file="${ant.file.<projectName>}"/>,
where:
${ant.file} is builtin property which indicates to currently runned build.xml
<projectName> is just project name
Try to always create dirname's using this pattern ${ant.file.<projectName> because if you will use only ${ant.file} it will indicates to main build. for example if you have build which runs other build and in the other build you will use ${ant.file} it will indicates to the main build. If it is not clear just read Apache ant documentation and try to create simple build.
I would ask about property ant.After some action I got property
<property name="prop" value="test"/>
in file build.properties .I have the property test
test=example
I want to get value from build.properties
but when I use
<echo>${${prop}}</echo>
I got error
Could somebody help me?How I can solve this problem?It's possible in general such way use property?
This should work assuming you have a build.properties file containing:
test=example
The build file might look like this:
<project basedir="." default="default">
<property file="build.properties"/>
<property name="prop" value="test"/>
<target name="default">
<echo>${test}</echo>
<echo>${prop}</echo>
</target>
</project>
i have an ant script as shown below:
<project name="nightly_build" default="main" basedir="checkout">
<target name="init">
<exec executable="C:/Work/Searchversion.exe"/>
<property file="initial.properties"/>
<property file="C:/Work/lastestbuild.properties"/>
<tstamp>
<format property="suffix" pattern="yyyyMMddHHmmss"/>
</tstamp>
</target>
<target name="main" depends="init">
<exec executable="C:/Program Files/True Blue Software/SnapshotCM/wco.exe">
<arg line='-h sinsscm01.sin.ds.net -S"/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_${Version_Number}" /'/>
</exec>
</target>
</project>
i created the above script to replicate a command: wco -h sinsscm01.sin.ds.net -S"/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_6.70.102.014" /
and 6.70.102.014 is found inside latestbuild.properties file in the form of:
Version_Number = 6.70.102.014
and this latestbuild.properties file is obtained when i execute C:/Work/Searchversion.exe
but when i execute this ant script using cruisecontrol, in my log file,
[Thread-24] INFO ScriptRunner - [exec] Cannot open snapshot 'sinsscm01.sin.ds.jdsu.net:/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_${Version_Number}': No such snapshot
where ${Version_Number} should have been 6.70.102.014
How do i tackle this issue?
EDIT 1:
after trial and error and substituting with a built in property ${ant.version}, i realise that my property file could be loaded in correctly over here. can anyone point out my mistake? i dont see anything wrong though
EDIT 2:
Just additional infomation... This is actually a delegate ant script for cruisecontrol(used to perform nightly build). Here is my config.xml file for per minute build:
<cruisecontrol>
<project name="dms" buildafterfailed="true">
<listeners>
<currentbuildstatuslistener file = "logs/dms/status.txt"/>
</listeners>
<bootstrappers>
</bootstrappers>
<modificationset quietperiod="60">
<alwaysbuild/>
</modificationset>
<schedule interval="60">
<ant buildfile="nightly_build.xml" target="main"/>
</schedule>
<log dir="logs/dms">
<merge dir="checkout/dms/build/test-results" />
</log>
<publishers>
</publishers>
</project>
</cruisecontrol>
should properties file be loaded in config.xml?
Try breaking your arguments to wco.exe into separate child elements like this:
<exec executable="C:/Program Files/True Blue Software/SnapshotCM/wco.exe">
<arg value="-h" />
<arg value="sinsscm01.sin.ds.net" />
<arg value="-S" />
<arg value="/mobile/6.70_Extensions/6.70.102/ANT_SASE_RELEASE_${Version_Number}" />
<arg value="/" />
</exec>
I think ant isn't expanding ${Version_Number} because it is inside ' "..." ' in the version you posted.
As mentioned in the docs for <exec> you should avoid use of the <arg line=...> form.
You could add assertions in your init target that the required properties file exists and that the property is defined. For example:
<property name="version.file" value="C:/Work/lastestbuild.properties"/>
<available file="${version.file}" property="version.file.available"/>
<fail unless="version.file.available" message="file [${version.file}] is not available"/>
<property file="${version.file}"/>
<fail unless="version" message="property [version] is not defined"/>
<echo message="version: ${version}"/>
I think that will help you spot that the file does not exist.
I took a look at your other question about this script you're putting together. In the code which writes the version number to file, you use filename latestbuild.properties:
TextWriter latest = new StreamWriter("C:\\Work\\latestbuild.properties");
In your Ant script, you are loading a different filename lastestbuild.properties.
Unless you've fixed it since then, that will be your problem. (If you modified your external script to take the filename as a parameter, and defined the filename once as an Ant property - as in my sample above - it would help you avoid this kind of problem.)
Regarding your discovery that you need to wait for your external script before continuing in Ant, take a look at the Sleep task.