ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Erik Price <>
Subject configuring Log4J from build.xml
Date Fri, 28 Mar 2003 15:13:02 GMT

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 
"" 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 
"" 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,


(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}"
       <classpath refid="test.classpath"/>

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

     <!-- 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"
       <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"
       <sysproperty key="test1.file.path"
       <!-- 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="${}">
             <fileset dir="${test.dir}" includes="**/*Test.class"/>

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

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


View raw message