Is it possible to use $XXX_HOME style variables in a build.properties file?
I set path of variable in build.properties like this
ant.home=/opt/java/myanthome
I have a $ANT_HOME environment variable with the correct path. Can I use this environment variable to set my ant.home in build.properties? I tried to add this in build.xml
<property environment="env"/>
<property file="build.properties" />
and in the build.properties file:
ant.home=${env.ANT_HOME}
but this does not work,it says:
${env.ANT_HOME} does not exist
It is possible but there is no sense in doing it.
You can add a filter that pre-processes your property file. Consider the following property file:
ant.home=#ANT_HOME#
Then, you can filter it and load the file into properties, like this:
<filter token="ANT_HOME" value="${env.ANT_HOME}"/>
<copy file="build.properties" tofile="build.properties.filtered" filtering="true" />
<property file="build.properties.filtered" />
The filter task, in addition with filtering=true, will replace the #ANT_HOME# token by the value of ${env.ANT_HOME}.
But after having done all this, the question is, why filter the property file to begin with? You can just remove the ant.home property from your file and directly use ${env.ANT_HOME} where necessary.
Related
I have an Ant xml file. In this file I want to set two properties based on the current working directory. The current working directory is always of the form /some/base/dir/SRC/sub/dir.
The first property must be set to the current working directory. The second property must be set to the part of the current working directory up to /SRC.
I can set the first property without any issue using the PWD environment variable , but I cannot figure out the second.
<property name="my.dir" value="${env.PWD}" />
<property name="src.dir" value="{what do I put here?}" />
I've heard this can be done with bash-style string manipulation (e.g. ${PWD%*/SRC}/SRC) using StringOps, but I cannot find any good examples.
One approach is to use the <pathconvert> task, perhaps like this:
<property name="my.dir" value="/some/base/dir/SRC/sub/dir"/>
<pathconvert property="src.dir">
<path path="${my.dir}"/>
<mapper type="regexp" from="^(.*)/SRC" to="\1" />
</pathconvert>
<echo message="src.dir=${src.dir}" />
Which gives:
[echo] src.dir=/some/base/dir
There are other mappers available which might work better for you than regexp.
I need to be able to read an ehcache configuration file ( ehcache.xml) from outside the classpath to be able to have differents files by environment (to be able for example to change the multicast adress for cache sharing).
Before I was simply using an XML defined bean :
<bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="#{
#PreferenceService.getEhcacheFileName() }" />
</bean>
PreferenceService.getEhcacheFileName() send back a path in a properties file.
if the propertie is filled with a classpath path (classpath:ehcache.xml), the application work properly.
But if I want to use an absolute path (/home/foo/ehcache.xml) the resource is not found.
Is it possible to use an absolute path ? And if yes what properties do I need to use ?
configLocation can contain an absolute path without any trouble.
I just forgot to add the file: in front of the path in my properties file.
So my path is now : "file:/foo/foo/ehcache.xml"
In Ant, can I create a target that contains something like a variable that represents a path?
For example, something like the following pseudo target:
<target name="initPath">
Path = "${basedir}/../../myProject/Project/"
</target>
Where Path is my variable and is initializated to specific value.
How can I do this?
Ant build scripts are written in XML. To create a property has to be in XML style, so instead of this:
some_prop="some value"
It is this:
<property name="some_prop" value="some value"/>
Properties can contain periods, and I recommend using them as name separators:
<property name="some.prop" value="some value"/>
How do you declare a constant? Here:
<property name="some.prop" value="some value"/>
That's because once a property is set, it cannot be changed.
This way, you can do something like this:
<property file="${basedir}/build.properties"/>
<property name="some.prop" value="some value"/>
Let's say that the build.properties file contains this line:
some.prop="Some other value"/>
Now, when you run your Ant build file, the value of some.prop will be "Some other value", and the <property name="some.prop" value="some value"/> won't change it. I could even do this:
$ ant -Dsome.prop="A completely different value"
And this value of the some.prop property will override what I have in my build.properties file and what I have in my Ant build file.
This is a very nice feature. It allows me to set a default value that developers can override:
<property name="copy.verbose" value="false"/>
...
<copy todir="${copy.to.dir}"
verbose="${copy.verbose}">
<fileset dir="${copy.from.dir}"/>
</copy>
By default, when my copy task runs, it runs in non-verbose mode which is what I want. However, let's say I am having a few problems with my build, and I want to see exactly what is being copied, I could do this:
$ ant -Dcopy.verbose=true
And, now my copy tasks will show me all the files being copied.
A path is a way to declare something like $CLASSPATH or $PATH in the command line. You can predeclare a path with an id, and then use it later:
<javac destdir="${main.destdir}"
srcdir="${main.srcdir}">
<classpath>
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</classpath>
</javac>
Here I'm adding a classpath. This is using <fileset/> to create a classpath based upon all of the jars in my ${lib.dir} directory.
I can also do this:
<path id="main.classpath">
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</path>
<javac destdir="${main.destdir}"
srcdir="${main.srcdir}"
classpathref="main.classpath"/>
Here, I predeclare my main.classpath and then use that later in my <javac> task.
You should read up on Ant in the on line Ant Manual. There is a semi-decent introduction in the manual which might help clarify a few issues for you.
Here is how you can define a property in Ant script.
Unfortunately it is not a variable, since they are immutable. You can set a value to it, but you cannot change it during your script execution.
Here you can see an example of assigning value to a property.
Update.
You can use path task. For example:
<path id="combinedPath">
<path path="${toString:oldPath}"/>
<path path="my.jar"/>
</path>
<path id="reanamePath">
<path path="${toString:oldPath}"/>
</path>
If you change path in one target you can definitely access it in another one.
Ant has classpath-like support built-in.
In general you wouldn't use a property for this functionality.
Please consider reading the Ant documentation; this is Ant 101 and it'll be quicker in the long run if you spend some time with the docs and figuring out the Ant way of doing things.
<property file="Build.properties"/>
this is valid if build.properties file is in root of project.now if the Build.properties file is in src folder and build.xml is in root(main project root).then what will be the syntax to access that build.properties file.
It can be done by giving in two ways
give the loction in classpath.
<property file="src/build.properties" />
Both of them worked for me.
You can define property at starting of your ant script.
And
use ${proj_home}*.properties to access your property file.
Hope it helps.
Other answers are valid. Note that you can pass a property file as ant command line
ant -propertyfile src/build.properties
I'm using my context.xml file to set init parameters for my java application, for example:
<Parameter
name="Environment"
description="The environment in which this code is running (e.g. Production, Staging, Development)."
value="Production"/>
I would like to be able to create a parameter who's value attribute is loading from a file. Is there anyway to do this? Should I be using a < Resource > element instead? If so, how do I setup a resource to load the contents of a file? I've tried Google, but I my not understand the context.xml file well enough to know what to look for. Any help is much appreciated!
In application-context.xml add this element
<context:property-placeholder ignore-unresolvable="true" location="classpath*:application.properties"/>
In the file application.properties you can define parameters like this
userName=root
password=123
And then you can use the parameters by this way
<property name="username" value="${userName}"/>
<property name="password" value="${password}"/>