Ant -contrib: problems with outofdate

I have a very similar situation, as in the example outofdate

(see gengrammer

, http://ant-contrib.sourceforge.net/tasks/tasks/outofdate.html ): I have a folder with files *.class

and *.seq

that need to be converted to *.pdf

using the program plantuml

(a java

).

This is my current task:

<property name="diagram.path" value="../graphics/plantuml/src"/>

...

<target name="diagram-files" depends="update-classpath">
    <outofdate property="manual.outofdate" outputsources="diagram.sources">
        <sourcefiles>
            <fileset dir="${diagram.path}" includes="*.class"/>
            <fileset dir="${diagram.path}" includes="*.seq"/>
        </sourcefiles>
        <mapper type="glob" dir="${diagram.path}" from="*.class" to="graphics/*.pdf"/>
        <mapper type="glob" dir="${diagram.path}" from="*.seq" to="graphics/*.pdf"/>
        <sequential>
            <shellscript shell="bash">
                cd ${diagram.path}
                echo ${diagram.sources}
                #for diagram in ${diagram.sources}
                #do
                # java -jar plantuml ... $diagram
                #done
            </shellscript>
        </sequential>
    </outofdate>
</target>

      

I can't get this to work because internally ${diagram.sources}

all (back) forward slashes have been removed. So repeating this variable gives me something like this:

C:UsersMYUSERpath-to-folderANDsoONmyfile.class
C:UsersMYUSERpath-to-folderANDsoONmyfile2.class

      

I don't know if I did something wrong or if this is a feature. Any help would be great!

by the way. I'm on Windows 10, but ant is running inside the cygwin shell. I have never had any problems with this combination.

+3


source to share


1 answer


It is not clear if the backslashes were flushed by Ant tasks outofdate

before execution reached your bash script, or if the backslash turned into a script and you see shell escape effects.

If it is the latter, then you can fix the problem by wrapping ${diagram.sources}

in quotes so that the shell interprets it as an unescaped string literal. For example:

build.xml

<project>
    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
        <classpath>
            <pathelement location="/Users/naurc001/ant-contrib-0.6-bin/lib/ant-contrib-0.6.jar"/>
        </classpath>
    </taskdef>
    <property name="diagram.path" value="../graphics/plantuml/src"/>
    <property name="diagram.sources.property" value="C:\private\tmp\anttest\graphics\plantuml\src\myfile.class C:\private\tmp\anttest\graphics\plantuml\src\myfile2.class" />
    <target name="diagram-files">
        <outofdate property="manual.outofdate" outputsources="diagram.sources">
            <sourcefiles>
                <fileset dir="${diagram.path}" includes="*.class"/>
                <fileset dir="${diagram.path}" includes="*.seq"/>
            </sourcefiles>
            <mapper type="glob" dir="${diagram.path}" from="*.class" to="graphics/*.pdf"/>
            <mapper type="glob" dir="${diagram.path}" from="*.seq" to="graphics/*.pdf"/>
            <sequential>
                <shellscript shell="bash">
                    echo Unquoted: ${diagram.sources.property}
                    echo Quoted: '${diagram.sources.property}'
                    for diagram in $(echo '${diagram.sources.property}')
                    do
                        # I don't have plantuml, so just stubbing it to print the command.
                        echo java -jar plantuml ... $diagram
                    done
                </shellscript>
            </sequential>
        </outofdate>
    </target>
</project>

      

Output

> ant diagram-files
Buildfile: /private/tmp/anttest/proj/build.xml

diagram-files:
[shellscript] Unquoted: 
C:privatetmpanttestgraphicsplantumlsrcmyfile.class 
C:privatetmpanttestgraphicsplantumlsrcmyfile2.class
[shellscript] Quoted: 
C:\private\tmp\anttest\graphics\plantuml\src\myfile.class 
C:\private\tmp\anttest\graphics\plantuml\src\myfile2.class
[shellscript] java -jar plantuml ... 
/private/tmp/anttest/graphics/plantuml/src/myfile.class
[shellscript] java -jar plantuml ... 
/private/tmp/anttest/graphics/plantuml/src/myfile2.class

BUILD SUCCESSFUL
Total time: 0 seconds

      

Description



I don't have access to a windows machine to directly test on that platform, so I simulated the problem by hardcoding mine diagram.sources.property

to use Windows style backslash windows. <shellscript>

repeats the same property twice: once without single quotes and once with single quotes. For the unquoted form, the output looks like you described because of the shell escaping. After quoting, we will get the expected results.

However, if we just passed on to '{diagram.sources.property}'

other commands like jar ...

, then we would have an unintended side effect. bash will pass the entire list as a single argument. To fix this, we can wrap the whole thing in $(echo ...)

to return it in multiple arguments. The test result shows that we are calling the command jar

once for each file.

Depending on which tools you need to run, sometimes you need to convert the Unix-style backslash path separators to Unix backslashes. If the need arises, I recommend using a utility cygpath

inside a bash script to convert backslashes to slashes. In particular, the options -u

and are useful here -w

.

The -u and -w options indicate whether you want to convert to UNIX format (POSIX) (-u) or Windows format (-w). Use -d to get DOS-style filenames and paths (8.3). The -m option outputs a Windows-style format, but with a forward slash instead of a backslash. This option is especially useful in shell scripts that use a backslash escape character.

You mentioned that you are using Cygwin, but perhaps you also have teammates working on Mac or straight Linux and need to interact with them. If so, then you can protect the calls cygpath

inside the check from the value uname -a

. In the Cygwin shell, the output uname -a

will contain some form of "Cygwin".

Additional links to bash escaping:

+1


source







All Articles