Ant propertyregex 任务替换字符串中的特殊字符
Ant propertyregex task to replace special character in string
我的字符串是C:\tools\jenkins\HOME\workspace\MAL1793_Driver_DIO
我想用 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 版。是否缺少任何设置?
<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:
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.
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-Contrib 或构建中的其他任何东西都需要它,您可以考虑使用内置的 Ant <pathconvert>
任务来替代 <propertyregex>
。
我觉得用ant script-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}" />
注意:将您的变量命名为与参数相同的名称,例如 var wsPath 可能会出错,它给了我!
礼貌:
我的字符串是C:\tools\jenkins\HOME\workspace\MAL1793_Driver_DIO
我想用 UNIX 风格的路径 "/"
"\"
我在我的 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 版。是否缺少任何设置?
<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:
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.
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-Contrib 或构建中的其他任何东西都需要它,您可以考虑使用内置的 Ant <pathconvert>
任务来替代 <propertyregex>
。
我觉得用ant script-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}" />
注意:将您的变量命名为与参数相同的名称,例如 var wsPath 可能会出错,它给了我!
礼貌: