I have a list of files in a directory . I have to read all the file names and extract the file name in a sorted order. How can I do that using ant script
You did not specify the sort criteria. Lots of options available see the resource documentation in the ANT manual
https://ant.apache.org/manual/Types/resources.html
Example
├── build.xml
└── src
├── data1
│ ├── file1.txt
│ ├── file2.txt
│ └── file3.txt
├── data2
│ ├── file4.txt
│ └── file5.txt
└── data3
└── file6.txt
Running the project prints the list of files sorted in reverse order based on modified date
build:
[echo] Files: /../src/data2/file4.txt:/../src/data3/file6.txt:/../src/data2/file5.txt:/../src/data1/file3.txt:/../src/data1/file2.txt:/../src/data1/file1.txt
build.xml
<project name="demo" default="build">
<target name="build">
<sort id="src.files">
<fileset dir="src" />
<reverse xmlns="antlib:org.apache.tools.ant.types.resources.comparators">
<date />
</reverse>
</sort>
<pathconvert targetos="unix" property="srcFiles" refid="src.files"/>
<echo message="Files: ${srcFiles}"/>
</target>
</project>
Related
I want to copy some specific files from source directory to destination directory on below condition using ANT.
Source folder contains the following files
35001_abc.sql
38001_abc.sql
38002_abc.sql
39001_abc.sql
I want to copy the files with filenames starting with 36000 and above.
The Output directory should contain the following files
38001_abc.sql
38002_abc.sql
39001_abc.sql
One idea is to use a regular expression on the filename to restrict ranges of digits.
Example
├── build.xml
├── src
│ ├── 35001_abc.sql
│ ├── 38001_abc.sql
│ ├── 38002_abc.sql
│ ├── 39001_abc.sql
│ ├── 41001_abc.sql
│ └── 46001_abc.sql
└── target
├── 38001_abc.sql
├── 38002_abc.sql
├── 39001_abc.sql
├── 41001_abc.sql
└── 46001_abc.sql
build.xml
<project name="demo" default="copy">
<property name="src.dir" location="src"/>
<property name="build.dir" location="target"/>
<target name="copy">
<copy todir="${build.dir}" overwrite="true" verbose="true">
<fileset dir="${src.dir}">
<filename regex="^(3[6-9]|[4-9]\d)\d{3}_abc.sql$"/>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
</project>
I need some help with Ant. I want to create a file using Ant, which to contain some names and the size in kb of some folders and also to contain other kind of data(not size) but with the same pattern.
Something like this:
build.date=April 01, 2000
folder1=10000
folder2=59093
folder3=646854
folder4=14897123
And also to make the sum of some folder sizes(for example foldersum=folder1+folder2) and write that in the file:
build.date=April 01, 2000
folder1=10000
folder2=59093
folder3=646854
folder4=14897123
foldersum=folder1+folder2
ANT is not a programming language. The best way to do this is to embed a scripting language, for example groovy.
Example
Given the following directory structure
├── build.xml
├── folder1
│ ├── file1.txt
│ ├── file2.txt
│ └── file3.txt
├── folder2
│ ├── file4.txt
│ └── file5.txt
└── folder3
└── file6.txt
Produces a file called "build-report.txt"
build.date=April 09,2015
folder1=1
folder2=1
folder3=1
foldersum=3
build.xml
<project name="demo" default="build">
<available classname="org.codehaus.groovy.ant.Groovy" property="groovy.installed"/>
<target name="build" depends="init">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<dirset id="dirs" dir="." includes="folder*"/>
<groovy>
import java.text.SimpleDateFormat
def sdf = new SimpleDateFormat("MMMM dd,yyyy")
new File("build-report.txt").withWriter { w ->
w.println "build.date=${sdf.format(new Date())}"
def total = 0
ant.project.references.dirs.each {
def dir = new File(it.toString())
def size = dir.directorySize()
w.println "${dir.name}=${size}"
total += size
}
w.println "foldersum=${total}"
}
</groovy>
</target>
<target name="init" unless="groovy.installed">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/groovy.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.4.3/groovy-all-2.4.3.jar"/>
<fail message="Groovy installed run build again"/>
</target>
</project>
I have a folder sources with a subfolder be under it. Next fileset should select all *.cls files under the be folder, recursively! (so also .cls files in subfolders of the be folder):
<fileset dir="${basedir}/sources">
<include name="be/**/*.cls" />
</fileset>
Apparently ant doesn't select a single file...
If I change it to
<fileset dir="${basedir}/sources/be">
<include name="**/*.cls" />
</fileset>
Ant selects all of the .cls files.
What's the difference between the two snippets?
Folder structure:
sources
be
dirA
dirB
.cls files
dirC
.cls files
They both copy the files, just in different ways.
Notice how in the "be" directory root is included in the first copy:
├── build.xml
├── sources
│ └── be
│ └── dirA
│ ├── dirB
│ │ ├── file1.cls
│ │ └── file2.cls
│ └── dirC
│ └── file3.cls
└── target
├── copy1
│ └── be
│ └── dirA
│ ├── dirB
│ │ ├── file1.cls
│ │ └── file2.cls
│ └── dirC
│ └── file3.cls
└── copy2
└── dirA
├── dirB
│ ├── file1.cls
│ └── file2.cls
└── dirC
└── file3.cls
build.xml
<project name="demo" default="copy">
<property name="src.dir" location="sources"/>
<property name="build.dir" location="target"/>
<target name="copy" depends="copy1,copy2">
</target>
<target name="copy1">
<copy todir="${build.dir}/copy1">
<fileset dir="${src.dir}">
<include name="be/**/*.cls" />
</fileset>
</copy>
</target>
<target name="copy2">
<copy todir="${build.dir}/copy2">
<fileset dir="${src.dir}/be">
<include name="**/*.cls" />
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
</project>
I am using a third party build script creates minified js files with the minified file called 'someFile.js' and an unminified version called 'someFile.js.uncompressed.js'. I need one of my tasks in the build.xml to copy only the js files that have a partner '.uncompressed.js' file to another location. For example, given a directory structure like this:
- rootDirectory
- minified.js
- minified.js.uncompressed
- unminified.js
- anotherDirectory
- anotherMinified.js
- anotherMinified.js.uncompressed.js
- unminified.js
- anotherUnminified.js
the destination directory should end up like this:
- rootDirectory
- minified.js
- anotherDirectory
- anotherMinified.js
Is there any way to accomplish this with ant? I am using ant 1.8.1.
Thanks in advance for any help!
You can use an Ant <present> selector to do this. For example:
<copy todir="dest">
<fileset dir="src">
<present targetdir="src">
<mapper type="glob" from="*" to="*.uncompressed.js" />
</present>
</fileset>
</copy>
In this case the targetdir in the selector is the same as the root directory of the fileset.
What this does is copy any file in the src directory tree where a file with the same name, with .uncompressed.js appended also present.
You could use a custom scriptselector for your fileset.
Example
├── build.xml
├── src
│ └── rootDirectory
│ ├── anotherDirectory
│ │ ├── anotherMinified.js
│ │ ├── anotherMinified.js.uncompressed
│ │ ├── anotherUnminified.js
│ │ └── unminified.js
│ ├── minified.js
│ ├── minified.js.uncompressed
│ └── unminified.js
└── target
└── rootDirectory
├── anotherDirectory
│ └── anotherMinified.js
└── minified.js
build.xml
<project name="demo" default="copy-files">
<target name="copy-files">
<copy todir="target">
<fileset dir="src">
<scriptselector language="javascript">
importClass(java.io.File);
var testForFile = new File("src/" + filename + ".uncompressed");
self.setSelected(testForFile.exists());
</scriptselector>
</fileset>
</copy>
</target>
<target name="clean">
<delete dir="target"/>
</target>
</project>
Notes:
This should work without the need for additional jars. Javascript is supported by modern JVMs.
I have a source directory with the following structure.
D:\Files\temporaryname\my_files_to_be_copied
The directory name "temporaryname" may vary from time to time. I need an ant script to copy the files "my_file_to_be_copied" to some other destination.
Simply I need to go one folder inside the source directory and start copying all. Please assist. Thanks in advance.
Lots of ways to do this here's one example, demonstrating filesets and mappers.
Example
├── build.xml
├── build
│ └── destination
│ ├── file1.txt
│ ├── file2.txt
│ ├── file3.txt
│ └── file4.txt
└── src
├── dir1
│ └── my_files_to_be_copied
│ ├── file1.txt
│ └── file2.txt
└── dir2
└── my_files_to_be_copied
├── file3.txt
└── file4.txt
build.xml
<project name="demo" default="copy">
<target name="copy" description="Copy files">
<copy todir="build/destination">
<fileset dir="src" includes="**/my_files_to_be_copied/**"/>
<cutdirsmapper dirs="2"/>
</copy>
</target>
<target name="clean" description="Additionally purge ivy cache">
<delete dir="build"/>
</target>
</project>