2

目前我有以下build.xml:

<project name="Bccn" default="help" basedir=".">
<!-- Define the properties used by the build -->
<property name="app.name" value="bccn" />
<property name="app.version" value="0.1-dev" />
<property name="tcserver.home" value="/home/abhishek/tomcat" />
<property name="work.home" value="${basedir}/work" />
<property name="dist.home" value="${basedir}/dist" />
<property name="src.home" value="${basedir}/src" />
<property name="web.home" value="${basedir}/web" />
<property name="lib.dir" value="${basedir}/lib" />

<target name="help">
    <echo>You can use the following targets:</echo>
    <echo>
    </echo>
    <echo>  help    : (default) Prints this message </echo>
    <echo>  all     : Cleans, compiles, and packages application</echo>
    <echo>  clean   : Deletes work directories</echo>
    <echo>  compile : Compiles servlets into class files</echo>
    <echo>  dist    : Packages artifacts into a deployable WAR</echo>
    <echo>
    </echo>
    <echo>For example, to clean, compile, and package all at once, run:</echo>
    <echo>prompt> ant all </echo>
</target>

<!-- Define the CLASSPATH -->
<path id="compile.classpath">
    <fileset dir="${tcserver.home}/bin">
        <include name="*.jar" />
    </fileset>
    <pathelement location="${tcserver.home}/lib" />
    <fileset dir="${tcserver.home}/lib">
        <include name="*.jar" />
    </fileset>
</path>

<target name="all" depends="clean,compile,dist" description="Clean work dirs, then compile and create a WAR" />

<target name="clean" description="Delete old work and dist directories">
    <delete dir="${work.home}" />
    <delete dir="${dist.home}" />
</target>

<target name="prepare" depends="clean" description="Create working dirs and copy static files to work dir">
    <mkdir dir="${dist.home}" />
    <mkdir dir="${work.home}/WEB-INF/classes" />
    <!-- Copy static HTML and JSP files to work dir -->
    <copy todir="${work.home}">
        <fileset dir="${web.home}" />
    </copy>
</target>

<target name="compile" depends="prepare" description="Compile Java sources and copy to WEB-INF/classes dir">
    <javac srcdir="${src.home}" destdir="${work.home}/WEB-INF/classes">
        <classpath refid="compile.classpath" />
    </javac>

    <copy todir="${work.home}/WEB-INF/classes">
        <fileset dir="${src.home}" excludes="**/*.java" />
    </copy>

</target>


<target name="dist" depends="compile" description="Create WAR file for binary distribution">
    <jar jarfile="${dist.home}/${app.name}-${app.version}.war" basedir="${work.home}" />

</target>

现在我将 log4j 作为本地依赖项包含在内,并希望在创建 .war 文件时包含它。但是,ANT 无法找到依赖项。有没有办法让它工作?对不起,这个基本问题,我是菜鸟。

更新(感谢我已经得到的帮助):

我不想添加“战争”的东西,所以我修改了我的 build.xml 如下:``

<target name="help">
    <echo>You can use the following targets:</echo>
    <echo>
    </echo>
    <echo>  help    : (default) Prints this message </echo>
    <echo>  all     : Cleans, compiles, and packages application</echo>
    <echo>  clean   : Deletes work directories</echo>
    <echo>  compile : Compiles servlets into class files</echo>
    <echo>  dist    : Packages artifacts into a deployable WAR</echo>
    <echo>
    </echo>
    <echo>For example, to clean, compile, and package all at once, run:</echo>
    <echo>prompt> ant all </echo>
</target>

<!-- Define the CLASSPATH -->
<path id="compile.classpath">
    <fileset dir="${tcserver.home}/bin">
        <include name="*.jar" />
    </fileset>
    <pathelement location="${tcserver.home}/lib" />
    <fileset dir="${tcserver.home}/lib">
        <include name="*.jar" />
    </fileset>
    <fileset dir="${lib.dir}">
        <include name="*.jar" />
    </fileset>
</path>

<target name="all" depends="clean,compile,dist" description="Clean work dirs, then compile and create a WAR" />

<target name="clean" description="Delete old work and dist directories">
    <delete dir="${work.home}" />
    <delete dir="${dist.home}" />
</target>

<target name="prepare" depends="clean" description="Create working dirs and copy static files to work dir">
    <mkdir dir="${dist.home}" />
    <mkdir dir="${work.home}/WEB-INF/classes" />
    <!-- Copy static HTML and JSP files to work dir -->
    <copy todir="${work.home}">
        <fileset dir="${web.home}" />
    </copy>

</target>

<target name="compile" depends="prepare" description="Compile Java sources and copy to WEB-INF/classes dir">
    <javac srcdir="${src.home}" destdir="${work.home}/WEB-INF/classes">
        <classpath refid="compile.classpath" />
    </javac>

    <copy todir="${work.home}/WEB-INF/classes">
        <fileset dir="${src.home}" excludes="**/*.java" />
        <fileset dir="${lib.dir}" includes="*.jar" />
    </copy>

</target>


<target name="dist" depends="compile" description="Create WAR file for binary distribution">
    <jar jarfile="${dist.home}/${app.name}-${app.version}.war" basedir="${work.home}" />

</target>

现在 ANT 可以找到依赖项并编译它。但是,当我将它部署到 Tomcat 服务器时,它无法归档依赖项。您能否提供一些关于如何打包依赖项以使其对 Tomcat 也可见的想法?

4

1 回答 1

0

您应该改用战争任务

<war destfile="${warname}" webxml="war/WEB-INF/web.xml">
    <fileset dir="${work.home}"/>
    <lib dir="${lib.dir}/" includes="log4j.jar"/>
    <classes dir = "${CLASSES}" />
</war>

您可以对其进行配置以查找要包含在您的 war 文件中的依赖项 jar,并指定 web.xml 文件夹的路径。您不应该将 .class 文件复制到 war 文件夹中的目标位置,让 war 任务为您完成。

于 2013-08-07T21:52:04.507 回答