2012-03-29 33 views
7

Tôi đang cố gắng thay thế số phiên bản trong tệp build.xml bằng cách sử dụng tập lệnh ANT.Thay thế thuộc tính trong tệp xml bằng ANT

Tôi đã thử nhiều cách tiếp cận khác nhau, đã tìm kiếm và tìm kiếm lại StackOverflow để tìm câu trả lời nhưng không thể nhận được truy vấn chính xác.

vì vậy đây là tập tin xml của tôi:

<?xml version="1.0" encoding="UTF-8"?> 
<?eclipse version="3.0"?> 

<project name="feature" default="main" basedir="."> 
<target name="init">  
    <property name="Version" value="1.0.0.20120327"/> 
</target> 

    <target name="main" depends="init"> 
    <description>Main target</description> 
</target> 
</project> 

Bây giờ là u có thể thấy phiên bản có ngày hôm qua. Tôi cần phải thay thế nó bằng ngày hiện tại.

Dưới đây là những gì tôi đã cố gắng:

<target name="replace"> 
    <tstamp > 
    <format property="touch.time" pattern="yyyyMMdd"/> 
    </tstamp> 

<property name="Feature.dir" location="../feature" /> 

<!--Didnt Work-->  
<copy file="${Feature.dir}\build.xml" tofile="${Feature.dir}\build1.xml" 
filtering="yes" overwrite="yes"> 
<filterset> 
    <filter token="Version" value="1.0.0.${touch.time}"/> 
</filterset> 
    </copy> 

    <!--Didnt work 

    <replacetoken><![CDATA[<property name="Version" value=""/>]]> 
    </replacetoken> 
    <replacevalue><![CDATA[<property name="Version"value="1.0.0.${touchtime}" />]]> 
    </replacevalue> 

    --> 


<!-- Didnt work 
    <copy file="${Feature.dir}/build.xml" tofile="${Feature.dir}/build1.xml" > 
     <filterchain> 
     <tokenfilter> 
       <replaceregex pattern="^[ \t]*Version[ \t]*=.*$" 
           replace="Version=1.0.0.${touch.time}"/> 
     </tokenfilter> 
      </filterchain> 
</copy> 
--> 
</target> 

Trả lời

9

Tôi sẽ sử dụng replaceregex bên trong một filterchain.

Ví dụ:

<copy file="${Feature.dir}\build.xml" tofile="${Feature.dir}\build1.xml"  
    filtering="yes" overwrite="yes"> 
    <filterchain> 
     <tokenfilter> 
      <replaceregex pattern="1.0.0.[0-9.]*" replace="1.0.0.${touch.time}"/> 
     </tokenfilter> 
    </filterchain> 
</copy> 

Nếu bạn muốn thay thế tập tin, cảm thấy tự do để sao chép vào một tập tin tạm thời và di chuyển nó trở lại.

<tempfile property="build.temp.file.name"/> 
<copy file="${Feature.dir}\build.xml" tofile="${build.temp.file.name}" ... /> 
<move file="${build.temp.file.name}" tofile="${Feature.dir}\build.xml" /> 
+0

Cảm ơn bạn đã làm việc. Nhưng chỉ một câu hỏi. Điều gì sẽ xảy ra nếu tôi muốn thực hiện thay đổi này cho cùng một tệp? Tôi đã thử: didnt làm việc – sloggers1894

+0

Không phải lo lắng. Xem câu trả lời đã chỉnh sửa của tôi. – Synesso

Các vấn đề liên quan