Java 蚂蚁脚本中的环境变量不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18714006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:24:39  来源:igfitidea点击:

Environmental variables in ant script not working

javaantenvironment-variables

提问by Maxim Kumpan

I'm trying to set up a machine-independent build environment for a Spring framework project, and my ant configuration appears to be not working. I've searched quite a bit but everyone seems to think that env.* references work out of the box. Could someone perhaps find the error of my ways?

我正在尝试为 Spring 框架项目设置独立于机器的构建环境,但我的 ant 配置似乎不起作用。我已经搜索了很多,但每个人似乎都认为 env.* 引用是开箱即用的。有人可能会发现我的方式错误吗?

The error:

错误:

bash-3.1$ ant build
Buildfile: c:\Users\mkumpan\Projects\Spring testing\build.xml

BUILD FAILED
c:\Users\mkumpan\Projects\Spring testing\build.xml:85: c:\Users\mkumpan\Projects\Spring testing${env.CATALINA_HOME}\lib does not exist.

build.xml:85:

build.xml:85:

<taskdef name="install" classname="org.apache.catalina.ant.InstallTask">
    <classpath refid="catalina-ant-classpath"/>
</taskdef>

catalina-ant-classpath reference:

catalina-ant-classpath 参考:

<path id="catalina-ant-classpath">
    <fileset dir="${appserver.lib}">
        <include name="catalina-ant.jar"/>
    </fileset>
</path>

${appserver.lib} declared in build.properties:

${appserver.lib} 在 build.properties 中声明:

appserver.home=${env.CATALINA_HOME}
appserver.lib=${appserver.home}/lib

deploy.path=${appserver.home}/webapps

Echoing the envvar works:

呼应 envvar 工作:

bash-3.1$ echo $CATALINA_HOME
C:\Program Files\Tomcat

The two big questions:

两个大问题:

  • Why the hell does it not parse out the envvar?
  • Why the hell does it prepend the absolute path to the envvar?
  • 为什么它不解析出 envvar?
  • 为什么它会在 envvar 前面加上绝对路径?

采纳答案by hmjd

Add the following line to the build.xmlfile:

将以下行添加到build.xml文件中:

<property environment="env"/>

to define the prefix when referencing environment variables. From the Propertyreference page for the environmentattribute:

在引用环境变量时定义前缀。从环境属性的属性参考页面:

the prefix to use when retrieving environment variables. Thus if you specify environment="myenv" you will be able to access OS-specific environment variables via property names "myenv.PATH" or "myenv.TERM".Note that if you supply a property name with a final "." it will not be doubled; i.e. environment="myenv." will still allow access of environment variables through "myenv.PATH" and "myenv.TERM". This functionality is currently only implemented on select platforms. Feel free to send patches to increase the number of platforms on which this functionality is supported ;). Note also that properties are case-sensitive, even if the environment variables on your operating system are not; e.g. Windows 2000's system path variable is set to an Ant property named "env.Path" rather than "env.PATH".

检索环境变量时使用的前缀。因此,如果您指定 environment="myenv",您将能够通过属性名称“myenv.PATH”或“myenv.TERM”访问特定于操作系统的环境变量。请注意,如果您提供带有最后一个“.”的属性名称。不会翻倍;即环境=“myenv。” 仍将允许通过“myenv.PATH”和“myenv.TERM”访问环境变量。此功能目前仅在特定平台上实现。随意发送补丁以增加支持此功能的平台数量;)。还要注意,属性是区分大小写的,即使操作系统上的环境变量不是;例如 Windows 2000'

回答by Gyanendra Dwivedi

I hope you are declaring <property environment="env."/>before using env.notation.

我希望你<property environment="env."/>在使用env.符号之前声明。

Also, below is the syntax in your build script to set specific environment variables.

此外,以下是构建脚本中用于设置特定环境变量的语法。

**Windows and OS/2**

Assume Ant is installed in c:\ant. The following sets up the environment:

假设 Ant 安装在 c:\ant 中。以下设置环境:

set ANT_HOME=c:\ant
set JAVA_HOME=c:\jdk-1.5.0.05
set PATH=%PATH%;%ANT_HOME%\bin

**Linux/Unix (bash)**

Assume Ant is installed in /usr/local/ant. The following sets up the environment:

假设 Ant 安装在 /usr/local/ant 中。以下设置环境:

export ANT_HOME=/usr/local/ant
export JAVA_HOME=/usr/local/jdk-1.5.0.05
export PATH=${PATH}:${ANT_HOME}/bin

**Linux/Unix (csh)**

setenv ANT_HOME /usr/local/ant
setenv JAVA_HOME /usr/local/jdk/jdk-1.5.0.05
set path=( $path $ANT_HOME/bin )

Having a symbolic link set up to point to the JVM/JDK version makes updates more seamless.

将符号链接设置为指向 JVM/JDK 版本可以使更新更加无缝。

回答by Constantinos

If anyone else is still struggling to make this work (like I did), and you can't (or would rather not) use exportfor all your properties, try set -aas suggested here

如果其他人仍在努力完成这项工作(就像我所做的那样),并且您不能(或宁愿不)使用export所有属性,请set -a按照此处的建议尝试