Reputation: 23
I'm looking for a solution to make a common-spring-boot component to avoid the many duplicate files in several different components. i.e: There are 2 components(say, C1 and C2) built based on the spring-boot to provide the RESTful services, the swagger is used in both of them to show the generated API docs, and the generated fat jar looks like:
C1.jar
│ logback-access.xml
│ logback.xml
├─com.host.c1.*
├─db
├─lib
├─logging
│ logback-access-base.xml
│ logback-base.xml
├─META-INF
├─org. springframework.boot.loader
└─static
└─swagger.ui.app
C2.jar is almost the same with C1.jar, but the different package name (com.host.c2.*).
We can see that there are many duplicate file in these 2 components, I'd like to make a common component to include all the common resources and configurations files to keep the component structure clean, say:
BaseComponent.jar
│ logback-access.xml
│ logback.xml
├─com.host.base.*
├─lib
├─logging
│ logback-access-base.xml
│ logback-base.xml
├─META-INF
├─org. springframework.boot.loader
└─static
└─swagger.ui.app
In this way, the new C1 and C2 based on the BaseComponent would look like:
C1.jar
├─com.host.c1.*
├─lib ... BaseComponent.jar
C2.jar
├─com.host.c1.*
├─lib ... BaseComponent.jar
I tried this manually but it did not work ,but the following excepiton when run the modified jar:
java.lang.IllegalStateException: Unable to open nested compressed entry lib/BaseComponent.jar
at org.springframework.boot.loader.jar.JarFile.getNestedJarFileFromFileEntry(JarFile.java:342)
at org.springframework.boot.loader.jar.JarFile.getNestedJarFile(JarFile.java:312)
at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchive(JarFileArchive.java:87)
at org.springframework.boot.loader.archive.JarFileArchive.getNestedArchives(JarFileArchive.java:74)
at org.springframework.boot.loader.ExecutableArchiveLauncher.getClassPathArchives(ExecutableArchiveLauncher.java:56)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:60)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:45)
Is it possible to achieve it? Any suggestion/solution would be appreciated.
Upvotes: 2
Views: 2366
Reputation: 116081
Spring Boot requires nested entries to be stored without any compression. The problem is that you've built the jar manually and, as a result, have compressed the nested jar file. Spring Boot's Maven and Gradle plugins will take care of this for you. Alternatively, if you want to continue to build the jar by hand, it should be possible to disable compression but it'll depend on the tool you're using. For example if you're using zip
on OS X -Zstore
will make it store the entries with no compression.
As an aside, the methods shown in your stack trace only exist in older versions of Spring Boot (1.1.1 is the latest version that has them). It's unrelated to this issue, but I'd recommend upgrading to 1.1.7.
When a Spring Boot app is performing component scanning, trying to load configuration files, etc it will use the classpath to do so, i.e. it doesn't matter if the files and classes are in the main jar file or one of the nested jar files as they're all on the classpath. As long as you follow the same approach for any resource loading that you do in your own code (use classpath:/
prefixed resource locations) you shouldn't have any problems.
Upvotes: 2
Reputation: 23
Andy, Yes, I know Spring Boot's Maven plugin can take care of it. I just want to check if it works in this way. I'd like to know if Spring Boot can do the following things from a nested jar file well:
BTW, You are right, the used Spring Boot is 1.0.2.RELEASE, it's old, I'll upgrade it later.
Upvotes: 0