【问题标题】:Using Ant, how do I open a file in a browser?使用 Ant,如何在浏览器中打开文件?
【发布时间】:2015-08-24 22:31:29
【问题描述】:

我有一个创建 HTML 报告的 Ant 任务。是否可以通过 Ant 任务在浏览器中自动加载该报告?如果是这样,是否可以以独立于用户的方式这样做,还是需要使用自定义用户属性?

谢谢,

保罗

【问题讨论】:

    标签: ant


    【解决方案1】:

    我使用 <script> 和 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>
    

    【讨论】:

      【解决方案2】:

      有一个独立的方式,就像我们在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>
      

      这是an answerscript 任务运行。不过javascript 语言确实是一个更好的选择,因为它在 JDK 6 中不需要classpath(也不需要manager)。而且里面的代码保持不变。

      【讨论】:

        【解决方案3】:

        尝试使用 Ant 的 exec 任务执行系统命令。

        http://ant.apache.org/manual/Tasks/exec.html

        该文档中的一个示例:

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

        【讨论】:

          【解决方案4】:

          我需要一个独立于平台的解决方案,因此基于“1.21 吉瓦”的答案:

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

          这可以在 ant 中调用:

          <open file="C:\index.html" />
          

          【讨论】:

          • 为了让它与 Java 8 和 Java 7 一起工作,我必须 (a) 删除 println (并从 ant 回显路径)和 (b) 而不是 location.toString().replace("%3A",":"); 我使用了 @ 987654324@
          【解决方案5】:

          我是怎么做到的:

          在我的 build.properties 中

          #Browser
          browser = open
          browser.args = -a Firefox
          

          在我的 build.xml 中

          <target name="openCoverage">
              <exec executable="${browser}" spawn="yes">
                  <arg line="${browser.args}" />
                  <arg line="${unit.html}" />
              </exec>
          </target>
          

          【讨论】:

            【解决方案6】:

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

            我不得不将项目名称附加到目录并用“%20”替换空格。之后它工作得很好。

            【讨论】:

              【解决方案7】:

              一种方法是使用文件名调用您喜欢的浏览器。如果你有 Ant 执行

              firefox "file:///G:/Report.html"
              

              它将使用该文件启动 Firefox。

              【讨论】:

                【解决方案8】:

                这会在系统默认浏览器中仅使用纯 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>
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2019-04-12
                  • 2013-01-14
                  • 2021-03-15
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多