我有一个构建文件,作为构建过程的一部分,它依赖于多个 taskdef。这些 taskdef 项(例如,webdoclet 和 jasper2)使用 log4j 作为记录器。理想情况下,我希望能够为每个文件提供不同的 log4j 配置文件,但至少,我希望能够指定使用哪个 log4j 配置文件。
我过去所做的工作是将包含我希望 taskdef 使用的 log4j.xml 的目录放在类路径的前面。例如:
<target name="define.jasper2"> <path id="jspc.classpath"> <!-- Put this FIRST so Jasper will find the log4j.xml in the Build directory --> <pathelement location="Build"/> <fileset dir="${tomcat.libs}"> .... </fileset> <pathelement location="${log4j.jar}"/> .... </path> <taskdef name="jasper2" classname="org.apache.jasper.JspC" classpathref="jspc.classpath"/> </target>
我已经证实,事实上,“Build”目录位于类路径的前面,并且该目录中存在 log4j.xml。但是,当我在详细模式下运行 ant 并调试 log4j 时,我看到 log4j 正在选择 当前工作目录 中的 log4j.xml,这不是我想要的。 Log4j 似乎没有使用类路径来解析默认的 log4j.xml。
我正在使用 log4j 1.2.8(嵌入在一个更大的框架中,所以我无法升级它)并且其中一些任务定义似乎依赖于 commons-logging 1.0.3。构建过程使用 Sun Java 5。
如果我在运行 ant 之前设置 ANT_OPTS=-Dlog4j.configuration=Build/log4j.xml
,taskdefs 会正确加载所需的 log4j.xml。
将“Build”目录放在用于工作的任务定义的类路径的前面。我不知道发生了什么变化。如何恢复我可以控制的行为——在 build.xml 中——哪个 log4j 配置文件用于给定任务?除了设置 ANT_OPTS 和重新排列文件以将应用程序的 log4j.xml 移出当前工作目录之外,还有其他方法让 log4j 找到正确的 log4j.xml 文件吗?
最佳答案
我可以使用带有 sysproperty
的相对文件 URL 指定配置文件,如下所示:
<java classname="com.lunatech.MainClass">
<sysproperty key="log4j.configuration" value="file:src/log4j-debug.properties"/>
<classpath refid="runtime.classpath"/>
</java>
关于java - 如何为不同的 ant 任务提供自定义 log4j.xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/481808/