Fileset directory not excluding properly - ant

Learning Ant - I'm trying to exclude all files under a certain directory with:
<fileset dir="${testclasses.dir}" id="test.files">
<exclude name="**/directory-to-exclude/*.class"/>
</fileset>
and to make sure I excluded correctly, I'm printing out all the classes under that fileset like so:
<path id="updated.test.classes.path">
<fileset dir="${testclasses.dir}"/>
</path>
<pathconvert pathsep="${line.separator}| |-- "
property="echo.path.compile"
refid="updated.test.classes.path">
</pathconvert>
<echo message="Printing updated class path: ${echo.path.compile}"/>
Yet I still see:
[echo] | |-- /path/to/testfiles/directory-to-exclude/SomeTestClass$$anon$1.class
[echo] | |-- /path/to/testfiles/directory-to-exclude/SomeTestClass$$typecreator1$1.class
[echo] | |-- /path/to/testfiles/directory-to-exclude/SomeTestClass$.class
[echo] | |-- /path/to/testfiles/directory-to-exclude/SomeTestClass$Complex$.class
[echo] | |-- /path/to/testfiles/directory-to-exclude/SomeTestClass$Complex.class
[echo] | |-- /path/to/testfiles/directory-to-exclude/SomeTestClass$Deep$.class
[echo] | |-- /path/to/testfiles/directory-to-exclude/SomeTestClass$Deep.class
[echo] | |-- /path/to/testfiles/directory-to-exclude/SomeTestClass$LessDeep$.class
[echo] | |-- /path/to/testfiles/directory-to-exclude/SomeTestClass$LessDeep.class
[echo] | |-- /path/to/testfiles/directory-to-exclude/SomeTestClass$StrToTime.class
[echo] | |-- /path/to/testfiles/directory-to-exclude/SomeTestClass$Struct$.class
[echo] | |-- /path/to/testfiles/directory-to-exclude/SomeTestClass$Struct.class
[echo] | |-- /path/to/testfiles/directory-to-exclude/SomeTestClass.class
why is this happening?

You aren't using the fileset you pre-defined, you want:
<path id="updated.test.classes.path">
<fileset refid="test.files"/>
</path>
Note the refid= parameter, which makes use of the fileset you defined with the exclusion.
Your actual code
<fileset dir="${testclasses.dir}"/>
in the path task will simply include all files, as you observed.

Related

Ant Dirset of all Sub-Directories which contain Files

I need to make a dirset containing all low-level directories which contain files.
So for example a structure like:
.
├── _a
├── _b
| ├── file1.class
| └── file2.class
├── _c
| └── _d
| └── file3.class
├── _e
| ├── _f
| └── _g
└── _h
└── file4.class
Would result in a dirset of the directories:
./b/
./c/d/
./h/
How could I achieve this?
I found the solution, it was to first create a fileset in the directory that I needed to scan, then use a pathconvert in order to get the parent directories of all files and to get the relative path.
All that was left was to create a dirset with an includes that specifies the paths from the pathconvert (it also removes duplicate directory entries).
<pathconvert pathsep="," property="filePaths">
<map from="${basedir}/" to="" />
<!-- get parent of file -->
<regexpmapper from="(.*)\${file.separator}" to="\1" />
<path>
<!-- change dir to your directory -->
<fileset dir="${basedir}/dir" casesensitive="yes">
<include name="**/*.*" />
</fileset>
</path>
</pathconvert>
<dirset dir="${basedir}/" casesensitive="yes" includes="${filePaths}" />
Not sure in which task you wanted.
Anyways, try fileset as given below:
<fileset dir="${base.dir}" casesensitive="yes">
<include name="**/*.*"/>
</fileset>

Ant property bug

I have two deployment configurations.
Each configuration store in its property file.
Dev configuration in dev.properties:
deploy.wowza.domain=DEV_IP_ADDRESS
Prod configuration in prod.properties:
deploy.wowza.domain=PROD_IP_ADDRESS
I have build.xml
<?xml version="1.0"?>
<project name="MAIN" default="dev" basedir=".">
<target name="dev">
<property file="${java.root.dir}/ant/dev.properties"/>
<echo>
DEV
${deploy.wowza.domain}
</echo>
<sleep seconds="1"/>
</target>
<target name="prod">
<property file="${java.root.dir}/ant/prod.properties"/>
<echo>
PROD
${deploy.wowza.domain}
</echo>
<antcall target="deploy"/>
</target>
</project>
If I run prod or dev task it show correct property value only from second run
D:\Dropbox\camwithme>ant prod
Buildfile: D:\Dropbox\camwithme\build.xml
prod:
[copy] Copying 1 file to D:\Dropbox\camwithme\wowza_cam\ant
[echo]
[echo] PROD
[echo] PROD_IP_ADDRESS
[echo]
BUILD SUCCESSFUL
Total time: 0 seconds
D:\Dropbox\camwithme>ant dev
Buildfile: D:\Dropbox\camwithme\build.xml
dev:
[copy] Copying 1 file to D:\Dropbox\camwithme\wowza_cam\ant
[echo]
[echo] DEV
[echo] PROD_IP_ADDRESS !!! Should be dev ip here !!!
[echo]
BUILD SUCCESSFUL
Total time: 0 seconds
D:\Dropbox\camwithme>ant prod
Buildfile: D:\Dropbox\camwithme\build.xml
prod:
[copy] Copying 1 file to D:\Dropbox\camwithme\wowza_cam\ant
[echo]
[echo] PROD
[echo] DEV_IP_ADDRESS
[echo]
Problem was because of another build.xml which load properties files before copy

