question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Springboot multi-module project missing project-level dependencies in build image.

See original GitHub issue

Description of the issue: Springboot application with a docker image built by JIB and with project-level dependencies does not appear to be able to find classes from project-level dependencies.

Caused by: java.lang.ClassNotFoundException is thrown for classes from project-level dependencies.

And beans etc aren’t loaded from project-level dependencies.

Expected behavior:

Application runs in a docker container

docker run -p 9081:9081 my.registry/platform/images/jib-example-spring-boot-multi-module:latest

in the same way as running

java -jar application.jar

Exposed end points should be reachable.

GET /

GET http://localhost:9081/api-component

GET http://localhost:9081/classpath

Steps to reproduce:

  1. Clone my example project https://github.com/australianfrog/spring-boot-multi-module

  2. Change [my.registry] in the parent pom.xml to your registry. You’ll actually have to change the <from><image>... to your image.

  3. Install Maven if not installed.

  4. Execute mvn compile jib:build from commandline.

  5. Run application with docker docker run -p 9081:9081 [image]

Environment:

jib-maven-plugin Configuration:

<plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>${jib-maven-plugin.version}</version>
                <configuration>
                    <from>
                        <image>[my.registry]/platform/images/distroless-java:latest</image>
                    </from>
                    <to>
                        <image>[my.registry]/platform/images/jib-example-spring-boot-multi-module:latest</image>
                    </to>
                </configuration>
            </plugin>

Log output:

λ docker run -p 9081:9081 my.registry/platform/images/jib-example-spring-boot-multi-module:latest
Unable to find image 'my.registry/platform/images/jib-example-spring-boot-multi-module:latest' locally
latest: Pulling from platform/images/jib-example-spring-boot-multi-module
4e1edcbff92b: Already exists
bb2297ebc4b3: Already exists
3e010093287c: Already exists
240baf35cf21: Already exists
3369f2a21544: Already exists
2d65fa87bf34: Pull complete
461d1549d366: Pull complete
43289173696c: Pull complete
Digest: sha256:f404abf0e77b53982ada8aa4ac714dc4fdd00988e6a29f481b0ba4f949858c2c
Status: Downloaded newer image for my.registry/platform/images/jib-example-spring-boot-multi-module:latest

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.3.RELEASE)

2019-03-08 05:03:32.098  INFO 1 --- [           main] com.example.jib.JibApplication           : Starting JibApplication on 861564ce4ee4 with PID 1 (/app/classes started by root in /)
2019-03-08 05:03:32.102  INFO 1 --- [           main] com.example.jib.JibApplication           : No active profile set, falling back to default profiles: default
2019-03-08 05:03:32.169  INFO 1 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@50a7bc6e: startup date [Fri Mar 08 05:03:32 GMT 2019]; root of context hierarchy
2019-03-08 05:03:33.314  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9081 (http)
2019-03-08 05:03:33.343  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-03-08 05:03:33.343  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.31
2019-03-08 05:03:33.362  INFO 1 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib]
2019-03-08 05:03:33.449  INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-03-08 05:03:33.449  INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1282 ms
2019-03-08 05:03:33.539  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2019-03-08 05:03:33.542  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-03-08 05:03:33.543  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-03-08 05:03:33.543  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-03-08 05:03:33.543  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2019-03-08 05:03:33.577  WARN 1 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jibApplication': Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.example.jib.JibApplication] from ClassLoader [sun.misc.Launcher$AppClassLoader@3d4eac69]
2019-03-08 05:03:33.583  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-03-08 05:03:33.614  INFO 1 --- [           main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-03-08 05:03:33.625 ERROR 1 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jibApplication': Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.example.jib.JibApplication] from ClassLoader [sun.misc.Launcher$AppClassLoader@3d4eac69]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:558) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
        at com.example.jib.JibApplication.main(JibApplication.java:21) [classes/:na]
Caused by: java.lang.IllegalStateException: Failed to introspect Class [com.example.jib.JibApplication] from ClassLoader [sun.misc.Launcher$AppClassLoader@3d4eac69]
        at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:758) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.util.ReflectionUtils.doWithLocalFields(ReflectionUtils.java:690) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.buildResourceMetadata(CommonAnnotationBeanPostProcessor.java:355) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.findResourceMetadata(CommonAnnotationBeanPostProcessor.java:339) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(CommonAnnotationBeanPostProcessor.java:298) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1022) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        ... 15 common frames omitted
Caused by: java.lang.NoClassDefFoundError: Lcom/example/jib/HelloJibFromApiComponent;
        at java.lang.Class.getDeclaredFields0(Native Method) ~[na:1.8.0_181]
        at java.lang.Class.privateGetDeclaredFields(Class.java:2583) ~[na:1.8.0_181]
        at java.lang.Class.getDeclaredFields(Class.java:1916) ~[na:1.8.0_181]
        at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:753) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
        ... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: com.example.jib.HelloJibFromApiComponent
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_181]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_181]
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) ~[na:1.8.0_181]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_181]
        ... 25 common frames omitted

Additional Information:

I posted another Stackoverflow post about this issue before I created this dedicated example project.

https://stackoverflow.com/questions/55056440/maven-multi-project-build-with-api-modules-does-not-seem-to-include-project-leve

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

11reactions
andxucommented, Mar 11, 2019

@australianfrog I have some experiences over multiple modules spring boot apps, in your parent pom.xml, you are referencingspring-boot-maven-plugin in each child pom, spring-boot-maven-plugin will try to archive a spring-boot run-able jar using repackage:

      <executions>
            <execution>
              <goals>
                <goal>repackage</goal>
              </goals>
              <configuration>
                <mainClass>${start-class}</mainClass>
              </configuration>
            </execution>
          </executions>

Finally, because you have no main class for project spring-boot-multi-module-api, you will get an empty jar, the original jar file is spring-boot-multi-module-api-0.0.1-SNAPSHOT.jar.original. using this jar as a dependency you will get no class error for com/example/jib/HelloJibFromApiComponent, to solve this, delete that plugin in parent pom, for using jib, you can either run mvn install -DskipTests in root folder and then runmvn jib:build in spring-boot-multi-module-launcher folder or run mvn package jib:build -pl spring-boot-multi-module-launcher -am -DskipTests in parent folder, you will also need to set jib.skip on project spring-boot-multi-module-api, see https://github.com/GoogleContainerTools/jib/issues/865

0reactions
chanseokohcommented, Mar 11, 2019

That’s a good idea to have links to examples on our landing page. I’ll put up a PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Started | Creating a Multi Module Project - Spring
This guide shows you how to create a multi-module project with Spring Boot. ... You can get a Maven build file with the...
Read more >
Maven multi-project build with API modules does not seem to ...
When I inspect my image using dive I have no SNAPSHOT DEPENDENCIES layer, and my project-level dependencies do not appear to be in...
Read more >
Multi-Module Project With Spring Boot
A quick guide to set up your spring boot multi module projects for your ... Provide the ability to build all modules with...
Read more >
Spring Boot Multi Module Project - Javatpoint
The pom.xml file of the parent project consists the list of all modules, common dependencies, and properties that are inherited by the child...
Read more >
Multi-Module Project With Spring Boot - Baeldung
In this quick tutorial, we'll show how to create a multi-module project with Spring Boot. First, we'll build a library jar that isn't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found