How to exclude base directory from phing/ant FileSet - ant

I have a phing build target that I want to run on each directory directly under my project base. I'm using a foreach task with a fileset to run the target on each directory. The problem I'm having is that the base directory is included, with a filename of "".
Here's an example of the directory structure:
One
Two
build.xml
Here's a simple build file to test:
<?xml version="1.0" encoding="UTF-8"?>
<project name="Test" default="test">
<target name="test">
<foreach param="filename" absparam="absfilename" target="echo-file">
<fileset dir="${project.basedir}">
<include name="*" />
<exclude name="*.*" />
</fileset>
</foreach>
</target>
<target name="echo-file">
<echo message="filename: ${filename}" />
<echo message="absfilename: ${absfilename}" />
</target>
</project>
And here's the results of the build (note the first entry with empty filename):
Buildfile: /Users/dmertl/test/build.xml
Test > test:
[foreach] Calling Buildfile '/Users/dmertl/test/build.xml' with target 'echo-file'
Test > echo-file:
[echo] filename:
[echo] absfilename: /Users/dmertl/test/
[foreach] Calling Buildfile '/Users/dmertl/test/build.xml' with target 'echo-file'
Test > echo-file:
[echo] filename: One
[echo] absfilename: /Users/dmertl/test/One
[foreach] Calling Buildfile '/Users/dmertl/test/build.xml' with target 'echo-file'
Test > echo-file:
[echo] filename: Two
[echo] absfilename: /Users/dmertl/test/Two

Not sure how I didn't figure this out the first time around, but here's the solution:
<foreach param="filename" absparam="absfilename" target="echo-file">
<fileset dir="${project.basedir}">
<include name="*" />
<exclude name="/" />
<exclude name="*.*" />
</fileset>
</foreach>
Excluding "/" in the fileset removes the root directory.

Related

Ant calling targets several times

I set up a simple Ant script to understand why my dependency targets are being called several times when I specified several targets to execute (in Eclipse):
<project name="test">
<macrodef name="mkjar" description="Build a jar from 'tgt'">
<attribute name="tgt" />
<sequential>
<echo message="mkjar #{tgt}" level="info" />
</sequential>
</macrodef>
<target name="Common">
<mkjar tgt="Common" />
</target>
<target name="Net" depends="Common">
<mkjar tgt="Net" />
</target>
<target name="DB" depends="Common">
<mkjar tgt="DB" />
</target>
<target name="FooBar" depends="Common,DB">
<mkjar tgt="FooBar" />
</target>
<target name="FooBar2" depends="Common,Net,DB">
<mkjar tgt="FooBar2" />
</target>
</project>
Output:
Buildfile: buildtest.xml
Common:
[echo] mkjar Common
Net:
[echo] mkjar Net
DB:
[echo] mkjar DB
FooBar2:
[echo] mkjar FooBar2
Common:
[echo] mkjar Common
DB:
[echo] mkjar DB
FooBar:
[echo] mkjar FooBar
BUILD SUCCESSFUL
Total time: 283 milliseconds
Why is Ant going twice into targets Common and DB? I thought macrodef would be executed inside the same Ant flow and project scope. I tried uglier things with antcall
<target name="mkjar" unless="jar-${tgt}">
<property name="jar-${tgt}" value="true" />
...
</target>
But it's not better (it's even creating several jar-<tgt> properties!).
I thought macrodef would be executed inside the same Ant flow and project scope
It does. When you invoke ant FooBar2 FooBar, both targets are executed in the same Ant project. But in this case, Ant does not "track" that a dependency target has already executed when it is also a dependency for the second target.
A dependency target is invoked only once when it is in the same chain of dependencies of only one target, for example:
<target name="A"/>
<target name="B" depends="A"/>
<target name="C" depends="B"/>
<target name="D" depends="C,B,A"/>
When running ant D, the following chain of targets is run: A --> B --> C --> D.
See https://ant.apache.org/manual/targets.html for more info.
One way to solve the issue here is to add a condition on the target to skip running it when a property is set:
<target name="Common" unless="common.already.executed">
<mkjar tgt="Common" />
<property name="common.already.executed" value="true" />
</target>

Accessing project prefix inside the included ant file

Is it possible to access "as" prefix inside the included ant file
(i.e. to access "as" attribute value specified in include task)
file including.xml:
<project name="myproject">
<include file="included.xml" as="nested" />
</project>
file included.xml:
<project>
<echo message="I am included into ${ant.project.name} as ${SomePropertyIAskAbout}" />
</project>
Desired output: "I am included into myproject as nested"
<include> executes an included buildfile as if were in the including buildfile. Having both files reference a custom property will do the trick.
including.xml
<project name="myproject">
<property name="including-as-attribute" value="nested" />
<include file="included.xml" as="${including-as-attribute}" />
</project>
included.xml
<project>
<fail unless="including-as-attribute"/>
<echo message="I am included into ${ant.project.name} as ${including-as-attribute}" />
</project>
Output of ant -f including.xml
[echo] I am included into myproject as nested

ant delete task with wildcard not working