Change directory of FileList when referencing it

I want to change the base directory of a FileList when referencing it later in my buildfile.
I define the following FileList:
<filelist dir="./dev" id="sourceFiles">
<file name="files/file.php" />
<file name="files/class.php" />
<file name="files/functions.php" />
</filelist>
And my targets are the following
<target name="fetch">
<copy todir="./src/files">
<filelist refid="sourceFiles" />
</copy>
</target>
<target name="build" depends="fetch">
<replaceregexp match="(\d+.\d+.\d+)(.\d+)?" replace="\1.${build.number}">
<filelist refid="sourceFiles" />
</replaceregexp>
</target>
So, when using the replaceregexp task, it will use the files located in ./dev - but I want the task to replace in the files copied earlier that are now located in ./src.
Of course, I could copy the file list and use another dir, but I'd very much like to only hold the list of files once in my buildfile.
How can I achieve this?
I think what you're looking for is a mapper within the copy command as follows:
<project name="demo" default="copy">
<property name="build.number" value="1.0.1"/>
<target name="copy">
<copy todir="build">
<fileset dir="src"/>
<regexpmapper from="^(.*)\.txt$" to="\1.${build.number}"/>
</copy>
</target>
<target name="clean">
<delete dir="build"/>
</target>
</project>
Here's my test files
|-- build
| `-- files
| |-- one
| | |-- file1.1.0.1
| | `-- file2.1.0.1
| `-- two
| `-- file3.1.0.1
|-- build.xml
`-- src
`-- files
|-- one
| |-- file1.txt
| `-- file2.txt
`-- two
`-- file3.txt

How to copy different files of 2 folders to a new place with ant

There are two folders A and B. I need to copy different files between A and B to a third folder C. How can I do it? There is a difference collection in ant, but how to use it in copy? Thanks for any idea.
Following ANT script will only copy those files not present in both directories:
<project name="copy" default="copy-files">
<target name="copy-files">
<copy todir="C" verbose="true" overwrite="true">
<fileset dir="A">
<present present="srconly" targetdir="B"/>
</fileset>
<fileset dir="B">
<present present="srconly" targetdir="A"/>
</fileset>
</copy>
</target>
</project>
Test setup:
$ tree
.
|-- A
| |-- one
| |-- shared
| `-- two
|-- B
| |-- four
| |-- shared
| `-- three
`-- build.xml
Running the build demonstrates that the files called "shared" are not copied:
$ ant
Buildfile: build.xml
copy-files:
[copy] Copying 4 files to C
[copy] Copying A/one to C/one
[copy] Copying A/two to C/two
[copy] Copying B/four to four
[copy] Copying B/three to three
BUILD SUCCESSFUL
Total time: 0 seconds

Properties files not found running java through Ant

the structure of the example project is:
.
|-- ./build
| `-- ./build/TestAntLoadFile.class
|-- ./build.xml
|-- ./dist
| |-- ./dist/icpFinder.jar
| `-- ./dist/icp-finder.properties
|-- ./icp-finder_bak.properties
`-- ./src
`-- ./src/TestAntLoadFile.java
and the code getting the properties file is:
public class TestAntLoadFile {
private static final String CUSTOMER_CONFIG_FILE_NAME
= "icp-finder.properties";
public static void main(String[] args) {
InputStream custumerConfigIn = TestAntLoadFile.class.
getClassLoader().getResourceAsStream(CUSTOMER_CONFIG_FILE_NAME);
System.out.println("custumerConfigIn: " + custumerConfigIn);
}
}
and build.xml core contend is :
<path id="run.classpath">
<fileset dir = "${dist.dir}" >
<include name="**/*.jar"/>
<include name="**/*.properties"/>
<include name="./icp-finder.properties"/>
</fileset>
</path>
<target name="run" depends="jar">
<java fork="true" classname="TestAntLoadFile">
<classpath>
<path refid="run.classpath"/>
</classpath>
</java>
</target>
the project run well in eclipse, Has anybody got any suggestions?
Rather than including the properties file itself in the classpath, you need to include the directory it resides in, something like this for example:
<path id="run.classpath">
<fileset dir="${dist.dir}" >
<include name="**/*.jar"/>
</fileset>
<dirset dir="${dist.dir}" />
<pathelement path="${dist.dir}" />
</path>

Resources