Java Mailing List Archive

http://www.ant-tasks.com/

Apache Ant Archive

» Ant Users List
» Ant Developers List
configuring Log4J from build.xml

configuring Log4J from build.xml

2003-03-28       - By Erik Price

Hi,

I have just started to incorporate Log4J into my simple command-line
application.  I build exclusively with Ant.  To avoid hard-coding Log4J
configurations into my code, I wish to use its built-in ability to read
a System Property passed in, which specifies the configuration file to
use for logging.

When I don't pass in the System Property, it works fine -- because Log4J
automatically searches the current class loader's path for a file named
"log4j.properties" and uses that as the configuration file.  I have such
a file.  However, I don't wish to rely on this file being in my class
loader's class path, so I'm trying to pass in the location of the
"log4j.properties" file with <sysproperty>.  I have confirmed (using a
simple System.out.println() in my APPLICATION code [not my JUNIT TEST
code]) that this property is passed in correctly, and that its value
*is* the path to the file, and that the file *does* exist (using
File.exists()).  However, Log4J never recognizes the file when passed in
this way; it only works if I remove the <sysproperty> element and place
the file in the expected part of the class loader's path.

I have included the relevant section of my build.xml at the end of this
email.  Does anyone have experience passing in a Log4J properties file
via Ant?  If someone has either (1) an alternative solution, or (2) any
advice, I'm willing to listen -- I would just like to avoid hard-coding
configuration information into my application's code.


Thank you,

Erik

(PS: I did ask about this on the Log4J mailing list but it doesn't seem
to get a lot of traffic... I just thought maybe someone here has
experience combining Log4J and Ant.)




  <!-- ===== test target

  Performs JUnit tests against compiled code.
  -->
  <target name="test" depends="compile,test-init"
   description="Perform JUnit tests">

    <javac destdir="${test.dir}"
           debug="${build.debug}"
           includeAntRuntime="false"
           srcdir="test">
      <classpath refid="test.classpath"/>
    </javac>

    <!-- grab any other non-source files -->
    <copy todir="${test.dir}">
      <fileset dir="test" excludes="**/*.java"/>
    </copy>

    <!-- unit-test time
         printsummary   not needed b/c we use brief formatter with
                        usefile off
         haltonfailure  we set a flag instead (otherwise junitreports
                        dont run)
         fork           CaliberRM SDK requires that we fork unit tests
         errorProperty  these props flag fail task (after junitreport)
         failureProperty -->
    <junit printsummary="false"
           haltonfailure="false"
           fork="true"
           errorProperty="test.failed"
           failureProperty="test.failed">
      <classpath refid="test.classpath"/>

<!-- this is where I pass the property in.  The value is set
     earlier in build.xml and *is* valid -->

      <sysproperty key="log4j.configuration"
                   value="${test.log4j.props}"/>
      <sysproperty key="test1.file.path"
                   value="${test.dir}/testreqreport1.xml"/>
      <!-- output to console -->
      <formatter type="brief" usefile="false"/>
      <!-- output to file -->
      <formatter type="xml"/>
      <!-- this is where we decide which files to junit -->
      <batchtest todir="${test.data.dir}">
            <fileset dir="${test.dir}" includes="**/*Test.class"/>
      </batchtest>
    </junit>

    <!-- generate a user-friendly report from xml -->
    <junitreport todir="${test.data.dir}">
      <fileset dir="${test.data.dir}">
        <include name="TEST-*.xml"/>
      </fileset>
      <report format="frames" todir="${test.reports.dir}"/>
    </junitreport>

    <!-- if the junit task set test.failed -->
    <fail message="Tests failed.  Check log and/or reports."
          if="test.failed"/>

  </target>



©2008 ant-tasks.com - Jax Systems, LLC, U.S.A.