Gradle | Spring boot dependencies are not excluding - spring

I'm trying to get log4j to work in a project that I'm working on. I added the relevant log4j dependencies in build.gradle and excluded the Spring boot starter logging so it can work.
This worked fine when I used Maven as the build tool, but once I switched to Gradle it's not working at all (excepts the logging from Spring boot starter). Here are the dependencies in my build.gradle
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web'){
exclude module: 'org.springframework.boot:spring-boot-starter-logging'
}
compile('org.springframework.boot:spring-boot-starter-log4j')
compile('org.springframework.boot:spring-boot-starter-data-rest')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.postgresql:postgresql:9.3-1101-jdbc41')
compile('org.scala-lang:scala-library:2.10.4')
testCompile('org.springframework.boot:spring-boot-starter-test') {
exclude module: 'commons-logging'
}
providedCompile('org.springframework.boot:spring-boot-starter-tomcat')
}
But as you can clearly see in the dependency tree the spring-boot-starter-logging is still there. I'm guessing that this is the issue why the logging isn't working.
Here's the dependency tree:
+--- org.springframework.boot:spring-boot-starter-data-jpa: -> 1.2.1.RELEASE
| +--- org.springframework.boot:spring-boot-starter:1.2.1.RELEASE
| | +--- org.springframework.boot:spring-boot:1.2.1.RELEASE
| | | +--- org.springframework:spring-core:4.1.4.RELEASE
| | | \--- org.springframework:spring-context:4.1.4.RELEASE
| | | +--- org.springframework:spring-aop:4.1.4.RELEASE
| | | | +--- aopalliance:aopalliance:1.0
| | | | +--- org.springframework:spring-beans:4.1.4.RELEASE
| | | | | \--- org.springframework:spring-core:4.1.4.RELEASE
| | | | \--- org.springframework:spring-core:4.1.4.RELEASE
| | | +--- org.springframework:spring-beans:4.1.4.RELEASE (*)
| | | +--- org.springframework:spring-core:4.1.4.RELEASE
| | | \--- org.springframework:spring-expression:4.1.4.RELEASE
| | | \--- org.springframework:spring-core:4.1.4.RELEASE
| | +--- org.springframework.boot:spring-boot-autoconfigure:1.2.1.RELEASE
| | | \--- org.springframework.boot:spring-boot:1.2.1.RELEASE (*)
| | +--- org.springframework.boot:spring-boot-starter-logging:1.2.1.RELEASE
Here's my log4j.properties file
log4j.rootLogger=INFO, fileout, CONSOLE
PID=????
LOG_PATTERN=[%d{yyyy-MM-dd HH:mm:ss.SSS}] log4j%X{context} - ${PID} %5p [%t] --- %c{1}: %m%n
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=${LOG_PATTERN}
# Log4j configurations for with file appender
log4j.category.org.hibernate.validator.internal.util.Version=WARN
log4j.category.org.apache.coyote.http11.Http11NioProtocol=WARN
log4j.category.org.apache.tomcat.util.net.NioSelectorPool=WARN
log4j.category.org.apache.catalina.startup.DigesterFactory=ERROR
log4j.appender.fileout=org.apache.log4j.RollingFileAppender
log4j.appender.fileout.File=sampleLog.log
log4j.appender.fileout.MaxFileSize=1024KB
log4j.appender.fileout.MaxBackupIndex=1
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.conversionPattern=${LOG_PATTERN}
UPDATE
I managed to fix the exclusion of jar file dependency. But the logging is still not working, log4j.properties is also in the WAR distribution under classes.
UPDATE 02
It worked issue is with the my IDE (STS)

All the spring-boot-starter-* projects are dependent on spring-boot-starter project, which in turn in dependent on spring-boot-starter-logging. I was able to remove this dependency by adding the following line in configurations section:
configurations {
compile.exclude module: 'spring-boot-starter-logging'
}

You have excluded the spring-boot-starter-logging module from spring-boot-starter-web, but as you clearly can see from the dependency tree, the module spring-boot-starter-data-jpa also has a dependency to spring-boot-starter-logging, so you also have to exclude it from there (and from all other spring-boot-starter-* dependencies, since all of them depend on spring-boot-starter-logging).

I have removed all logging dependencies using the below code
configurations {
all*.exclude module : 'spring-boot-starter-logging'
}

Related

How to resolve JAXB classpath issues on Java 12

