-1

I have been adapting this suggested code for making a war file, and have created the following contents of the BuildWar.xml file show in the image above:

<?xml version="1.0" encoding="UTF-8"?>

<project name="myproject" default="default">
    <target name="default" depends="setup,compile,buildwar,deploy"></target>

    <target name="setup">
        <mkdir dir="dist" />
        <echo>Copying web into dist</echo>
        <copydir dest="dist/web" src="web" />
        <copydir dest="dist/web/WEB-INF/lib" src="${basedir}/../web/WEB-INF/lib" />
    </target>

    <target name="compile">
        <delete dir="${dist.dir}/web/WEB-INF/classes" />
        <mkdir dir="${dist.dir}/web/WEB-INF/classes" />
        <javac destdir="${dist.dir}/web/WEB-INF/classes" srcdir="src">
            <classpath>
                <fileset dir="${basedir}/myapp/web/WEB-INF/lib">
                    <include name="*" />
                </fileset>
            </classpath>
        </javac>
        <copy todir="${dist.dir}/web/WEB-INF/classes">
            <fileset dir="src">
                <include name="**/*.properties" />
                <include name="**/*.xml" />
            </fileset>
        </copy>
    </target>

    <target name="buildwar">
        <war basedir="${basedir}/dist/web" destfile="My.war"
             webxml="${basedir}/dist/web/WEB-INF/web.xml">
            <exclude name="WEB-INF/**" />
            <webinf dir="${basedir}/dist/web/WEB-INF/">
                <include name="**/*.jar" />
            </webinf>
        </war>
    </target>

    <target name="deploy">
        <copy file="My.war" todir="${tomcat.deploydir}" />
    </target>

</project>

Running the above code as an ant buildfile in eclipse produces some warnings that copydir has been deprecated, and also produces the following error message:

BUILD FAILED
C:\mypath\myapp\BuildWar.xml:10: srcdir C:\mypath\web\WEB-INF\lib does not exist!

However, when I replace the .. in line 10 with myapp, I get an error message saying that

BUILD FAILED
C:\mypath\myapp\BuildWar.xml:10: srcdir C:\mypath\myapp\myapp\web\WEB-INF\lib does not exist!

How do I fix the code so that it no longer gives the BUILD FAILED message?

Community
  • 1
  • 1
CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • First of all can you please specify where the `myapp` project folder resides in the file system? I see that you are using `myapp/myapp` paths (you have it like 3 times in your last failed message) – c.s. Aug 15 '13 at 19:20
  • I don't see the declaration of 'basedir' and for building a war file user the target name = war – Aniket Aug 15 '13 at 19:22
  • It seems that you are missing crucial information about how to run java and compile files. a) Your `JAVA_HOME` variable should point to `C:\mypath\Java\jdk1.7.0_17`. You should also find the `PATH` variable and add `C:\mypath\Java\jdk1.7.0_17\bin`. If all is set correctly and you run `java -version` from command line you should see `java version 1.7.0_17` b) Then go to Eclipse `Windows->Preferences->Java->Installed JREs` and add a new runtime that points to that JDK above, if it is not already there. If it is, just select it as default. Then make sure your project uses that JDK – c.s. Aug 15 '13 at 21:20
  • Your class `myapp.web.HomeServlet` is not found. If this was supposed to go to the `WEB-INF/classes` then the ant script didn't do its job. The war file is actually a zip file. Unzip it somewhere and check if all the classes and jars exist where they are supposed to be. Compare with your web folder. Then check the ant script why it didn't copy the missing items – c.s. Aug 15 '13 at 23:26

2 Answers2

1

I think the line:

<copydir dest="dist/web/WEB-INF/lib" src="${basedir}/../web/WEB-INF/lib" />

should just be:

<copydir dest="dist/web/WEB-INF/lib" src="${basedir}/web/WEB-INF/lib" />

${basedir} refers to C:\mypath\myapp, if that is where Ant is run from (or where BuildWar.xml lives)

Peter Faller
  • 132
  • 1
  • 1
  • 5
  • C:\Program Files\Java\jre7 is a Java runtime environment, that is, it is sufficient for running Java code; but to compile Java code you need a JDK. If you have one installed, set JAVA_HOME to point to the JDK rather than the JRE; otherwise visit java.oracle.com and download / install one. – Peter Faller Aug 15 '13 at 19:45
1

Declare it right at the top as an attribute of the project tag.

<?xml version="1.0" encoding="UTF-8"?>

<project basedir="C:\......." name="myproject" default="default">
<target name="default" depends="setup,compile,buildwar,deploy"></target>

<target name="setup">
    <mkdir dir="dist" />
    <echo>Copying web into dist</echo>
    <copydir dest="dist/web" src="web" />
    <copydir dest="dist/web/WEB-INF/lib" src="${basedir}/../web/WEB-INF/lib" />
</target>

<target name="compile">
    <delete dir="${dist.dir}/web/WEB-INF/classes" />
    <mkdir dir="${dist.dir}/web/WEB-INF/classes" />
    <javac destdir="${dist.dir}/web/WEB-INF/classes" srcdir="src">
        <classpath>
            <fileset dir="${basedir}/myapp/web/WEB-INF/lib">
                <include name="*" />
            </fileset>
        </classpath>
    </javac>
    <copy todir="${dist.dir}/web/WEB-INF/classes">
        <fileset dir="src">
            <include name="**/*.properties" />
            <include name="**/*.xml" />
        </fileset>
    </copy>
</target>

<target name="buildwar">
    <war basedir="${basedir}/dist/web" destfile="My.war"
         webxml="${basedir}/dist/web/WEB-INF/web.xml">
        <exclude name="WEB-INF/**" />
        <webinf dir="${basedir}/dist/web/WEB-INF/">
            <include name="**/*.jar" />
        </webinf>
    </war>
</target>

<target name="deploy">
    <copy file="My.war" todir="${tomcat.deploydir}" />
</target>

</project>

After you declare the base dir write all paths relative to basedir.

Aniket
  • 279
  • 3
  • 8
  • 21
  • To set Java_Home environment variable refer this link. Its very easy. computerhope.com/issues/ch000549.htm – Aniket Aug 15 '13 at 20:05