Spring boot application can't see beans when run as a jar - java

My spring boot web app works when run it in eclipse but when I try to run as an executable jar it fails to register any of the beans.
The configuration is all annotated - there are no xml config files.
I used mvn clean compile and mvn package commands to generate the jar.
Has anyone had a similar problem or have any ideas?
Thanks

Executable jars can not include nested jars, so it is necessary to workaround that limitation. A common approach is to shade the jars (i.e. unpacking all jars, and then packing in to single jar).
Spring-boot takes a different approach that relies on a custom maven repackaging plugin and handling by the Spring Boot Loader module. It generally just works, but per the documentation:
The spring-boot-starter-parent POM includes configuration
to bind the repackage goal. If you are not using the parent POM you
will need to declare this configuration yourself. See the plugin
documentation for details.
More information:
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#getting-started-first-application-executable-jar
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#executable-jar
My guess is that you are are you either not using the spring-boot maven plugin, or are not using the spring-boot parent POM. If that's not the issue, then you will need to post more information.

I have now resolved the problem and the app is working as a standalone jar.
Turns out the project folder structure was wrong - such that most of the app was unreachable!

Related

Configure maven to package fat jar without unpacking dependencies in a vertx project, like spring boot does

How can I tell maven to include dependency jar files while building a fat jar, rather than unpacking them to .class files?
I have a vertx 3.6.0 project producing a fat jar. I am using vertx-maven-plugin:1.0.13, and I run mvn clean package to build. In order to take advantage of Veracode SCM (static scanning), the dependencies inside my fat jar have to be intact, meaning the original dependency jar files have to be contained inside my fat jar. Maven is unpacking all dependencies though, so all I have are class files.
We have another spring boot project which works as expected. It seems the final spring boot repackage goal puts all the dependency jar files in the BOOT-INF dir inside the jar file.
Final 2 goals using vertx-maven-plugin:
maven-jar-plugin:2.4:jar
vertx-maven-plugin:1.0.13:package
Final 2 goals using spring-boot-maven-plugin:
maven-jar-plugin:2.4:jar
spring-boot-maven-plugin:2.1.2.RELEASE:repackage
I've searched the docs for vertx-maven-plugin and all over https://maven.apache.org and elsewhere without any luck so far.
Is there a way to get this same repackage behavior for a non spring boot app?
You can't do that with Vert.x because it doesn't do fancy classloading.
But since it's embeddable, you can create a SpringBoot app that just starts Vert.x. Then you'll get your dependency scanner working.
But I would first check with Veracode if it' can't inspect your POM or Gradle build file instead of scanning a JAR.
If your packaging requirement is for the veracode scan piece, you would probably try packaging your project artifacts into a tar, or zip using the maven-assembly plugin.
Veracode couldn't scan a dependency jar inside another jar for a non-spring boot project. So, you can try one of the following. i) tar packaging using maven-assembler plugin, or ii) convert it to spring-boot project if thats simpler and you could ,or iii) uber-jar creation using maven-shade plugin and need to make some configuration changes for not getting unpackaged to .class files. Still would suggest you can explore with the tar packaging option - (reference:) https://maven.apache.org/plugins/maven-assembly-plugin/

Why does maven jar project not contain it's dependencies?

I have recently been working with Spring Boot and all of the jar dependencies are packaged within the Spring Boot 'uber' jar.
I have recently made a regular jar application and none of the jars are packaged within the jar, but I am still able to execute the code within the java by calling specific classes (with main methods)
Can anyone shed some light? Any similar questions I see regarding this always speak to the shade plugin
EDIT:
I understand I didn't specify the shade plugin to create an uber jar, but could anyone explain why it's not needed for the majority of projects, and how the code is able to run if the dependencies are not there?

Start Spring Boot App from other application

I have a java application and a Spring Boot application. I want to MySpringBootApp.run() and MySpringBootApp.hereYouHaveSomeInfo(), so I want to call methods of the Spring Boot App but Spring Boot kind of processes my class and renames it, so I get a ClassNotFoundException in the other App.
Thanks for you help!
If I understand you correctly, you are building the executable version of the spring boot application.
This jar file cannot be used as a dependency.
You need to build the classical jar file without the dependencies and add this to your application's dependency list.
I suppose you build with maven. The target folder will contain two files:
a yourapp-<version>.jar and
a yourapp-<version>.jar.original which is substanially smaller.
The .jar File is not suitable for inclusion as a dependency. You need the .original file. I suggest, if you need both, that you setup a project creating the .original file as regular .jar target, without the spring-boot plugin. And you add another project, with the spring-boot plugin, that requires the "simple" .jar file as dependency and has the spring-boot plugin enabled.
Your second application will also list the "simple" jar file as a dependency, the fat jar can be delivered as usual.

Why spring boot generates jar or war file with .original extension?

Why after building spring boot app, it generates two jar or war files with .original extension? I use spring boot maven build plugin.
For example:
application.jar
application.jar.original
The answer is that you are using repackage goal in your spring-boot-maven-plugin.
So, What it does?
Maven first builds your project and packages your classes and resources into a WAR (${artifactId}.war) file.
Then, repackaging happens. In this goal, all the dependencies mentioned in the pom.xml are packaged inside a new WAR (${artifactId}.war) and the previously generated war is renamed to ${artifactId}.war.original.
I assume that you're using the spring boot maven build plugin. This behavior is documented here: https://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html

Include specific packages in spring boot application build

Is there a way to include specific packages during a spring boot application build? I have used spring-boot-maven plugin.
I understand it's not very customary to have classes in the same project which we don't want in the build, but I want to check if this is possible at all?
Even if a spring-boot executable jar can be built using any other way (i.e. by not using the spring-boot-maven plugin) I'd be delighted to know.

Categories