Ant propertyregex任务替换字符串中的特殊字符

问题描述 投票:0回答:2

我的字符串是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版。有没有设置丢失?

maven ant
2个回答
1
投票

<propertyregex>任务不是Ant的一部分,它是第三方Ant-Contrib Ant任务集合的一部分。您引用的错误消息表明您至少缺少在构建文件中使用Ant-Contrib所需的<taskdef>

有关如何设置和使用Ant-Contrib的说明,请访问SourceForge

首先,您必须安装Apache Ant,大多数Ant-Contrib任务都需要Ant 1.5或更高版本才能正常工作。你可以从Apache下载Ant。

可以在downloads页面上找到Ant-contrib版本。可以从project页面访问邮件列表,CVS和错误跟踪器。

有关cpptasks的安装说明,请参阅cc任务。要安装ant-contrib:

  1. 将ant-contrib-0.3.jar复制到Ant安装的lib目录中。如果要在自己的项目中使用其中一个任务,请添加行 <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>

到您的构建文件。

  1. 将ant-contrib-0.3.jar放在一个单独的位置。你现在必须明确地告诉Ant在哪里找到它(比如在/ 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 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可能会给出错误,它给了我!

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

© www.soinside.com 2019 - 2024. All rights reserved.