So the overall problem is this:
We have multiple property files
<property file="prop1"/>
<property file="prop2"/>
prop1 contains a property looking like:
mg.prop = ${mg2.prop}
prop2 contains mg2.prop
mg2.prop = Hello
If they were in the same file and I queried mg.prop, I'd get "Hello" back. Since they are in separate files this does not work (I need to load prop1 before prop2!)
I wrote a custom ant task that does the following:
String resolved = resolveProperty(propertyName);
getProject().setProperty(propertyName, resolved);
If I run
log("Resolved property value = " + getProject().getProperty(propertyName));
Right after, I get the correct value.
However in the Ant script, if I do
<echo message="${mg.prop}"/>
it shows me the original value.
Any thoughts on how to solve this?
From the Ant manual:
"Properties are immutable: whoever sets a property first freezes it for the rest of the build; they are most definitely not variables."
http://ant.apache.org/manual/Tasks/property.html
Depending on your situation, you might be able to accomplish what you want by loading prop1 twice, using loadproperties and a filter chain that the first time takes only lines not containing "{mg2.prop}", and the second time takes only lines that do contain it.
http://ant.apache.org/manual/Tasks/loadproperties.html
http://ant.apache.org/manual/Types/filterchain.html#linecontains
You can also use the var task of ant-contrib to reset values.
From the doc:
The next example shows a property being set, echoed, unset, then
reset:
<property name="x" value="6"/>
<echo>${x}</echo> <!-- will print 6 -->
<var name="x" unset="true"/>
<property name="x" value="12"/>
<echo>${x}</echo> <!-- will print 12 -->
Here's how I ended up resolving this - I turfed the custom ant task.
I ended up concatenating all the properties files into one, in the reverse order of precedence.
So if I wanted properties from 3.properties to override those in 2.properties and 1.properties, I did the following:
<concat destfile="resolved.properties">
<fileset file="1.properties" />
<fileset file="2.properties" />
<fileset file="3.properties" />
</concat>
<property file="resolved.properties"/>
Related
Net: 'junitreport' doesn't seem to provide a way to set the temporary folder that it uses while performing transformations, and the doesn't seem to be a way to update 'java.io.tmpdir' for the reporting task. In this case, setting 'user.home' or 'java.io.tmpdir' from the command line while invoking ant is not a solution which can be used.
In detail: I'm using junit tasks within ant/mantis, with this syntax:
<target name="unittest_report">
<sequential>
<echo>User Home [ user.home ] [ ${user.home} ]</echo>
<echo>Java Temp [ java.io.tmpdir ] [ ${java.io.tmpdir} ]</echo>
<junitreport todir="${dir.unittest.reports}/raw">
<fileset dir="${dir.unittest.reports}/raw">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${dir.unittest.reports}"/>
</junitreport>
</sequential>
</mantis:modifyenvironment>
This seems to be a very standard usage, with the addition of the "echo" statements to show system property values which are active when the task is executed.
The task runs fine, except that the processing writes temporary output relative to the java temporary directory (java.io.tmpdir):
[15:55:03+0000/tester/unittest_report/echo] User Home [ user.home ] [ C:\Users\MyUser ]
[15:55:03+0000/tester/unittest_report/echo] Java Temp [ java.io.tmpdir ] [C:\Users\MyUser\AppData\Local\Temp\ ]
[15:55:04+0000/tester/unittest_report/junitreport] Processing c:\work\tester\build\reports\unittest\raw\TESTS-TestSuites.xml to
C:\Users\MyUser\AppData\Local\Temp\null591799606
The key values are the "java.io.tmpdir" value and the write to "C:\Users\MyUser\AppData\Local\Temp\null591799606".
I've tried to temporary update the temporary directory using "sysproperty", for example:
<mantis:modifyenvironment>
<sysproperty key="java.io.tmpdir" value="${dir.build}/junit.report.tmp"/>
<sequential>
<echo>User Home [ user.home ] [ ${user.home} ]</echo>
<echo>Java Temp [ java.io.tmpdir ] [ ${java.io.tmpdir} ]</echo>
<junitreport todir="${dir.unittest.reports}/raw">
<fileset dir="${dir.unittest.reports}/raw">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${dir.unittest.reports}"/>
</junitreport>
</sequential>
</mantis:modifyenvironment>
But, that has no effect.
Then:
1) Is this correct expectation of "sysproperty" to update the system property for the "junitreport" task? For example, the effect of "sysproperty" might be limited to nested "java" sequential task invocations, in which case the expectation that the update be visible to "junitreport" would be incorrect.
2) Or, is there a limitation for "java.io.tmpdir" in particular which prevents this from working?
3) Or, is there a bug in "sysproperty"?
For this problem, I'm prevented from updating "java.io.tmpdir" (or "user.home") from the command line. Those values cannot be modified in a wider scope than the "junitreport" task invocations.
Finally: Not sure if it fits under this same set of questions:
4) Where is the documentation for "mantis:modifyenvironment"? I find this in my local "mantis.jar", but I am finding no documentation for it in the mantis manual / documentation.
5) That "junitreport" modifies the file system through "java.io.tmpdir" with no possible override seems to be very bad practice.
About questions 1-3 about sysproperty I can tell you that the only place it may be used is before executing a new JVM: I.E. within java and similar tasks, like junit. (You can read it in the respective documentations).
My Ant junit task is currently set to output minimal information. On the console, for each test class, it prints the class name, how many tests were run, failed, and errored, along with the elapsed time for each test. If the test failed, there's an additional line saying the test failed.
I'd like to get additional detail on the console, but ONLY if a test fails. If I follow advice like this, by adding an additional plain formatter with usefile=false, then I get additional redundant detail for ALL tests, even if all tests pass. This includes printing each test method executed, and a redundant line for each test class.
Is it possible to get this?
I believe I've implemented a reasonable solution.
The key points of the solution are:
* Executing a task after the unit tests are complete, which only runs if "test.failed"
* Use the "xmltask" task library to parse the test results files and emit concise results
I wrote the following target to do this:
<target name="show-unit-test-failures" if="test.failed">
<taskdef if:set="xmltask.present" name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>
<echo if:set="xmltask.present" message="Unit test failure report details:"/>
<xmltask if:set="xmltask.present">
<fileset dir="gen/reports/artifacts/junit">
<include name="TEST-*Test.xml"/>
</fileset>
<call path="//testcase[./error]">
<parThatam name="className" path="#classname"/>
<param name="methodName" path="#name"/>
<param name="errorText" path="./error/text()"/>
<actions>
<echo>---------------------------------</echo>
<echo>#{className}.#{methodName}:</echo>
<echo>#{errorText}</echo>
</actions>
</call>
<call path="//testcase[./failure]">
<param name="className" path="#classname"/>
<param name="methodName" path="#name"/>
<param name="errorText" path="./failure/text()"/>
<actions>
<echo>---------------------------------</echo>
<echo>#{className}.#{methodName}:</echo>
<echo>#{errorText}</echo>
</actions>
</call>
</xmltask>
</target>
Note that I also let it "fail gracefully" if the "xmltask.jar" file isn't available. The ability to reference namespaces in tasks is a new feature in Ant 1.9.1 and newer.
I call this target at the end of my "run-unit-test" target.
So, I have this setup in ant:
<target name="context">
<echo>docbase: ${docbase.dir}</echo>
<antcall target="_context.docbase"/>
<antcall target="_context.nodocbase"/>
</target>
<target name="_context.docbase" if="${docbase.dir}">
<echo>Context with docbase</echo>
<context.md docbase="..."/>
</target>
<target name="_context.nodocbase" unless="${docbase.dir}">
<echo>Context without docbase</echo>
<context.md/>
</target>
And when docbase.dir is set to true or false things work as you'd expect. But when docbase.dir is set to an actual value (e.g., /tmp/docbase) for some reason it hits the _context.nodocbase target instead of the expected _context.docbase target.
Reading the docs it implies that if the expanded property isn't one of the "truthy" for "falsey" constants then it will just interpret it as before, but this isn't what I'm seeing.
What am I doing wrong? Should I use a different approach?
Note: I'm using Ant 1.8.2 (and conditional parameter expansion started in 1.8.0).
As mentioned here: http://ant.apache.org/manual/properties.html
As of Ant 1.8.0, you may instead use property expansion; a value of true (or on or yes) will enable the item, while false (or off or no) will disable it. Other values are still assumed to be property names and so the item is enabled only if the named property is defined.
This is some really subtle information here. So what's going on in your code is ${docbase.dir} is expanded to "/tmp/docbase". That's not true/on/yes/false/off/no so Ant then looks for a property named "/tmp/docbase". It can't find it, so it runs the unless.
Here's an example that I hope will clarify things. This will run only the if target:
<project name="MyProject" default="context" basedir=".">
<target name="context">
<echo>docbase: ${docbase.dir}</echo>
<property name="/tmp/docbase" value="i exist!"/>
<antcall target="_context.docbase"/>
<antcall target="_context.nodocbase"/>
</target>
<target name="_context.docbase" if="${docbase.dir}">
<echo>Context with docbase</echo>
</target>
<target name="_context.nodocbase" unless="${docbase.dir}">
<echo>Context without docbase</echo>
</target>
</project>
The important thing here is this line:
<property name="/tmp/docbase" value="i exist!"/>
Because that property is defined, it will run the if and not the unless.
I have a java project and an ANT script to build and then distribute the project to other projects with a simple copy command.
I would like to only name the place where to copy to files to once in the header of the ant script, and not have an explicit copy-task for every project that is dependent on this project.
I can't find anything like arrays in ANT, so what would be the cleanest way of distributing something to multiple directories?
According to what I commented under Martin's answer, I'd like to post my version of solution as another choice. And I am using property names from Martin's answer to make it clear.
<target name="deploy" >
<property name="dest.dirs" value="/dir/one,/dir/two,/dir/thr/ee" />
<for list="${dest.dirs}" param="dest.dir" parallel="true" delimiter="," >
<sequential>
<copy todir="#{dest.dir}" >
<fileset dir="${srd.dir}" />
</copy>
</sequential>
</for>
</target>
Please note that "for" is an Ant-Contrib task, and it's using Macrodef in the back so you should use #{} to refer to "dest.dir"; the "dest.dirs" will be splited into a list (maybe String[]) by delimiter. Here we use comma to split it (and the default value to delimiter is comma). I also added "parallel" to make it copy files to all the "dest.dirs" at same time, however, if the project to copy is large, you should delete "parallel".
Please check http://ant-contrib.sourceforge.net/tasks/tasks/for.html
and http://ant-contrib.sourceforge.net/tasks/tasks/foreach.html for more information.
I don't believe you have many viable options: the copy task accepts only a single directory.
Create your own copy task that takes a list of directories.
Exec a script/program that does the copying.
Have the subprojects do a pull.
I'm really hesitant about having a project push to other projects, because that makes the assumption that those projects will work with the newly-pushed code. IMO the "sub-"projects should be making the decision if they want the new version or not.
To me this sounds more like a dependency management issue, better handled with Ivy/Maven/Gradle (or other Maven-alike).
All that said, it sounds like you'd want to do option 1, create a custom Ant task that accepts a list of destination directories; it might be pretty easy to just extend the existing copy task to get all its functionality--just add a "todirs" property.
You might consider using a scriptmapper in your copy task with enablemultiplemappings true.
First, list the target directories in a property and create a filelist from it. (You could use a dirset, but the API for filelist is simpler.) Then run the copy, with the scriptmapper setting up the multiple destinations.
<property name="dest.dirs" value="/dir/one,/dir/two,/dir/thr/ee" />
<filelist id="dests" dir="/" files="${dest.dirs}" />
<copy todir="/" enablemultiplemappings="yes">
<fileset dir="${srd.dir}" />
<scriptmapper language="javascript">
<![CDATA[
// Obtain a reference to the filelist
var filelist = project.getReference( "dests" );
var dests = filelist.getFiles( project );
for ( var i = 0; i < dests.length; i++ )
{
self.addMappedName( dests[i] + "/" + source );
}
]]>
</scriptmapper>
</copy>
I'm using the propertyfile task shown below in my build script:
<target name="build-brand" depends="-init" description="Adds version information to branding files.">
<propertyfile file="${basedir}/branding/core/core.jar/org/netbeans/core/startup/Bundle.properties">
<entry key="currentVersion" value="${app.windowtitle} ${app.version}" />
</propertyfile>
</target>
The task works as expected, except that each time I build the project, the date comment line of the Bundle.properties file is updated with the current time stamp. This occurs even if the app.version variable does not change and results in an un-necessary commit to version control consisting solely of the following diff:
--- Base (BASE)
+++ Locally Modified (Based On LOCAL)
## -1,4 +1,4 ##
-#Thu, 22 Jul 2010 15:05:24 -0400
+#Tue, 10 Aug 2010 13:38:27 -0400
How can I prevent addition of or remove this date comment from the .properties file? I considered a delete operation in propertyfile nested entry element, but a key value is required.
This isn't a great solution, but how about removing the comment all together?
<target name="build-brand" depends="-init" description="Adds version information to branding files.">
<propertyfile file="${basedir}/branding/core/core.jar/org/netbeans/core/startup/Bundle.properties">
<entry key="currentVersion" value="${app.windowtitle} ${app.version}" />
</propertyfile>
<replaceregexp file="${basedir}/branding/core/core.jar/org/netbeans/core/startup/Bundle.properties" match="^#.*\n" replace=""/>
</target>
If you need to put a single property in a file just use echo:
<echo output="somefiles.properties">lastmodified=${lastmodified}</echo>
Try: <propertyfile file="..." comment="">
Edit: Which probably won't work :(. It looks like the culprit is actually Properties.store(OutputStream, String):
Next, a comment line is always
written, consisting of an ASCII #
character, the current date and time
(as if produced by the toString method
of Date for the current time), and a
line separator as generated by the
Writer.