java - ant-contrib:过时的问题

标签 java ant slash ant-contrib plantuml

我的情况与 outofdate 示例非常相似(请参阅 gengrammerhttp://ant-contrib.sourceforge.net/tasks/tasks/outofdate.html):我有一个包含 *.class< 的文件夹*.seq 文件应在 plantuml 的帮助下转换为 *.pdf(一个 java程序)。

这是我当前的任务:

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

我无法让它运行,因为在 ${diagram.sources} 中,所有(反)斜杠都被删除了。所以回应这个变量给了我这样的东西:

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

不知道是我做错了什么还是这是一个功能。任何帮助都会很棒!

顺便说一句。我在 Windows 10 上,但 ant 在 cygwin shell 中运行。我从来没有遇到过这种组合的问题。

最佳答案

不清楚反斜杠是否被 Ant 删除了 outofdate执行前的任务到达您的 bash 脚本,或者如果反斜杠进入脚本并且您看到了 shell 转义的效果。

如果是后者,那么把${diagram.sources}包裹起来或许就能解决问题在引号中,以便 shell 将其解释为字符串文字而不进行转义。例如:

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>

输出

> 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

解释

我无法访问 Windows 机器以在该平台上进行直接测试,因此我通过对 diagram.sources.property 进行硬编码来模拟该问题。在路径中使用 Windows 风格的反斜杠。 <shellscript>回显相同的属性两次:一次不带单引号,一次带单引号。对于未引用的形式,由于 shell 转义,输出看起来像您所描述的那样。引用后,我们得到了预期的结果。

但是,如果我们刚刚通过 '{diagram.sources.property}'到其他命令,如 jar ... ,那么我们就会产生意想不到的副作用。 Bash 会将整个列表作为单个参数传递。为了解决这个问题,我们可以将整个内容包装在 $(echo ...) 中。将其转回多个参数。测试输出证明我们调用了 jar对每个文件执行一次命令。

根据您需要运行的工具,有时需要将 Windows 风格的反斜杠路径分隔符转换为 Unix 风格的正斜杠。如果出现这种需要,那么我建议使用 cygpath bash 脚本中用于将反斜杠转换为斜杠的实用程序。特别是 -u-w选项在这里很有帮助。

The -u and -w options indicate whether you want a conversion to UNIX (POSIX) format (-u) or to Windows format (-w). Use the -d to get DOS-style (8.3) file and path names. The -m option will output Windows-style format but with forward slashes instead of backslashes. This option is especially useful in shell scripts, which use backslashes as an escape character.

您提到您正在使用 Cygwin,但也许您也有队友在 Mac 或纯 Linux 上运行,您需要与他们互操作。如果是这样,那么您可以保护对 cygpath 的调用。在检查 uname -a 的值中.在 Cygwin shell 中,uname -a 的输出将包含某种形式的“Cygwin”。

关于 bash 转义的其他引用资料:

关于java - ant-contrib:过时的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45239440/

相关文章:

java - Camel - 将特定参数从路由传递到通用 bean 方法

java - 为什么 ant 每次运行都要编译所有的类?

java - Ant 在通过 Jenkins 运行时给出 "Unsupported major.minor version 52.0"

php -/file.php 到/file/试试

c# - 女士访问数据库

java - 如何使用 Spring Framework 发送邮件

java - 最大 <jsp :include> depth

java - 由于 continue,for 循环数组中的基本 else 语句被忽略

shell - 如何为通过 shell 脚本调用的 Ant 脚本传递参数?

php - 跨操作系统中的 Web 斜杠问题