Search code examples
spring-bootmaven-shade-pluginauto-value

LogFactory ClassNotFoundException with Google AutoValue and spring-context-support


A Spring boot project depends on auto-value 1.1 and spring-context-support 4.2.1. When I run it with mvn spring-boot:run, I see the following error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'velocityConfigurer' defined in class path resource [org/springframework/boot/autoconfigure/velocity/VelocityAutoConfiguration$VelocityWebConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: autovalue/shaded/org/apache/commons/logging/LogFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
    at com.example.SampleApplication.main(SampleApplication.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.springframework.boot.maven.RunMojo$LaunchRunner.run(RunMojo.java:418)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: autovalue/shaded/org/apache/commons/logging/LogFactory
    at org.apache.velocity.runtime.log.CommonsLogLogChute.init(CommonsLogLogChute.java:75)
    at org.apache.velocity.runtime.log.LogManager.createLogChute(LogManager.java:81)
    at org.apache.velocity.runtime.log.LogManager.updateLog(LogManager.java:269)
    at org.apache.velocity.runtime.RuntimeInstance.initializeLog(RuntimeInstance.java:871)
    at org.apache.velocity.runtime.RuntimeInstance.init(RuntimeInstance.java:262)
    at org.apache.velocity.app.VelocityEngine.init(VelocityEngine.java:93)
    at org.springframework.ui.velocity.VelocityEngineFactory.createVelocityEngine(VelocityEngineFactory.java:249)
    at org.springframework.web.servlet.view.velocity.VelocityConfigurer.afterPropertiesSet(VelocityConfigurer.java:120)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
    ... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: autovalue.shaded.org.apache.commons.logging.LogFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 31 common frames omitted

Looking at AutoValue's pom.xml. It seems Apache commons has been shaded, but why aren't the shaded classes loaded?

My application's pom.xml is on Github.


Solution

  • The problem is the VelocityAutoConfiguration class... For some reason, AutoValue includes Velocity in its shaded jar but does not relocate the references. When you include spring-context-support, some Velocity-related classes are loaded, which are picked up by VelocityAutoConfiguration, which then tries to load from the AutoValue included classes, crashing spectacularly.

    My solution so far has been adding

    @EnableAutoConfiguration(exclude = VelocityAutoConfiguration.class)