7

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 it require the use of custom user properties?

Thanks,

Paul

Paul
  • 19,704
  • 14
  • 78
  • 96

8 Answers8

8

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>
arrowd
  • 33,231
  • 8
  • 79
  • 110
Gabor Imre
  • 81
  • 1
  • 1
7

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.

Community
  • 1
  • 1
Jarekczek
  • 7,456
  • 3
  • 46
  • 66
6

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>
Shaun
  • 4,789
  • 3
  • 22
  • 27
5

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" />
Matt Gumbel
  • 51
  • 1
  • 3
  • In order to get this to work with Java 8 and Java 7 I had to (a) remove println (and echo the path from ant instead) and (b) instead of `location.toString().replace("%3A",":");` I used `location.toString().replaceAll("%3A",":");` – Clintm Sep 19 '16 at 22:46
4

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>
Paul Peelen
  • 10,073
  • 15
  • 85
  • 168
3

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.

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
2

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.

rajah9
  • 11,645
  • 5
  • 44
  • 57
1

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>
Natix
  • 14,017
  • 7
  • 54
  • 69