Ant构建无法扩展环境变量



我在为开发目的而设置的新服务器上的构建过程中遇到了问题。

这是在带有Java/Tomcatv7的Centos 6盒子上运行的。输出如下:

Buildfile: /home/ec2-user/twv/trunk/EES/build.xml
init:
     [echo] Build of EES started at 1332 on December 19 2015
clean:
     [echo] Cleaning EES
   [delete] Deleting directory /home/ec2-user/twv/trunk/EES/build
compile:
     [echo] Compiling EES
    [mkdir] Created dir: /home/ec2-user/twv/trunk/EES/build/WEB-INF/classes
    [javac] Compiling 78 source files to /home/ec2-user/twv/trunk/EES/build/WEB-INF/classes
BUILD FAILED
/home/ec2-user/twv/trunk/EES/build.xml:118: /home/ec2-user/twv/trunk/EES/${currentenv.TOMCAT_HOME}/lib does not exist.

SO上的其他响应表明没有创建环境变量,但如果我运行以下内容:

echo $TOMCAT_HOME

它返回:

/usr/local/tomcat7

所以我知道这个变量是存在的并且是设置的。

这是build.xml文件:

<?xml version="1.0"?>
<!-- #######################################################################
                        B u i l d   S c r i p t
####################################################################### -->
<project name="EES" default="war">
        <description> EES </description>
        <property environment="currentenv" />
        <property name="tomcat.home" location="${currentenv.TOMCAT_HOME}"/>
        <property file="build.${currentenv.ANT_HOST_NAME}" />
        <property name="webapp.path" value="build"/>
<!-- #######################################################################
                T a r g e t : I n i t
####################################################################### -->
  <target name="init">
    <tstamp/>
    <echo>Build of ${ant.project.name} started at ${TSTAMP} on ${TODAY}</echo>
  </target>

<!-- #######################################################################
                T a r g e t : D e f a u l t
####################################################################### -->
        <target name="war" depends="init, clean, compile,compilejsp,copywebroot" description="Bundle up everything">
        <!-- target name="war" depends="init, clean,compile,copywebroot" description="Bundle up everything" -->
                <echo>Packaging ${ant.project.name}</echo>
                <jar destfile="EES.war" basedir="${webapp.path}"/>
                <echo>Finished packaging ${ant.project.name}</echo>
        </target>
<!-- #######################################################################
                T a r g e t : C o p y w e b r o o t
####################################################################### -->
        <target name="copywebroot">
                <copy todir="${webapp.path}" >
                        <fileset dir="WebRoot">
                               <include name="**"/>
                        </fileset>
                </copy>
        </target>
<!-- #######################################################################
                T a r g e t : j s p c
####################################################################### -->
        <target name="jspc">
                <taskdef classname="org.apache.jasper.JspC" name="jasper2" >
                        <classpath id="jspc.classpath">
                                <pathelement location="${java.home}/../lib/tools.jar"/>
                                <fileset dir="${tomcat.home}/bin">
                                        <include name="*.jar"/>
                                </fileset>
                                <fileset dir="${tomcat.home}/lib">
                                        <include name="*.jar"/>
                                </fileset>
                                <fileset dir="${tomcat.home}/lib">
                                        <include name="*.jar"/>
                                </fileset>
                                <fileset dir="${ant.home}/lib">
                                        <include name="ant.jar"/>
                                </fileset>
                        </classpath>
                </taskdef>
                <mkdir dir="${webapp.path}/WEB-INF/src"/>
<!--            <jasper2 verbose="255"
                     validateXml="false"
                     uriroot="${webapp.path}"
                     webXmlFragment="${webapp.path}/WEB-INF/generated_web.xml"
                     outputDir="${webapp.path}/WEB-INF/src" /> -->
                 <java classname="org.apache.jasper.JspC"
                          classpathref="jspc.classpath"
                          fork="true" failonerror="true">
                        <arg line="-v -d ${webapp.path}/WEB-INF/src -webapp ${webapp.path}"/>
                    </java>
        </target>
        <target name="compilejsp" depends="jspc">
                <mkdir dir="${webapp.path}/WEB-INF/classes"/>
                <mkdir dir="${webapp.path}/WEB-INF/lib"/>
                <javac destdir="${webapp.path}/WEB-INF/classes"
                        optimize="off"
                        debug="on" failonerror="false"
                        srcdir="${webapp.path}/WEB-INF/src"
                        excludes="**/*.smap">
                        <classpath>
                                <pathelement location="${webapp.path}/WEB-INF/classes"/>
                                <fileset dir="${webapp.path}/WEB-INF/lib">
                                        <include name="*.jar"/>
                                </fileset>
                                <pathelement location="${tomcat.home}/lib"/>
                                <fileset dir="${tomcat.home}/lib">
                                        <include name="*.jar"/>
                                </fileset>
                                <fileset dir="${tomcat.home}/bin">
                                        <include name="*.jar"/>
                                </fileset>
                        </classpath>
                        <include name="**" />
                        <exclude name="tags/**" />
                </javac>
        </target>
<!-- #######################################################################
                T a r g e t : C o m p i l e
####################################################################### -->
        <target name="compile">
                <echo>Compiling ${ant.project.name}</echo>
                <mkdir dir="${webapp.path}/WEB-INF/classes"/>
                <javac includeAntRuntime="false" srcdir="src"
                                debug="on"
                                destdir="${webapp.path}/WEB-INF/classes">
                        <!--destdir="${webapp.path}/WEB-INF/classes">-->
                        <classpath>
                                <fileset dir="WebRoot/WEB-INF/lib">
                                        <include name="**/*.jar"/>
                                </fileset>
                                <fileset dir="${tomcat.home}/lib">
                                        <include name="**/*.jar"/>
                                </fileset>
                        </classpath>
                </javac>
                <copy file="src/in.xls"
                          tofile="${webapp.path}/WEB-INF/classes/com/xxx/ees/action/in.xls">
                </copy>
        <copy todir="${webapp.path}/WEB-INF/classes">
            <fileset dir="src">
                <include name="**"/>
                <exclude name="**/*.java"/>
                <exclude name="**/*.class"/>
            </fileset>
        </copy>
                         <echo>Finished compiling ${ant.project.name}</echo>
        </target>

        <!-- =================================
          target: deploy
         ================================= -->
    <target name="deploy" depends="war" description="--> deploys the war file locally">
        <copy todir="${tomcat.home}/webapps"  file="EES.war"/>
    </target>
        <target name="remotedeploy" depends="war">
                <scp keyfile="${user.home}/.ssh/id_dsa" passphrase="" file="EES.war"  remoteTodir="x@y.com.com:" verbose="true"/>
                <sshexec keyfile="${user.home}/.ssh/id_dsa" host="x.y.com" username="xxx"  command="/home/xxx/deployees"/>
        </target>
<!-- #######################################################################
                T a r g e t : C l e a n
####################################################################### -->
        <target name="clean">
                <echo>Cleaning ${ant.project.name}</echo>
                <delete dir="${webapp.path}" >
                </delete>
        </target>
</project>

有关于如何修复的建议吗?

我的猜测是Ant没有"看到"TOMCAT_HOME,因为在其中调用Ant的系统进程上没有定义该变量。

确保从配置文件中有TOMCAT_HOME变量的同一系统用户调用Ant,并且在启动Ant时保留环境变量。

不确定这是否是最好的解决方案,但我在build.xml 中更改了这一行

<fileset dir="${tomcat.home}/lib">

指向tomcat的位置:

<fileset dir="/usr/local/tomcat7/lib">

这就成功了。

这行应该读成这样吗:

<fileset dir="${TOMCAT_HOME}/lib">

以匹配环境变量?

您通过分配硬编码值dir="/usr/local/tomcat7/lib"暂时解决了构建问题,但这不是一个永久性的解决方案。如果您格式化了您的机器,但路径发生了更改,或者如果您与可能有不同tomcat安装路径的人共享此代码,这将是一个问题。ant脚本中真正的罪魁祸首是这行代码:

<property name="tomcat.home" location="${currentenv.TOMCAT_HOME}"/>

您可以看到,如果要指定相对路径,location属性将在ant属性中使用。您已经指定了location属性,因此ant build正在拼命搜索相对于当前路径的tomcat安装(因此失败):

/home/ec2-user/twv/trunk/EES/${currentenv.TOMCAT_HOME}/lib

这里的解决方案是指定value属性而不是location,因为您的$TOMCAT_HOME指的是/usr/local:中的绝对路径

<property name="tomcat.home" value="${currentenv.TOMCAT_HOME}"/>

只需添加我的0.02$,我就遇到了这个问题,因为我在传递给mkdir的var名称中有一个拼写错误,因此当执行mkdir时,由于没有var可扩展,它只创建了一个与变量名称匹配的dir。