Gradle logger conflicts with logger used in ant build


#1

using following cmd to load an ant build file to be used in gradle

ant.importBuild 'build.xml'
    ant.createenv()
    customize.dependsOn 'prepare_env'
    build.dependsOn 'prepare_env', 'customize', 'versioninfo', 'compass_compile', 'compress_js'

This works fine but when I execute any ant task via gradle e.g. gradle build an exception is thrown which states that logging class is already loaded

log4j:ERROR A "org.apache.log4j.PatternLayout" object is not assignable to a "org.apache.log4j.Layout" variable.
log4j:ERROR The class "org.apache.log4j.Layout" was loaded by
log4j:ERROR [AntClassLoader[
 ...
 C:\project\lib\jaxen-1.1.3.jar;C:\project\lib\jgroups-3.2.5.Final.jar;
 C:\project\lib\joda-time-2.0.jar;
 C:\project\lib\junit-4.9.jar;
 C:\project\lib\log4j-1.2.16.jar;
 C:\project\lib\mime-util-2.1.3.jar;
 ....
]]
whereas object of type
log4j:ERROR "org.apache.log4j.PatternLayout" was loaded by [org.gradle.internal.classloader.MutableURLClassLoader@71f250e8].

in this case the log4j-1.2.16 conflicts with gradles log4j-over-slf4j-1.7.2. It is not possible to change the log4j implementation because it is a third party app.

Is it possible to run the ant task in a sandbox where the classloader of gradle does not influence the build?


(Denis Lutz) #2

Hey guys, is there a solution for that in the meanwhile?


(Luke Daley) #3

I don’t quite understand the setup here.

Why do you need to put a custom log4j lib on the Ant classpath?


(a. n.) #4

I’ve the same problem. (sorry if the question is stupid, I’m quite new to Gradle) The framework I’m using (Hybris) forces Ant vers.1.9.1

I’ve created my own gradle distribution changing ant to version 1.9.1 instead of 1.9.2 in order to be able to call ant tasks inside my build.gradle. The problem I’m having now is exactly the one mentioned here.

log4j:ERROR A "org.apache.log4j.PatternLayout" object is not assignable to a "org.apache.log4j.Layout" variable.
log4j:ERROR The class "org.apache.log4j.Layout" was loaded by
 log4j:ERROR [AntClassLoader[
...
/data/proj/lib/junit-4.9.jar:
/data/proj/lib/log4j-1.2.16.jar:
/data/proj/lib/mail-1.4.4.jar:
...
]] whereas object of type
 log4j:ERROR "org.apache.log4j.PatternLayout" was loaded by [org.gradle.internal.classloader.MutableURLClassLoader@7a5f39b0].

in the current settings.gradle of the gradle project I’ve just changed

libraries.ant = dependencies.module(‘org.apache.ant:ant:1.9.1’) {

dependency ‘org.apache.ant:ant-launcher:1.9.1@jar’ }

just by calling ‘gradlew task’ I get

$ ./gradlew task
log4j:ERROR A "org.apache.log4j.PatternLayout" object is not assignable to a "org.apache.log4j.Layout" variable.
log4j:ERROR The class "org.apache.log4j.Layout" was loaded by
 log4j:ERROR [AntClassLoader[/data/proj/platform/bootstrap/bin/ybootstrap.jar:/data/proj/platform/bootstrap/bin/yant.jar:
.....
r]] whereas object of type
 log4j:ERROR "org.apache.log4j.PatternLayout" was loaded by [org.gradle.internal.classloader.MutableURLClassLoader@1c49094].
[ant:echo] ---------------------------------------------------------------
[ant:echo] hybris Platform Environment
[ant:echo] ---------------------------------------------------------------
  .....
   log4j:ERROR [AntClassLoader[/data/proj/platform/bootstrap/bin/ybootstrap.jar:/data/proj/platform/bootstrap/bin/yant.jar:
.....
r]] whereas object of type
 log4j:ERROR "org.apache.log4j.PatternLayout" was loaded by [org.gradle.internal.classloader.MutableURLClassLoader@1c49094].
:tasks
.....
  (tasks are correctly shown)
....

It looks like only by importing the build.xml gradle calls ant default task and then fails in the mid.

Question is why is it calling it? my build.gradle has only the ant.importBuild nothing more.

The other thing is that even upgrading log4j to 1.2.17 in the Hybris ant distribution doesn’t solve the problem.


#5

@Luke: The framework [y] which I am using is providing ant build which is wrapped by gradle and this 3rd Party build file includes the lib. Because the framework it self is not in the VCS and regularly updated I don’t want to change all build files which are including the lib.


(a. n.) #6

I’m using [y] as well and I’d like to leave its ant distribution untouched.

I can also see how Gradle could leverage great capabilities to the whole build process. Here is my contact if you want http://www.google.com/recaptcha/mailhide/d?k=011SdL92uH8gw779RSqKNaTA==&c=BoTd4ANA0FNV9ROVTKnzpO7Dpa8RzHAO5SrBEK3rJ_Q=


(andy.jewell) #7

Did anyone ever resolve this? I’m having the same issue. If I leave log4j out of the ant classpath, I get a class not found exception. If I put it in, I get the same conflict described here with the no classdef found error.


(Peter Niederwieser) #8

Is it possible to run the ant task in a sandbox where the classloader of gradle does not influence the build?

It is run in a sandbox, except that the logging is bridged. I think the choices are:

  • Use a compatible Log4J version on the Ant side * Call Ant as an external process * Do the work on the Gradle side

(andy.jewell) #9

I went with your option 1 and removed log4j from the ant CP and replaced it with log4j-over-slf4j and that worked. Thanks for the help!


(Denis Lutz) #10

Hey A.N. may I ask you for what company are you working? Can you please write me directly on my email: denis.lutz[…attt…]gmail.com


(Vladimir Strugatsky) #11

I also had to add slf4j-api and slf4j-nop JARs to Ant classpath to get this to work.

Thank you for useful info!