I want to use ant to find the file with the latest version number. For example, I have a file directory named tomcat with the following files:
apache-tomcat-6.0.37.zip
apache-tomcat-6.0.38.zip
apache-tomcat-6.0.39.zip
I want ant to determine that apache-tomcat-6.0.39.zip is the latest file. Is there a way to do this?
Thanks!
Use resource collections, see last and sort. f.e. :
<project>
<path id="foo">
<last>
<sort>
<fileset dir="C:/some/path" includes="**/apache-tomcat-*.zip"/>
</sort>
</last>
</path>
<echo>$${foo} => ${toString:foo}</echo>
</project>
output :
[echo] ${foo} => C:\some\path\apache-tomcat-6.0.39.zip
use <pathconvert>to get file names from the relevant <fileset>.
use <sortlist> to sort the filenames in natural String order.
pick the last filename from the list.
try this:
<fileset dir="${your.base.dir}" id="one">
<include name="**/apache-tomcat-.*.zip"/>
</fileset>
<pathconvert property="orig.list" refid="one" pathsep=","/>
<sortlist property="sorted.list" value="${orig.list}" delimiter="," />
<propertyregex property="result" input="${sorted.list}" regexp=",?([^,]+?)$" select="\1"/>
inputList: <property name="one" value="a.b-2,a.b-5,a.b-1,a.b-3,a.b-4"/>
outputList: [echo] a.b-5
Related
I am using FileSet in an Ant build file that is being read as an input from external source. How can I check if a specific file exists in the FileSet?
For example, if the FileSet being read is **/src/*.java, I should be able to find MyClass.java in it. However, if the FileSet is **/src/AClass.java, **/src/BClass.java, then MyClass.java is not in it.
Even if there is a way I can expand the FileSet and do a string contains check, then that should work.
There are plenty of ways to create selectors within a FileSet, but there is nothing I could find that tells how to find/select a File in a given FileSet. So I can't back it up with an example that I could try. Any help will be very appreciated.
Using the restrict resource collection to include only the named file:
<project ... xmlns:rsel="antlib:org.apache.tools.ant.types.resources.selectors">
...
<restrict id="filtered.fileset">
<fileset refid="source.fileset"/>
<rsel:name name="MyClass.java"/>
</restrict>
<condition property="file.found" value="yes">
<resourcecount when="greater" count="0" refid="filtered.fileset" />
</condition>
I implemented that by doing an intersection of the complete Fileset with the Fileset containing just this one file that I am looking, and verifying that if the count equals 1. The other conditions can be easily AND-ed or OR-ed.
<target name="checkSomething">
<condition property="something.present">
<and>
<available file="/src/theFile"/>
<resourcecount when="equal" count="1">
<intersect>
<fileset dir="${src.dir}" includes="*"/>
<fileset dir="${src.dir}" includes="**/src/something.java"/>
</intersect>
</resourcecount>
</and>
</condition>
<echo message="Something present: ${something.present}"/>
</target>
I need to copy a file using ant from one folder to the other.
I know the exact name of the src file but for the destination file it's unknown since it's a version based folder I used the following script but it didn't work (please notice the * in the todir attribute:
<copy todir="${env.WORKSPACE}/my_dist_folder*" >
<fileset dir="${env.WORKSPACE}/my_src_folder">
<exclude name="**/*.svn"/>
</fileset>
</copy>
Please Advice!
Thanks
assuming there may be multiple my_dist_folder-xx.. folders:
<!-- goups all directories of the form "my_dist_folder-xx" -->
<dirset id="dir_list" dir="${env.WORKSPACE}">
<include name="my_dist_folder*"/>
</dirset>
<!-- generate path names of the above dirs and sort them -->
<pathconvert property="dir_names_list" refid="dir_list" pathsep=","/>
<sortlist property="sorted_names_list" value="${dir_names_list}" delimiter="," />
<!-- pick the last path-name (as it corresponds with the latest version-number directory ) -->
<propertyregex property="dist_folder" input="${sorted_names_list}" regexp=",?([^,]+)$" select="\1"/>
<!-- use this "dist_folder" as "todir" in the copy task -->
<copy todir="${dist_folder}" >
<fileset dir="${env.WORKSPACE}/my_src_folder">
<exclude name="**/*.svn"/>
</fileset>
</copy>
although, from your question, it appears that all such my_dist_folder-xx.. folders would be directly under the basedir ${env.WORKSPACE}... in case they may be under sub-directories within the basedir, then replace the line <include name="my_dist_folder*"/> with <include name="**/my_dist_folder*"/> in the <dirset> task.
UPDATE: also, if you're sure that there'll always be only one such folder my_dist_folder-xx.. on the system, then you may remove the following two lines- <sortlist> and <propertyregexp>, because the <dirset> will generate only one path-name and therefore you won't need to sort/pick-the-last-one. so you may use the <pathconvert> property to directly set the destination folder. eg:
<dirset id="dir_list" dir="${env.WORKSPACE}">
<include name="my_dist_folder*"/>
</dirset>
<!-- generate path name of the above dir -->
<pathconvert property="dir_names_list" refid="dir_list" pathsep=","/>
<!-- use this "dir_names_list" as "todir" in the copy task -->
<copy todir="${dir_names_list}" >
<fileset dir="${env.WORKSPACE}/my_src_folder">
<exclude name="**/*.svn"/>
</fileset>
</copy>
I have a sqlMapConfig.xml that has three SQLMaps defined in it.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!-- Statement namespaces are required for Ibator -->
<settings enhancementEnabled="true" useStatementNamespaces="true"/>
<!-- Setup the transaction manager and data source that are
appropriate for your environment
-->
<transactionManager type="JDBC">
<dataSource type="SIMPLE" >
<property name="JDBC.Driver"
value="com.mysql.jdbc.Driver"/>
<property name="JDBC.ConnectionURL"
value="jdbc:mysql://localhost:3306/sug"/>
<property name="JDBC.Username"
value="root"/>
<property name="JDBC.Password"
value="admin"/>
</dataSource>
</transactionManager>
<!-- SQL Map XML files should be listed here -->
<sqlMap resource="com/tatakelabs/dbmaps/categories_SqlMap.xml" />
<sqlMap resource="com/tatakelabs/dbmaps/pro_SqlMap.xml" />
<sqlMap resource="com/tatakelabs/dbmaps/pro_category_SqlMap.xml" />
</sqlMapConfig>
I get a runtime error - Cause: java.io.IOException: Could not find resource com/tatakelabs/dbmaps/categories_SqlMap.xml
categories_SqlMap.xml is present in that location. I tried changing the location of the map xml, but that did not help. sqlMapConfig.xml validates against the DTD. categories_SqlMap.xml also validates against the right DTD. I am at my wits end trying to figure out why it can't find the resource. The sqlMap files are generated by iBator.
This was happening because the sqlmap file location was not getting copied to target. Added a copy goal and that fixed it.
I had the same problem. It appears the problem lies with the location of the config file. Thus, its in relation of the project resource structure.
I moved the config file in the same package as the mapper classes and it worked. In this case try moving all the resources to this package and update the resource attributes to:
<sqlMap resource="categories_SqlMap.xml" />
<sqlMap resource="pro_SqlMap.xml" />
<sqlMap resource="pro_category_SqlMap.xml" />
Solved it.
I moved the xml file to where the Pojo was located and provided the path as follows:
<sqlMap resource="com/heena/ibatis/model/jsr/jsr.xml" />
And it worked.
place it ...src\java\abc.xml under the Source Packages directory.
If you are using Spring, you can use a SqlMapClientFactoryBean specifying property "mappingLocations". In this property you can specify a generic path, such as "com/tatakelabs/dbmaps/*_SqlMap.xml" or with a variable such as ${mapfiles}, that will be resolved by Spring as an array of file names. This lets you omit sqlMap element in sqlMapConfig. This technique will run with iBatis 2.3.4 on. However sql-map-config-2.dtd is also contained inside iBatis.jar, so you can experience some parsing errors, as /com/ibatis/sqlmap/engine/builder/xml/sql-map-config-2.dtd may have a bug. In this case you may want to replace it inside the jar with the one from the URL:
http://ibatis.apache.org/dtd/sql-map-config-2.dtd.
I want to find the path of a file in a directory (similar to unix 'find' command or the 'which' command, but I need it to work platform-independent) and save it as a property.
Tried to use the whichresource ant task, but it doesn't do the trick (I think it's only good for looking inside jar files).
I would prefer if it would be pure ant and not to write my own task or use a 3rd-party extension.
Notice that there might be several instances of a file by that name in the path - I want it to only return the first instance (or at least I want to be able to choose only one).
Any suggestions?
One possibility is to use the first resource selector. For example to find a file called a.jar somewhere under directory jars:
<first id="first">
<fileset dir="jars" includes="**/a.jar" />
</first>
<echo message="${toString:first}" />
If there are no matching files nothing will be echoed, otherwise you'll get the path to the first match.
Here is an example which selects the first matching file. The logic is as follows:
find all matches using a fileset.
using pathconvert, store the result in a property, separating each matching file with line separator.
use a head filter to match the first matching file.
The functionality is encapsulated in a macrodef for reusability.
<project default="test">
<target name="test">
<find dir="test" name="*" property="match.1"/>
<echo message="found: ${match.1}"/>
<find dir="test" name="*.html" property="match.2"/>
<echo message="found: ${match.2}"/>
</target>
<macrodef name="find">
<attribute name="dir"/>
<attribute name="name"/>
<attribute name="property"/>
<sequential>
<pathconvert property="#{property}.matches" pathsep="${line.separator}">
<fileset dir="#{dir}">
<include name="#{name}"/>
</fileset>
</pathconvert>
<loadresource property="#{property}">
<string value="${#{property}.matches}"/>
<filterchain>
<headfilter lines="1"/>
</filterchain>
</loadresource>
</sequential>
</macrodef>
</project>
I created a macro based on martin-clayton's answer.
sample project with macro and a property file that is read from the found file
<?xml version="1.0" encoding="utf-8"?>
<project name="test properties file read" default="info">
<macrodef name="searchfile">
<attribute name="file" />
<attribute name="path" default="custom,." />
<attribute name="name" />
<sequential>
<first id="#{name}">
<multirootfileset basedirs="#{path}" includes="#{file}" erroronmissingdir="false" />
</first>
<property name="#{name}" value="${toString:#{name}}" />
</sequential>
</macrodef>
<searchfile name="custom.properties.file" file="config.properties" />
<property file="${custom.properties.file}" />
<target name="info" >
<echo>
origin ${config.origin}
</echo>
</target>
Currently I do
<foreach list="${myfolders}" target="bundle"
param="worksheet" inheritall="true"/>
to execute the target "bundle" on a list of folders. However the problem is I need to set this list. How do I use Ant to just loop through all the folders given the parent directory?
If there is a way to do this and also exclude specific folders that would be even better. Thanks.
You can provide a <dirset> for the <foreach> task to operate on:
<foreach target="bundle" param="worksheet" inheritall="true">
<path>
<dirset dir="${subDir}">
<include name="*"/>
</dirset>
</path>
</foreach>
Notice that the list parameter isn't used when I do it this way.
You can't use <dirset> directly under the <foreach> as you can with <fileset>. However, you can put the <dirset> under the <path> as shown above. The <include name="*"/> prevents recursing down the directory tree.
You can do this with subant
Example:
<?xml version="1.0" encoding="UTF-8"?>
<project name="subant" default="subant1">
<target name="subant1">
<subant target="">
<dirset dir="." includes="*"/>
<target name="release" />
</subant>
</target>
</project>
I use the foreach task from ant-contrib for a similar job. This will call a specified target for each entry in a list, passing the entry as a parameter each time.