简体   繁体   中英

Ant propertyregex task to replace special character in string

My string is C:\\tools\\jenkins\\HOME\\workspace\\MAL1793_Driver_DIO

I want to replace windows-style directory path "\\" with UNIX style path "/"

I have used an Ant propertyregex task in my pom file to achieve this as below.

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

But after execution I am getting this error:

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.

I am using Ant version 1.7. Are there any settings missing?

The <propertyregex> task is not part of Ant, it's part of the third-party Ant-Contrib collection of Ant tasks. The error message you quote indicates that you're at least missing the <taskdef> needed to use Ant-Contrib in your buildfile.

Instructions on how to set up and use Ant-Contrib are available at 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>

You might consider using the built-in Ant <pathconvert> task as an alternative to the <propertyregex> if you don't already have Ant-Contrib or need it for anything else in your build.

I feel using ant script-javascript for this is much simpler

        <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}" />

note: that naming your variable same as argument eg var wsPath may give error, it gave to me!

courtesy: https://stackoverflow.com/a/16099717/4979331

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM