简体   繁体   中英

Ant build fails not expanding out environment variables

I have a problem with the build process on a new server that I have setup just for development purposes.

This is running on a Centos 6 box with Java/Tomcat v7. The output is as follows:

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.

Other responses on SO suggest that the environment variable isn't created but If I run the following:

echo $TOMCAT_HOME

it returns:

/usr/local/tomcat7

So I know that the variable exists and is set.

This is the build.xml file:

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

Any advice on how to fix?

My guess is that Ant does not "see" TOMCAT_HOME because the system process in which Ant has been called has not that variable defined on it.

Ensure you are calling Ant from the same system user that has a TOMCAT_HOME variable in its profile, and also that the environment variables are preserved when launching Ant.

Not sure if this is the best solution but I changed this line in the build.xml

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

to point to the location of tomcat:

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

And that did the trick.

Should the line have read something like:

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

to match the environment variable?

You have temporarily solved the build issue by assigning the hard-coded value dir="/usr/local/tomcat7/lib" , but that is not a permanent fix. If you format your machine and the path changes, or if you share this code with someone who might have a different tomcat installation path, it will be a problem. The real culprit in your ant script is this line of code:

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

You see, the location attribute is used in ant properties if you want to specify a relative path . You have specified location attribute so ant build is searching for tomcat installation relative to your current path desperately (and thus fails):

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

The solution here is to specify a value attribute instead of location since your $TOMCAT_HOME refers to an absolute path in /usr/local :

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

Just add my 0.02$, I've experienced this issue because I had a typo in the var names passed to mkdir , and consequently when mkdir was executed then as there was no var to expand then it just created a dir matching the variable name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM