Ant没有编译资源,我无法使用log4j进行日志记录

时间:2013-10-29 06:04:41

标签: java logging ant log4j

当通过ant运行java app项目时,ant正在寻找log4j属性,

有人可以帮助我如何将资源/ log4j.properties包含到我的项目的编译中,这样我就可以使用log4j

这是我的build.xml

<property name="maven.repo.local" value="${user.home}/.m2/repository" />


<target name="clean">
    <echo message="deleting directory build" />
    <delete dir="build" />
</target>

<target name="compile" depends="clean">
    <echo message="creating directory build/classes" />
    <mkdir dir="build/classes" />
    <echo message="compiling source codes" />
    <javac srcdir="src" classpathref="jars" includeantruntime="false" destdir="build/classes" />

</target>

<target name="jar" depends="clean,compile">
    <echo message="creating a Jar fire, using compiled classes from build/classes" />
    <mkdir dir="build/jar" />


    <jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
        <manifest>
            <attribute name="Main-Class" value="com.sample.anttutorial.Main" />
        </manifest>
        <zipgroupfileset dir="${maven.repo.local}" includes="log4j/log4j/1.2.17/log4j-1.2.17.jar" />
    </jar>
</target>

<target name="run">
    <java jar="build/jar/HelloWorld.jar" fork="true" />

</target>

<path id="jars">
    <fileset dir="${maven.repo.local}" includes="log4j/log4j/1.2.17/log4j-1.2.17.jar" />
</path>

1 个答案:

答案 0 :(得分:0)

在你的log4j.properties中:

# Log format to Ant Build
log4j.logger.org.apache.tools.ant=info, antout
log4j.additivity.org.apache.tools.ant=false
log4j.appender.antout=org.apache.log4j.RollingFileAppender
log4j.appender.antout.layout=org.apache.log4j.PatternLayout
log4j.appender.antout.layout.ConversionPattern=[%d]\t%m%n
log4j.appender.antout.File=${catalina.home}/logs/AntBuild.out
您可以在代码中

定义:

    Project p = new Project(); 
    Log4jListener log4jListener = new Log4jListener();
    p.addBuildListener(log4jListener);
相关问题