Im porting application using JAXB to JDK 11/12 but am completely lost when trying to fix those JAXB classpath issues. I have googled and read basically everything related to this so Im aware of the changes made in Java11, i.e. removal of JAXB from JDK ... However in that case I would assume a trivial fix consisting of adding api and impl JARs on classpath. But this is not working and despite I have those JARs on classpath Im still getting the known exception, despite using 2.3.2 JAXB:
javax.xml.bind.JAXBException
- with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
even if I see the class is in different package and I instruct to use correct package via system variable it does not work either:
gradle -Djavax.xml.bind.context.factory=com.sun.xml.bind.v2.ContextFactory build
javax.xml.bind.JAXBException
- with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory]
Does anyone have clue how to resolve this? I dont want to play with manifest files etc. It should be possible to just place the right JARs on classpath and that is it, or not?
Summary here but unable to get it working:
http://www.descher.at/descher-vu/2019/01/java-11-jaxb-and-osgi/
Update, dependencies used in Gradle:
- jaxb-xjc has jaxb-runtime (impl.) as transitive dependency. I see it is picked by Gradle but still the same error.
classpath "com.sun.activation:javax.activation:1.2.0"
classpath "jakarta.xml.bind:jakarta.xml.bind-api:2.3.2"
classpath "org.glassfish.jaxb:jaxb-xjc:2.3.2"
classpath"org.glassfish.jaxb:jaxb-core:2.3.0.1"
[org.gradle.internal.operations.DefaultBuildOperationExecutor] Build operation 'Resolve jaxb-runtime.jar (org.glassfish.jaxb:jaxb-runtime:2.3.2)' completed
[org.gradle.internal.component.model.ComponentAttributeMatcher] Selected match org.glassfish.jaxb:jaxb-runtime:2.3.2 configuration runtime from candidates [org.glassfish.jaxb:jaxb-runtime:2.3.2 configuration runtime] for {org.gradle.dependency.bundling=external, org.gradle.jvm.version=12, org.gradle.usage=java-runtime}
This is output of "gradle dependencies" and all the JAXB jars are there, still classpath error is being thrown. The JAR file jaxb-runtime from Glassfish for sure has this ContextFactory class.
runtimeClasspath - Runtime classpath of source set 'main'.
+--- org.springframework.boot:spring-boot-starter-log4j2 -> 2.1.0.RELEASE
| +--- org.apache.logging.log4j:log4j-slf4j-impl:2.11.1
| | +--- org.slf4j:slf4j-api:1.7.25
| | +--- org.apache.logging.log4j:log4j-api:2.11.1
| | \--- org.apache.logging.log4j:log4j-core:2.11.1
| | \--- org.apache.logging.log4j:log4j-api:2.11.1
| +--- org.apache.logging.log4j:log4j-core:2.11.1 (*)
| +--- org.apache.logging.log4j:log4j-jul:2.11.1
| | \--- org.apache.logging.log4j:log4j-api:2.11.1
| \--- org.slf4j:jul-to-slf4j:1.7.25
| \--- org.slf4j:slf4j-api:1.7.25
+--- org.projectlombok:lombok -> 1.18.2
+--- org.apache.commons:commons-lang3 -> 3.8.1
+--- org.apache.commons:commons-collections4 -> 4.4
+--- com.google.guava:guava -> 23.0
| +--- com.google.code.findbugs:jsr305:1.3.9
| +--- com.google.errorprone:error_prone_annotations:2.0.18
| +--- com.google.j2objc:j2objc-annotations:1.1
| \--- org.codehaus.mojo:animal-sniffer-annotations:1.14
+--- org.apache.ws.xmlschema:xmlschema-core -> 2.0.1
+--- org.glassfish.jaxb:jaxb-runtime:2.3.2
| +--- jakarta.xml.bind:jakarta.xml.bind-api:2.3.2
| | \--- jakarta.activation:jakarta.activation-api:1.2.1
| +--- org.glassfish.jaxb:txw2:2.3.2
| +--- com.sun.istack:istack-commons-runtime:3.0.8
| | \--- jakarta.activation:jakarta.activation-api:1.2.1
| +--- org.jvnet.staxex:stax-ex:1.8.1
| | +--- jakarta.activation:jakarta.activation-api:1.2.1
| | \--- jakarta.xml.bind:jakarta.xml.bind-api:2.3.2 (*)
| +--- com.sun.xml.fastinfoset:FastInfoset:1.2.16
| \--- jakarta.activation:jakarta.activation-api:1.2.1
\--- org.glassfish.jaxb:jaxb-core:2.3.0.1
+--- javax.xml.bind:jaxb-api:2.3.0 -> 2.3.1
| \--- javax.activation:javax.activation-api:1.2.0
+--- org.glassfish.jaxb:txw2:2.3.0.1 -> 2.3.2
\--- com.sun.istack:istack-commons-runtime:3.0.5 -> 3.0.8 (*)
From some reason I had no luck with Glassfish JARs, but those original Sun/Oracle dependencies worked.