I would like to set up an ant task to delete all .class and .jar
files from the same directory the build.xml file is, including
nonsense.class which is in the same directory as the build.xml
file. So I've set up the following build.xml file for ant 1.9.0
as follows:
<?xml version="1.0"?>
<project name="HelloWorld" default="deploy">
<!-- ... -->
<target name="clean">
<delete file="nonsense.class" />
<delete file="*.class" />
<delete file="*.jar" />
</target>
</project>
When I execute it nonsense.class is removed but none of
the other .class or .jar files. What am I doing wrong?
You need to use a fileset to delete more than one file:
<target name="clean">
<delete file="nonsense.class" />
<delete>
<fileset dir=".">
<include name="*.class"/>
<include name="*.jar"/>
</fileset>
</delete>
</target>

Jenkins JUnit Test Result Report plugin states that the JUnit xml file is not found?

The exact message received from jenkins is:
No test report files were found. Configuration error?
Build step 'Publish JUnit test result report' changed build result to FAILURE
When configuring the JUnit Test Result Report plugin, on entering the 'Test Report XMLs' path as '/reports/TEST-*.xml', the following error is displayed beneath the path:
'/reports/TEST-*.xml' doesn't match anything: '' exists but not '/reports/TEST-*.xml'
I have tried using the full path as well but that produces the same result. In both cases the paths should have picked up the 'TESTS-TestSuites.xml' file that was present in the /reports directory.
I'm not sure whether this is a problem with the plugin or the XML file being generated. I'm also aware that it could be an issue with the ant build script that I have written to run the JUnit tests and produce the XML result file therefore I have included the contents of this below in case something needs to be changed:
<?xml version="1.0" encoding="utf-8"?>
<project name="jenkins-tests" basedir="." default="linux">
<property name="junit.output.dir" value="output"/>
<property name="src.dir" value="src"/>
<property name="lib.dir" value="libs" />
<property name="bin.dir" value="bin" />
<property name="full-compile" value="true" />
<path id="classpath.base"/>
<path id="classpath.test">
<pathelement location="${bin.dir}" />
<pathelement location="${src.dir}" />
<pathelement location="${lib.dir}" />
<pathelement location="${lib.dir}/junit.jar" />
<path refid="classpath.base" />
</path>
<target name="clean" description="Clean up build artefacts">
<delete dir="${basedir}/${junit.output.dir}" />
</target>
<target name="prepare" depends="clean" description="Prepare for build">
<mkdir dir="${basedir}/${junit.output.dir}" />
<mkdir dir="${junit.output.dir}/reports"/>
</target>
<target name="compile" depends="prepare">
<javac srcdir="${src.dir}" destdir="${bin.dir}" verbose="${full-compile}" includeAntRuntime="false" >
<classpath refid="classpath.test"/>
</javac>
</target>
<target name="test" depends="compile">
<junit printsummary="true" haltonfailure="false">
<formatter type="xml" usefile="true"/>
<classpath refid="classpath.test" />
<batchtest fork="yes" todir="${junit.output.dir}">
<fileset dir="${src.dir}">
<include name="*.java"/>
</fileset>
</batchtest>
</junit>
</target>
<target name="test-reports" depends="test">
<junitreport tofile="TESTS-TestSuites.xml" todir="${junit.output.dir}/reports">
<fileset dir="${junit.output.dir}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${junit.output.dir}/reports" />
</junitreport>
</target>
</project>
I've been researching into this problem for a while now and haven't found any solution so I would appreciate any help. Thanks.
Jenkins looks for the path from the workspace root. Ensure that the given path is correct or use wildcards to look in multiple locations. Try using **/reports/TEST-*.xml
Are you sure the reports folder is right under the workspace? Verify manually if the test result files are indeed present in the location given in the path.
For my Android project which has multiple Gradle product flavors I used the following path for Test report XMLs:
**/build/test-results/**/TEST-*.xml

ant build script don't run on windows using command line

i have written an ant script, which runs ok and generate the .jar file when i use it with eclipse.
But when i use it on command prompt on windows xp, it's shows successfull, but nothing happens. ant is properly configured and also i can run other ant scripts.
here is my build.xml file
<?xml version="1.0"?>
<project name="TaskNodeBundle" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="bundlename" value="task-node-bundle" />
<property name="src.dir" location="../src" />
<property name="lib.dir" location="../lib" />
<property name="build.dir" location="/buildoutput" />
<property name="build.dest" location="build/dest" />
<!--
Create a classpath container which can be later used in the ant task
-->
<path id="classpath">
<fileset dir="${lib.dir}/">
<include name="*.jar" />
</fileset>
</path>
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${build.dest}" />
</target>
<!-- Deletes the existing build directory-->
<target name="mkdir" depends="clean">
<mkdir dir="${build.dest}"/>
</target>
<!-- Compiles the java code -->
<target name="compile" depends="mkdir">
<javac srcdir="${src.dir}" destdir="${build.dest}" classpathref="classpath" />
</target>
<target name="package-bundle" depends="compile" description="Generates the bundle">
<jar destfile="${dist.dir}/${bundlename}.jar">
<fileset dir="${src.dir}">
<include name="**/**.class" />
<include name="**/**.properties"/>
<include name="/META-INF/**.*" />
<include name="/META-INF/spring/**.*" />
</fileset>
</jar>
</target>
</project>
When you execute an ant script from the command line it will execute the first target defined in the build.xml file (in your case clean).
You can specify the target(s) to be executed on the command line
$ ant target1 target2
or define a default target in your build.xml file with the default attribute of the <project> tag:
<project name="TaskNodeBundle" basedir="." default="package-bundle">

Resources