如何在ANT中检测windows操作系统 [英] How to detect the windows OS in ANT

查看:29
本文介绍了如何在ANT中检测windows操作系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ant 来编译 Java 应用程序.问题是一些开发人员使用 win 7,而其他开发人员使用 xp 和 vista.编译的一部分是使用WIX构建一个msi,在win7上这是一个目录,在xp和vista上它在另一个目录中.

I'm using ant to compile a Java application. The problem is some of the devs are on win 7 and others are on xp and vista. Part of the compiling is to build an msi using WIX, on win7 this is one directory and on xp and vista it's in another.

ant 任务在 Maven 中控制.我想通过一种条件标记来区分 ant 中的 Windows 操作系统之间的区别,以设置 wix 目录.有什么想法吗?

The ant task is controlled in Maven. I'm after a way of telling the difference between windows os's in ant with a conditional tag to set the wix directory. Any ideas?

我知道它将采用这种格式:

I know it will be in this format:

<if>
   <condition property="isWin7">
    Check for windows 7
   </condition>
   <then>
    set wix path to win 7 installation
   </then>
   <else>
    set to vista/xp wix installation
   </else>
 </if>

任何帮助都会很棒.

推荐答案

看起来 ANT 可以测试 name, family &操作系统版本:

It looks like the ANT <condition> can test for name, family & version of operating system:

基于该链接,我们可以查询一些与操作系统相关的属性.一种是普通代码中使用的普通family属性:

Based on that link, there are some properties related to OS that we can query. One is the normal family property used in the common code:

<!-- CHECK FOR WINDOWS FAMILY OS -->
<condition property="is_windows">
    <os family="windows"/>
</condition>

我的 ANT 版本没有打印出 ${os.family} 的解析值.

My version of ANT does not print out a resolved value for ${os.family}.

还有:

  • os.name <--- 这是您需要检查的
  • os.arch
  • 操作系统版本
  • os.name <--- this is the one you need to check
  • os.arch
  • os.version

这是我为展示这些属性的使用而制作的演示脚本:

Here's a demo script I made to show the use of these properties:

<?xml version="1.0" encoding="UTF-8"?>
<project name="Test" default="build" >

    <!-- CHECK FOR WINDOWS FAMILY OS -->
    <condition property="is_windows">
        <os family="windows"/>
    </condition>

    <condition property="is_windows_7">
        <os name="Windows 7"/>
    </condition>

    <!-- DISPLAYS WINDOWS OS -->
    <target name="display_windows" if="is_windows" >
        <echo message="OS Family is:       Windows" />
    </target>


    <target name="build" >

        <antcall target="display_windows" />

        <echo message="OS Name is:         ${os.name}" />
        <echo message="OS Architecture is: ${os.arch}" />
        <echo message="OS Version is:      ${os.version}" />

    </target>

</project>

自从回答这个问题以来,上面的代码已经被提升到我们的生产构建系统,它提供了跨 Windows & 的共享功能.麦克.

Since answering this question, the code above has been promoted to our production build system, where it is providing shared functionality across Windows & Mac.

@thekbb 提出了删除 <antcall target="display_windows"/> 的好建议.,并按照以下代码更新目标定义以依赖于 display_windows:

@thekbb made a good suggestion to remove the <antcall target="display_windows" />, and update the target definition to depend on display_windows as per the below code:

    <target name="build" depends="display_windows">

        <echo message="OS Name is:         ${os.name}" />
        <echo message="OS Architecture is: ${os.arch}" />
        <echo message="OS Version is:      ${os.version}" />

    </target>

这是基于 antcall 在新的 JVM 中启动一个新的 ant 实例的事实.一些用户可能会发现这种优化更容易理解;其他人可能出于性能原因想要这样做.

This based on the fact that antcall launches a new instance of ant in a new JVM. Some users may find this optimisation easier to understand; others may want to do this for performance reasons.

这篇关于如何在ANT中检测windows操作系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