2016-12-20 124 views
0
替換特殊字符

我的字符串是C:\tools\jenkins\HOME\workspace\MAL1793_Driver_DIO螞蟻propertyregex任務,在串

我想與UNIX風格路徑替換Windows風格的目錄路徑"\""/"

我在POM文件中使用一個Ant propertyregex任務如下所示。

<execution> 
    <id>ReplaceWSPath</id> 
    <phase>process-resources</phase> 
    <configuration> 
    <tasks> 
     <echo>"Updating workspace path"</echo> 
     <propertyregex 
     property="WSPath" 
     input="C:\tools\jenkins\HOME\workspace\MAL1793_Driver_DIO" 
     regexp="\" 
     replace="/" 
     global="true" /> 
     <echo>"workspace Path = ${WSPath}"</echo> 
    </tasks> 
    </configuration> 
    <goals> 
    <goal>run</goal> 
    </goals> 
</execution> 

但執行後我收到此錯誤:

Problem: failed to create task or type propertyregex 
[ERROR] Cause: The name is undefined. 
[ERROR] Action: Check the spelling. 
[ERROR] Action: Check that any custom tasks/types have been declared. 
[ERROR] Action: Check that any <presetdef>/<macrodef> declarations have taken place. 

我使用Ant 1.7版本。是否缺少任何設置?

+0

你可以分享你的最終Maven配置?謝謝! –

回答

1

<propertyregex>任務不是Ant的一部分,它是第三方Ant-Contrib Ant任務集合的一部分。您引用的錯誤消息表明您至少錯過了在構建文件中使用Ant-Contrib所需的<taskdef>。關於如何設置和使用Ant-的Contrib

說明可在SourceForge

First you must install Apache Ant itself, most of the Ant-Contrib tasks require Ant 1.5 or higher to work properly. You can download Ant from Apache .

Ant-contrib releases are available at the downloads page. Mailing lists, CVS and bug trackers can be accessed from the project page.

See the cc tasks for installation instructions for cpptasks. To install ant-contrib:

  1. Copy ant-contrib-0.3.jar to the lib directory of your Ant installation. If you want to use one of the tasks in your own project, add the lines

    <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>

to your build file.

  1. Keep ant-contrib-0.3.jar in a separate location. You now have to tell Ant explicitly where to find it (say in /usr/share/java/lib):

    <taskdef resource="net/sf/antcontrib/antcontrib.properties"> <classpath> <pathelement location="/usr/share/java/lib/ant-contrib-0.3.jar"/> </classpath> </taskdef>

可以考慮使用內置的Ant任務<pathconvert>作爲替代<propertyregex>,如果您還沒有有Ant-Contrib或需要它在你的構建中的其他任何東西。

0

我覺得使用ant腳本的JavaScript因爲這是更簡單

 <property name="wsPath" value="C:\tools\jenkins\HOME\workspace\MAL1793_Driver_DIO" /> 
     <script language="javascript"> 
      var wsPath_BackSlash = project.getProperty("wsPath"); 
      println("before: " + wsPath_BackSlash); 
      var wsPath_FrwdSlash= wsPath_BackSlash.replace("\\", "/"); 
      println("wsPath_FrwdSlash: "+wsPath_FrwdSlash); 
      project.setProperty("wsPath", wsPath_FrwdSlash);     
     </script> 
     <echo message="${wsPath}" /> 

注:在命名變量相同的參數e.g VAR wsPath可能會錯誤,它給了我!

禮貌:https://stackoverflow.com/a/16099717/4979331