Gradle dependency conflict when using `implementation`

I have a project with two gradle modules: lib and app. I just changed lib's build.gradle to stop exposing a dependency (i.e. I moved from api -> implementation). The app module doesn't directly depend on OkHttp logging interceptor so I figure it's better to not expose it.
lib module's build.gradle:
dependencies {
// api 'com.squareup.okhttp3:logging-interceptor:3.10.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
...
}
app module's build.gradle:
dependencies {
implementation project(':lib')
implementation group: 'com.zendesk', name: 'support-providers', version: '2.0.0'
...
}
However I'm now seeing a compile problem:
Conflict with dependency 'com.squareup.okhttp3:logging-interceptor' in
project ':app'. Resolved versions for runtime classpath (3.10.0) and
compile classpath (3.8.1) differ
If I look at project structure I see this:
+--- project :lib
...
+--- com.zendesk:support-providers:2.0.0
| +--- com.zendesk:core:1.0.0
| | +--- com.zendesk:java-common:1.13
| | +--- com.google.dagger:dagger:2.12 -> 2.15 (*)
| | +--- com.squareup.retrofit2:retrofit:2.3.0
| | | \--- com.squareup.okhttp3:okhttp:3.8.0 -> 3.8.1
| | | \--- com.squareup.okio:okio:1.13.0
| | +--- com.squareup.retrofit2:converter-gson:2.3.0
| | | +--- com.squareup.retrofit2:retrofit:2.3.0 (*)
| | | \--- com.google.code.gson:gson:2.7
| | +--- com.squareup.okhttp3:logging-interceptor:3.8.1 // <----- SEE HERE
| | | \--- com.squareup.okhttp3:okhttp:3.8.1 (*)
| | +--- com.squareup.okhttp3:okhttp:3.8.1 (*)
| | +--- com.android.support:support-annotations:27.0.2 -
lib isn't revealing any of it's dependencies (obviously) and app depends on Zendesk sdk which depends on different version of OkHttp logging interceptor.
I only see two ways to fix this:
revert api -> implementation in lib module, thus exposing logging interceptor to app module
declare a top level dependency on logging interceptor and set to 3.10 to force Zendesk to use latest:
app build.gradle:
dependencies {
implementation project(':lib')
implementation group: 'com.zendesk', name: 'support-providers', version: '2.0.0'
// used just to force zendesk to use 3.10
implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
...
}
Neither of these seem very clean. IMO the app module shouldn't know anything about OkHttp logging interceptor. Is there another option?
If Zendesk updated their library to use implementation for their OkHttp dependency would this solve the problem? Will Gradle let two dependencies use different versions of the same transitive dependency as long as they don't expose to the project as a whole?
with Gradle this works a little different ...
one can either enforce the version 3.10.0:
dependencies {
implementation group: 'com.zendesk', name: 'support-providers', version: '2.0.0'
}
configurations.all() {
resolutionStrategy.force "com.squareup.okhttp3:logging-interceptor:3.10.0"
}
or just exclude version 3.8.1 (which is leaving nothing but the desired version 3.10.0):
dependencies {
implementation ('com.zendesk:support-providers:2.0.0") {
exclude "com.squareup.okhttp3:logging-interceptor:3.8.1"
}
}

gradle searching jar in artifactory without version

My Gradle build has below dependency
| | +--- org.spire-math:spire_2.10:0.7.4
| | | +--- org.scala-lang:scala-library:2.10.2 -> 2.10.5
| | | +--- org.spire-math:spire-macros_2.10:0.7.4
| | | | +--- org.scala-lang:scala-library:2.10.2 -> 2.10.5
| | | | \--- org.scala-lang:scala-reflect:2.10.2 -> 2.10.5 (*)
| | | \--- org.scala-lang:scala-reflect:2.10.2 -> 2.10.5 (*)
| | \--- org.slf4j:slf4j-api:1.7.5 -> 1.7.10
I get the following error on running the build
* What went wrong:
Could not resolve all dependencies for configuration ':myProject
:compileClasspath'.
> Could not find scala-reflect.jar (org.scala-lang:scala-reflect:2.10.5).
Searched in the following locations:
http://company_intranet:port/artifactory/vr-trunk/org/scala-lang/scala-reflect/2.10.5/scala-reflect-2.10.5.jar
I am not sure why the jar is searched without the version
Could not find scala-reflect.jar
I see that the version number is not appended to the jar
The jar with the right version is available in the artifactory
The cache in my local machine was causing this error.
Once i used gradlew clean build -x test --refresh-dependencies , it worked

