Using Ant, how do I open a file in a browser?
I have an Ant task that creates an HTML report. Is it possible to load that report automatically in a browser from the Ant task? If so, is it possible to do so in a user-independent way or would 开发者_StackOverflowit require the use of custom user properties?
Thanks,
Paul
I used <script>
with 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>
There is an independent way, like we do in java:
Desktop.getDesktop.open(new File("file.html")) ?
I see no exit without ant optional tasks. From all the scripts beanshell looks most lightweight and does not require any new knowledge. So I did it this way:
<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>
And this is an answer about getting script
task running. However javascript
language is indeed a better option, as it needs no classpath
(and no manager
) in JDK 6. And the code inside remains the same.
Try using Ant's exec
task to execute a system command.
http://ant.apache.org/manual/Tasks/exec.html
An example from that document:
<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>
I need a solution that is platform independent, so based upon "1.21 gigawatts" answer:
<scriptdef name="open" language="javascript">
<attribute name="file" />
<![CDATA[
var location = "file://"+attributes.get("file").toString().replaceAll("\\\\","/");
location = java.net.URLEncoder.encode(location, "UTF-8");
location = location.toString().replace("%3A",":");
location = location.toString().replace("%2F","/");
println("Opening file " + location);
var uriLocation = java.net.URI.create(location);
var desktop = java.awt.Desktop.getDesktop();
desktop.browse(uriLocation);
]]>
</scriptdef>
This can be called in ant with:
<open file="C:\index.html" />
How I did it:
In my build.properties
#Browser
browser = open
browser.args = -a Firefox
In my build.xml
<target name="openCoverage">
<exec executable="${browser}" spawn="yes">
<arg line="${browser.args}" />
<arg line="${unit.html}" />
</exec>
</target>
Basing this on Gabor's answer I had to do a few more things to get it to work. Here's my code:
<!-- 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>
I had to append the project name to the directory and replace the spaces with "%20". After that it worked fine.
One way to do this is to invoke your favorite browser with the filename. If you have Ant execute
firefox "file:///G:/Report.html"
it will launch Firefox with that file.
This opens the given HTML file in the system-default browser using only pure Ant.
<property name="report.file" value="${temp.dir}/report/index.html" />
<exec executable="cmd" spawn="yes">
<arg value="/c" />
<arg value="${report.file}" />
</exec>
精彩评论