Change directory of FileList when referencing it - ant

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

Related

Fileset directory not excluding properly

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.

Ant: copy folder and then delete only the files copied

I need a build script to be able to copy a folder with all its nested files and folders to another folder. The destination folder already contains its own files and folders, and its nested folder structure can overlap with the source folder. Having executed some commands, I then need to delete only the files that were copied.
Example:
src_folder
subfolder1
file1
dest_folder
subfolder1
file2
file3
Here I need to delete only dest_folder/subfolder1/file1
Is there a way to do this with Ant? If not, can you suggest an alternative build tool that makes this possible?
One way to accomplish similar results would be to back up dest_folder, but it has a lot of files and it takes too long to copy it.
Example
Project files
├── build.xml
├── dest_folder
│   └── subfolder1
│   ├── file2
│   └── file3
└── src_folder
└── subfolder1
└── file1
Build output
$ ant
Buildfile: build.xml
copy-files:
[copy] Copying 1 file to dest_folder
[copy] Copying src_folder/subfolder1/file1 to dest_folder/subfolder1/file1
run-cmd-on-files-in-dest-folder:
[apply] dest_folder/subfolder1/file1
clean-files:
[delete] Deleting dest_folder/subfolder1/file1
build.xml
<project name="demo" default="run">
<target name="run" depends="copy-files,run-cmd-on-files-in-dest-folder,clean-files"/>
<target name="copy-files">
<copy todir="dest_folder" verbose="true">
<fileset dir="src_folder"/>
</copy>
</target>
<target name="run-cmd-on-files-in-dest-folder">
<apply executable="echo">
<srcfile/>
<fileset dir="dest_folder">
<present present="both" targetdir="src_folder"/>
</fileset>
</apply>
</target>
<target name="clean-files">
<delete verbose="true">
<fileset dir="dest_folder">
<present present="both" targetdir="src_folder"/>
</fileset>
</delete>
</target>
</project>

Intermodule dependency in ant

I am using Ant 1.8
I have multiple modules in intelliJ IDEA. Each module has a build.xml and currently i need to browse till build.xml of that file and run ant for every module.
e.g. module B's build success depends on whether module A's build was successful.
Now, i want to update this process. It will be great if an option exists wherein i can write a single build process which will first build distribution for module A and then while building distribution for B, it will be checked if build for module A is successful.
Is there any way using current Ant mechanism. i could see something similar in ivy but i cant use it in my project.
Please suggest an approach using basic Ant features.
The subant task in ANT is the most flexible way to invoke a multi-module build, for example:
<project name="parent" default="build">
<target name="build">
<subant>
<filelist dir=".">
<file name="moduleA/build.xml"/>
<file name="moduleB/build.xml"/>
</filelist>
<target name="clean"/>
<target name="build"/>
</subant>
</target>
</project>
Project structure
|-- build.xml
|-- moduleA
| `-- build.xml
`-- moduleB
`-- build.xml
Note:
In my opinion the most powerful way to use this task is to combine it with the buildlist task from Apache ivy. Let the ivy inter-module dependency declarations automatically determine the module build order.
Thanks Mark!!
Your answer helped me a lot.
In addition to above answer I would like to add details, if properties are being loaded from properties file.
Project Structure:
|-- build.xml
|-- ProjectOne
-- build.xml
-- antbuilds.properties
|-- ProjectTwo
-- build.xml
-- antbuilds.properties
Common ANT build file:
<project name="Parent" default="all">
<target name="ProjectOne">
<subant>
<property file="ProjectOne/antbuilds.properties"/>
<filelist dir=".">
<file name="ProjectOne/build.xml"/>
</filelist>
<target name="deploy"/>
</subant>
</target>
<target name="ProjectTwo">
<subant>
<property file="ProjectTwo/antbuilds.properties"/>
<filelist dir=".">
<file name="ProjectTwo/build.xml"/>
</filelist>
<target name="deploy"/>
</subant>
</target>
<target name="all" depends="ProjectOne, ProjectTwo">
</target>

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