Ant task to pick words from a String
Say I have the string - "D:\ApEx_Schema\Functions\new.sql@@\main\ONEVIEW_Integration\3" I require to fetch the following into diff variables - file name - file path - and the version (which is the last character of the string)
Pls any helps using ANT tasks
====================================== I am trying to read a txt file which contains data as given below :-
.\ApEx_Schema\Functions\new.sql@@\main\ONEVIEW_Integration\3
.\ApEx_Schema\Functions\Functions.sql@@\main\ONEVIEW_Integration\3
.\ApEx_Schema\Indexes\Indexes.sql@@\main\ONEVIEW_Integration\2
and trying to collect the file name, its path details and its version and updating the same in a DB using the SQL task. Though my build.xml is not giving output as desired. Any suggestions and views!!!
My Build.xml file looks like - ==============START========开发者_Python百科===================
<description>
obiee copy files build file
</description>
<replace file="D:\buildFRIDAY\database.txt" token=".\" value="D:\"/>
<loadfile property="src" srcFile="D:\buildFRIDAY\database.txt"/>
<path id="antclasspath">
<fileset dir="D:\OraHome_1\oracledi\drivers">
<include name="ojdbc14.jar"/>
</fileset>
</path>
<for list="${src}" param="detls" delimiter="${line.separator}">
<sequential>
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
<propertyregex property="path" input="@{detls}"
regexp="(.*)\\.*@@" select="\1" />
<propertyregex property="file" input="@{detls}"
regexp=".*\\(.*)@@" select="\1" />
<propertyregex property="version" input="@{detls}"
regexp=".*\\(.*)" select="\1" />
<echo>
Input: @{detls}
Path: ${path}
File: ${file}
Version: ${version}
</echo>
<if>
<matches string="@{detls}" pattern=".sql" />
<then>
</then>
</if>
<if>
<matches string="@{detls}" pattern="[0-9]" />
<then>
<sql
driver="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@172.16.88.68:1521:rdev"
userid="rapid"
password="rapid"
print="yes"
classpathref="antclasspath">
Insert into ROLTA_PATCH_FILE_APP_TAB (PATCH_NO,FILE_NAME,FILE_PATH,FILE_VERSION,APPLIED_DATE,STATUS) values ('3.2.12',"@{detls}",'D:\ApEx_Schema\Functions\Functions.sql','3',to_date('11-MAR-11','DD-MON-RR'),'Y');
Insert into ROLTA_PATCH_TAB (PATCH_NO,PATCH_NAME,APPL_NAME,APPLIED_DATE) values ('3.2.12','2.1.11','@{detls}',to_date('11-MAR-11','DD-MON-RR'));
</sql>
</then>
</if>
</sequential>
</for>
==============END===========================
This isn't something that Ant is good at.
Probably the simplest solution is to use the ant-contrib propertyregex
task:
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
<property name="candidate"
value="D:\ApEx_Schema\Functions\new.sql@@\main\ONEVIEW_Integration\3" />
<propertyregex property="path" input="${candidate}"
regexp="(.*)\\.*@@" select="\1" />
<propertyregex property="file" input="${candidate}"
regexp=".*\\(.*)@@" select="\1" />
<propertyregex property="version" input="${candidate}"
regexp=".*\\(.*)" select="\1" />
<echo>
Input: ${candidate}
Path: ${path}
File: ${file}
Version: ${version}
</echo>
That involves getting hold of the jar of the ant-contrib collection of tasks.
A - pretty nasty - alternative is to use a scriptdef
task.
<scriptdef name="get_elements" language="javascript">
<attribute name="candidate" />
<attribute name="path-property" />
<attribute name="file-property" />
<attribute name="version-property" />
<![CDATA[
filesep = project.getProperty( "file.separator" );
candidate = attributes.get( "candidate" );
path = candidate.substring( 0, candidate.indexOf( "@@" ) );
file = path.substring( path.lastIndexOf( filesep ) + 1 );
path = path.substring( 0, path.lastIndexOf( filesep ) );
version = candidate.substring( candidate.lastIndexOf( filesep ) + 1 );
project.setProperty( attributes.get( "path-property" ), path );
project.setProperty( attributes.get( "file-property" ), file );
project.setProperty( attributes.get( "version-property" ), version );
]]>
</scriptdef>
<property name="candidate"
location="D:\ApEx_Schema\Functions\new.sql@@\main\ONEVIEW_Integration\3"
relative="yes" />
<get_elements candidate="${candidate}"
path-property="path"
file-property="file"
version-property="version" />
<echo>
Input: ${candidate}
Path: ${path}
File: ${file}
Version: ${version}
</echo>
(Note: tested this on a unix OS, so you may need to adjust path separator treatments if on Windows.)
Update:
Some notes on your implementation.
You may need to namespace the 'for' task:
<ac:for list="${src}" param="detls" delimiter="${line.separator}"
xmlns:ac="antlib:net.sf.antcontrib">
...
</ac:for>
If you want propertyregex
to change a property (which you do as you're iterating) you need to set
override="yes"
on each of those.
Inside a for
you need to escape the at signs, for example:
<propertyregex property="path"
input="@{detls}"
regexp="(.*)\\.*\@\@" select="\1"
override="yes"/>
To refer to the property set by the propertyregex
use ${path}
rather than @{path}
.
An alternative that won't require the contributed task "propertyregex", nor javascript.
<property name="candidate" value="D:\ApEx_Schema\Functions\new.sql@@\main\ONEVIEW_Integration\3" />
<pathconvert property="path">
<path location="${candidate}" />
<regexpmapper from="(.*)\\.*@@" to="\1" />
</pathconvert>
<pathconvert property="file">
<path location="${candidate}" />
<regexpmapper from=".*\\(.*)@@" to="\1" />
</pathconvert>
<pathconvert property="version">
<path location="${candidate}" />
<regexpmapper from=".*\\(.*)" to="\1" />
</pathconvert>
<echo>
Input: ${candidate}
Path: ${path}
File: ${file}
Version: ${version}
</echo>
精彩评论