Gradle dependency gets excluded from EAR

I have a gradle project with several subprojects. and I have defined several dependencies in library with version numbers and aliases in the root project. Now whenever subproject needs it it can define that alias with what level it wants e.g. compile.
root
- build.gradle
- libraries
- xercesImpl: 'xerces:xercesImpl:2.8.1',
- xalan: 'xalan:xalan:2.7.1',
\ project1 : ear
- build.gradle
- ear project('project2')
\ project2 : jar
- build.gradle
- compile libraries.xercesImpl
- compile libraries.xalan
Since I have defined xercesImpl and xalan as dependencies of project1 and project1 is included in project2 ear those jars should be added to EAR, but it's getting excluded from EAR. I checked and transitive dependencies is not declared anywhere as false.
I have printed dependency tree with gradlew and it prints only above 2 jars as excluded others are included:
+--- org.hibernate:hibernate:3.1.3
| +--- commons-logging:commons-logging:1.0.4 -> 1.1
| | +--- log4j:log4j:1.2.12
| | +--- logkit:logkit:1.0.1
| | +--- avalon-framework:avalon-framework:4.1.3
| | \--- javax.servlet:servlet-api:2.3
| +--- ehcache:ehcache:1.1
| | \--- commons-logging:commons-logging:1.0.4 -> 1.1 (*)
| +--- cglib:cglib:2.1_3
| | \--- asm:asm:1.5.3
| +--- asm:asm:1.5.3
| +--- asm:asm-attrs:1.5.3
| +--- commons-collections:commons-collections:2.1.1 -> 3.1
| +--- dom4j:dom4j:1.6.1
| +--- javax.transaction:jta:1.0.1B
| \--- antlr:antlr:2.7.6rc1 -> 2.7.6
+--- net.sf.ehcache:ehcache:2.9.0
| \--- org.slf4j:slf4j-api:1.7.7
+--- xerces:xercesImpl:2.8.1 (*)
+--- xalan:xalan:2.7.1 (*)

Grails exclude a jar from dependency

I have a grails 2.4.5 project with dependency section in BuildConfig.groovy as:
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg.
//compile ":memcached:1.0.3.2"
//compile ":grails-melody:1.47.2"
// runtime 'mysql:mysql-connector-java:5.1.16'
compile "org.grails:grails-core:2.2.2.BUILD-SNAPSHOT", { exclude "grails"} // added for 2.2.1 support of inner class enums (http://jira.grails.org/browse/GRAILS-9627)
}
The program on running gives this error:
Fatal error during compilation org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/C:/Users/Rahulserver/.m2/repository/org/grails/grails-project-api/2.4.5/grails-project-api-2.4.5.jar!/org/codehaus/groovy/grails/compiler/Grailsc.class]; nested exception is java.lang.IncompatibleClassChangeError: class org.springframework.core.type.classreading.ClassMetadataReadingVisitor has interface org.springframework.asm.ClassVisitor as super class
I did some research on error java.lang.IncompatibleClassChangeError: class org.springframework.core.type.classreading.ClassMetadataReadingVisitor has interface org.springframework.asm.ClassVisitor as super class
And found here that with spring 3.2 above, we don't need any spring-asm explicitly.
The dependency report on my project shows that org.grails:grails-core:2.2.2.BUILD-SNAPSHOT depends on spring-asm:
+--- org.grails:grails-core:2.2.2.BUILD-20130226.093746-1
| \--- commons-collections:commons-collections:3.2.1
| \--- commons-io:commons-io:2.1
| \--- org.springframework:spring-context:4.0.9.RELEASE
| \--- org.springframework:spring-beans:4.0.9.RELEASE
| \--- org.springframework:spring-aspects:3.1.4.RELEASE
| \--- org.springframework:spring-expression:4.0.9.RELEASE
| \--- org.springframework:spring-asm:3.1.4.RELEASE
| \--- org.springframework:spring-context-support:4.0.9.RELEASE
| \--- org.springframework:spring-core:4.0.9.RELEASE
| \--- org.springframework:spring-web:4.0.9.RELEASE
| \--- aopalliance:aopalliance:1.0
| \--- org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final
| \--- commons-validator:commons-validator:1.3.1
| \--- org.springframework:spring-aop:4.0.9.RELEASE
So what should I do to resolve this conflict? Is there any way I could avoid the
org.springframework:spring-asm:3.1.4.RELEASE dependency?
What is a dependency on Grails 2.2.x doing in a 2.4.5 project?
The bug you are referring to in the comments was fixed in 2.3-M1, so is not present in 2.4.x.
I suspect your errors will disappear if you remove the explicit dependency on grails-core 2.2.2*.

Resources