从eclipse web项目创建war文件

时间:2013-08-15 19:15:32

标签: java eclipse tomcat servlets ant

我一直在调整this suggested code 来制作war文件,并在上图中创建了BuildWar.xml文件的以下内容:

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

在eclipse中将上述代码作为ant buildfile运行会产生一些警告,表示不推荐使用copydir,并且还会产生以下错误消息:

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

但是,当我用myapp替换第10行中的..时,我收到一条错误消息,说明

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

如何修复代码,使其不再提供BUILD FAILED消息?

2 个答案:

答案 0 :(得分:1)

我认为这一行:

&lt; copydir dest =“dist / web / WEB-INF / lib”src =“$ {basedir} /../ web / WEB-INF / lib”/&gt;

应该是:

&lt; copydir dest =“dist / web / WEB-INF / lib”src =“$ {basedir} / web / WEB-INF / lib”/&gt;

$ {basedir}指的是C:\ mypath \ myapp,如果这是运行Ant的地方(或者BuildWar.xml所在的地方)

答案 1 :(得分:1)

在顶部将其声明为项目标记的属性。

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

声明基础目录后,写下相对于basedir的所有路径。