Ant任务从String中选择单词

时间:2011-03-10 06:41:50

标签: ant

说我有字符串 - “D:\ ApEx_Schema \ Functions \ new.sql @@ \ main \ ONEVIEW_Integration \ 3” 我需要将以下内容提取到diff变量中 - 文件名  - 文件路径   - 和版本(字符串的最后一个字符)

请使用ANT任务帮助

====================================== 我正在尝试读取包含以下数据的txt文件: -

.\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

尝试收集文件名,路径详细信息及其版本,并使用SQL任务在数据库中更新文件名。 虽然我的build.xml没有按照需要提供输出。 任何建议和意见!

我的Build.xml文件看起来像 - ============== START ===========================

<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 ===========================

2 个答案:

答案 0 :(得分:2)

这不是Ant擅长的东西。 可能最简单的解决方案是使用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>

这涉及获取ant-contrib任务集合的jar。

< - 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>

(注意:在unix操作系统上对此进行测试,因此如果在Windows上,您可能需要调整路径分隔符处理。)

<强>更新

关于您的实施的一些注意事项。

您可能需要命名'for'任务:

<ac:for list="${src}" param="detls" delimiter="${line.separator}"
        xmlns:ac="antlib:net.sf.antcontrib">
    ...
</ac:for>

如果您希望propertyregex更改属性(您在迭代时执行此操作),则需要设置    每个人override="yes"。 在for内,你需要逃离at符号,例如:

<propertyregex property="path"
               input="@{detls}"
               regexp="(.*)\\.*\@\@" select="\1"
               override="yes"/>

引用propertyregex使用${path}而不是@{path}设置的属性。

答案 1 :(得分:1)

一种替代方案,不需要提供任务“propertyregex”,也不需要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> 
相关问题