JavaScript +蚂蚁问题
正在处理 ANT 构建文件,其中定义了两个宏,需要从另一个宏调用一个宏。
这是我需要做的(为了便于理解,我简化了整个场景)。我的主要目标使用两个参数调用 FirstMacro。第一个宏需要使用收到的两个参数执行“某些操作”并对 SecondMacro 进行嵌入调用。
传递给 FirstMacro 的参数之一是其中包含“反斜杠”() 的文件路径。当我使用 echo 在宏中打印它时,它打印得很好。但是,当我通过 JavaScript 将相同的参数传递给 SecondMacro 时,反斜杠消失了(我看到的是 C:Test,而不是 C:\Test)。 保留反斜杠或用正斜杠替换它们的解决方案是什么?
请注意,我正在使用 ANT 1.7,并且我已经尝试过 string.replace(/\/g,"/") 等。
另请注意,您可以复制粘贴下面的整个代码,将其另存为 build.xml 并尝试运行主要任务是查看运行中的问题。
<?xml version="1.0"?>
<project name="build" basedir="." default="main">
<description>
==========================
Macro: Second Macro
==========================
</description>
<macrodef name="secondMacro">
<attribute name="param"/>
<sequential>
<echo>secondMacro param: @{param}</echo>
</sequential>
</macrodef>
<description>
==========================
Macro: FirstMacro
==========================
</description>
<macrodef name="firstMacro">
<attribute name="param1"/>
<attribute name="param2"/>
<sequential>
<echo>firstMacro first param: @{param1}</echo>
<echo>firstMacro second param: @{param2}</echo>
<script language="javascript">
<![CDATA[
var ext = "@{param2}";
if ("".equals(ext)) {
ext = "out";
}
macrotask = project.createTask("secondMacro");
macrotask.setDynamicAttribute("param", "@{param1}" + "."+ext );
macrotask.perform();
]]>
</script>
</sequential>
</macrodef>
<description>
==========================
Target: main target
==========================
</description>
<target name="main">
<firstMacro param1="C:\TestFolder/TestFile" param2=""/>
</target>
</project>
working on an ANT build file, where I have two macros defined and need to call one Macro from other.
Here is what I need to do (I have simplified the whole scenario for easy understanding). My main target calls FirstMacro with two params. The First Macro needs to do "something" with two params received and make an embedded call to SecondMacro.
One of the params passed to FirstMacro is a file path with "backslash" () in it. When I print it in the macro using echo it prints fine. But when I pass the same parameter to SecondMacro via JavaScript, the backslashes are gone (instead of C:\Test I see C:Test).
What is the solution to preserve the backslash or replace them with forward slash.
Note that I am using ANT 1.7 and I have already tried string.replace(/\/g,"/") etc.
Also note that, you can copy paste the entire code below, save it as build.xml and try running the main task to see the issue in running.
<?xml version="1.0"?>
<project name="build" basedir="." default="main">
<description>
==========================
Macro: Second Macro
==========================
</description>
<macrodef name="secondMacro">
<attribute name="param"/>
<sequential>
<echo>secondMacro param: @{param}</echo>
</sequential>
</macrodef>
<description>
==========================
Macro: FirstMacro
==========================
</description>
<macrodef name="firstMacro">
<attribute name="param1"/>
<attribute name="param2"/>
<sequential>
<echo>firstMacro first param: @{param1}</echo>
<echo>firstMacro second param: @{param2}</echo>
<script language="javascript">
<![CDATA[
var ext = "@{param2}";
if ("".equals(ext)) {
ext = "out";
}
macrotask = project.createTask("secondMacro");
macrotask.setDynamicAttribute("param", "@{param1}" + "."+ext );
macrotask.perform();
]]>
</script>
</sequential>
</macrodef>
<description>
==========================
Target: main target
==========================
</description>
<target name="main">
<firstMacro param1="C:\TestFolder/TestFile" param2=""/>
</target>
</project>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
印刷
应该是你的JS代码的问题。我对 JS 知之甚少,但反斜杠在大多数语言中意味着“这是一个转义字符”。所以当JS看到单个反斜杠时,它可能会将其视为转义字符并进行一些转换... 即使你使用beanshell或其他东西,也可能会发生类似的事情。
要解决这个问题,你可以尝试修改你的JS代码(抱歉,我无能为力)或尝试使用斜杠(/)而不是反斜杠。
更新:您可以使用 ant-contrib 中的 if 任务。
这是 if-task 版本“firstMacro”的示例。
prints
It should be your JS code's problem. I know little about JS, but backslash means "this is an escape character" in most languages. So when JS see a single backslash, it may regard it as an escape character and do some convertion... Even if you use beanshell or something else, similar thing may happen.
To solve the problem, you can try to modify your JS code (which I can't help, sorry) or try to use slash (/) instead of backslash.
Update: you can use the if task from ant-contrib.
Here is the example of the if-task version "firstMacro".