构建Ant项目时出现Javadoc错误

时间:2011-08-24 08:46:49

标签: ant log4j javadoc

我正在尝试为我的项目编写build.xml文件。当我将build.xml作为Ant项目运行时,出现以下错误:

D:\workspace\LogAlerter\src\com\j32bit\alerter\launcher\LogAlerter.java:9:  
error: package org.apache.log4j does not exist  
    [javadoc] import org.apache.log4j.Logger;

我在LogAlerter.Java中导入了log4j。这是我的build.xml文件:

<?xml version="1.0"?>
<project name="LogAlerter" default="main" basedir=".">
    <!-- Sets variables which can later be used. -->
    <!-- The value of a property is accessed via ${} -->
    <property name="src.dir"   location="src"   />
    <property name="build.dir" location="build" />
    <property name="dist.dir"  location="dist"  />
    <property name="docs.dir"  location="docs"  />
    <property name="libs.dir"  location="lib"  />

    <!--
        Create a classpath container which can be later used in the ant task
    -->
    <path id="build.classpath">
        <fileset dir="${libs.dir}">
            <include name="**/*.jar" />
        </fileset>
    </path>


    <!-- Deletes the existing build, docs and dist directory-->
    <target name="clean">
        <delete dir="${build.dir}" />
        <delete dir="${docs.dir}" />
        <delete dir="${dist.dir}" />
    </target>

    <!-- Creates the  build, docs and dist directory-->
    <target name="makedir">
        <mkdir dir="${build.dir}" />
        <mkdir dir="${docs.dir}" />
        <mkdir dir="${dist.dir}" />
    </target>

    <!-- Compiles the java code (including the usage of library for JUnit -->
    <target name="compile" depends="clean, makedir" >
        <javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath" includeantruntime="false">
        </javac>
    </target>

    <!-- Creates Javadoc -->
    <target name="docs" depends="compile">
        <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
            <!-- Define which files / directory should get included, we include all -->
            <packageset dir="${src.dir}" defaultexcludes="yes">
                <include name="**" />
            </packageset>
        </javadoc>
    </target>

    <!--Creates the deployable jar file  -->
    <target name="jar" depends="compile">
        <jar destfile="${dist.dir}\LogAlerter.jar" basedir="${build.dir}">
            <manifest>
                <attribute name="Main-Class" value="LogAlerter.Main" />
            </manifest>
        </jar>
    </target>

    <target name="main" depends="compile, jar, docs">
        <description>Main target</description>
    </target>

</project>

3 个答案:

答案 0 :(得分:7)

尝试将类路径引用添加到javadoc任务中:

<javadoc packagenames="src" 
         sourcepath="${src.dir}" 
         destdir="${docs.dir}"
         classpathref="build.classpath">

答案 1 :(得分:2)

警告告诉您的是,您没有为javadoc任务提供完整的类路径。尝试在编译任务中添加类似的类路径引用,并查看引导的位置。

答案 2 :(得分:2)

导入很好,但要确保它在运行时可用于JavaDoc工具。 log4j.jar中应该出现build.classpath

使用classpathref目标中的docs,如下所示:

<javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}" classpathref="build.classpath">