使用Ant,如何在浏览器中打开文件?
我有一个创建HTML报告的Ant任务。是否可以从Ant任务在浏览器中自动加载该报告?如果是,是否可以以独立于用户的方式执行此操作,或者是否需要使用自定义用户属性 谢谢使用Ant,如何在浏览器中打开文件?,ant,Ant,我有一个创建HTML报告的Ant任务。是否可以从Ant任务在浏览器中自动加载该报告?如果是,是否可以以独立于用户的方式执行此操作,或者是否需要使用自定义用户属性 谢谢 Paul尝试使用Ant的exec任务来执行系统命令 该文件中的一个例子: <property name="browser" location="C:/Program Files/Internet Explorer/iexplore.exe"/> <property name="file" location="a
Paul尝试使用Ant的
exec
任务来执行系统命令
该文件中的一个例子:
<property name="browser" location="C:/Program Files/Internet Explorer/iexplore.exe"/>
<property name="file" location="ant/docs/manual/index.html"/>
<exec executable="${browser}" spawn="true">
<arg value="${file}"/>
</exec>
一种方法是使用文件名调用您喜爱的浏览器。如果你有蚂蚁执行
firefox "file:///G:/Report.html"
它将启动带有该文件的Firefox。有一种独立的方式,就像我们在java中所做的那样:
Desktop.getDesktop.open(new File("file.html")) ?
如果没有ant可选任务,我看不到任何出口。从所有脚本来看,beanshell看起来最轻量级,不需要任何新知识。所以我是这样做的:
<property name="bshJar" value="
C:\lang\java\bsh-1.3.0.jar:
C:\lang\java\bsf.jar:
C:\lang\java\commons-logging-1.1.1.jar" />
<script manager="bsf" language="beanshell" classpath="${bshJar}">
java.awt.Desktop.getDesktop().open(
new java.io.File("c:\\temp\\1\\stackoverflow\\DVD FAQ.htm"));
</script>
java.awt.Desktop.getDesktop().open(
新的java.io.File(“c:\\temp\\1\\stackoverflow\\DVD FAQ.htm”);
这是关于运行script
任务的。然而javascript
语言确实是一个更好的选择,因为它在jdk6中不需要classpath
(也不需要manager
)。里面的代码保持不变。我是如何做到的:
在我的build.properties中
#Browser
browser = open
browser.args = -a Firefox
在my build.xml中
<target name="openCoverage">
<exec executable="${browser}" spawn="yes">
<arg line="${browser.args}" />
<arg line="${unit.html}" />
</exec>
</target>
我在javascript中使用了
:
<property name="mydirectory" location="target/report"/>
<script language="javascript"><![CDATA[
location = "file:///"+project.getProperty("mydirectory").toString().replaceAll("\\\\","/")+"/index.html";
java.awt.Desktop.getDesktop().browse(java.net.URI.create(location));
]]></script>
基于Gabor的回答,我不得不做更多的事情来让它发挥作用。这是我的密码:
<!-- Build and output the Avenue.swf-->
<target name="Open in browser" >
<property name="myDirectory" location="BuildTest/bin-debug"/>
<script language="javascript">
<![CDATA[
var location = "file:///"+project.getProperty("myDirectory").toString().replaceAll("\\\\","/")+"/BuildTest.html";
location = location.toString().replace(/ /g, "%20");
// show URL - copy and paste into browser address bar to test location
println(location);
var uriLocation = java.net.URI.create(location);
var desktop = java.awt.Desktop.getDesktop();
desktop.browse(uriLocation);
]]>
</script>
</target>
我需要一个独立于平台的解决方案,因此基于“1.21千兆瓦”的答案:
这将仅使用纯Ant在系统默认浏览器中打开给定的HTML文件
<property name="report.file" value="${temp.dir}/report/index.html" />
<exec executable="cmd" spawn="yes">
<arg value="/c" />
<arg value="${report.file}" />
</exec>
为了让它在Java8和Java7中工作,我必须(a)删除println(并从ant中回显路径)和(b)而不是location.toString().replace(“%3A”,“:”)代码>我使用了location.toString().replaceAll(“%3A”,“:”)代码>
<open file="C:\index.html" />
<property name="report.file" value="${temp.dir}/report/index.html" />
<exec executable="cmd" spawn="yes">
<arg value="/c" />
<arg value="${report.file}" />
</exec>