如何在我的ant构建中包含本地依赖项

时间:2013-08-07 21:47:02

标签: java ant

目前我有以下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无法找到依赖项。有没有办法让它运作?对不起基本问题,我是一个菜鸟。

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

我不想添加“war”的东西所以我修改了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也是可见的?

1 个答案:

答案 0 :(得分:0)

您应该使用war task代替:

<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任务为你做。