Spring Boot package web application to .jar - java

Why Spring Boot by default packaging web applications as .jar ? Are there any advantages over .war ?
Shouldn't be all web applications packed as .war in order to be deployed to real web server ?
I thought that .jar files are for desktop applications whereas .war files are for web applications. since .jar doesn't contain web application components as mentioned here:
Java war vs. jar - what is the difference?

You linked to a question about a WAR vs a JAR, while Spring boot's JAR is indeed a JAR, it contains more than what you usually put inside a JAR. Spring boot comes with an embedded servlet container, inside the JAR itself.
So the difference:
A JAR: Usually contains resources/libraries/desktop applications/...
A WAR: A web application that can be run on a web container
The Spring boot fat JAR: A web application that runs on its own web container
About your question:
Shouldn't be all web applications packed as .war in order to be deployed to real web server?
If you want to run a web application on an existing servlet container, you have to use a WAR file. But the Spring boot JAR comes with its own servlet container, so you can perfectly run it as well.
Which of the two you choose is your own choice. Even though Josh Long's mantra is "Make JAR, not WAR".

By default Spring boot will provide you with embedded Tomcat (in jar). Deploying such jar will make you less dependant on the environment and requires less configuration ("out of the box solution").
See very similar question and its answers.

I think whole point of springboot is speed of development with minimal configuration. So you may think that they provide out of the box solution , that server(tomcat or jetty ) is embedded in the jar.
Also micro-services on cloud are promoting embedded server in jar only so that server can be optimized for application only and there is no need to setup tomcat server for small application.
If you want you can create war of spring boot application
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html

Related

What happens during WAR packaging in spring boot? Can we have both external Tomcat and Embedded Tomcat for a single application?

I actually want to know what exactly happens during WAR packaging of a spring boot application using Maven. My specific interest would be to know whether the embedded tomcat dependency will be included or not while packaging app as WAR.
Also, If we have some tomcat config properties defined in application.properties and deploy the WAR file to external Tomcat which config will be taken into account while running the application? Like the config properties defined in server.xml of Tomcat server or the properties defined in application.properties?
My specific interest would be to know whether the embedded tomcat dependency will be included or not while packaging app as WAR
This is covered in the Spring Boot reference documentation:
If you use the Spring Boot build tools, marking the embedded servlet container dependency as provided produces an executable war file with the provided dependencies packaged in a lib-provided directory. This means that, in addition to being deployable to a servlet container, you can also run your application by using java -jar on the command line.
When you deploy a war to Tomcat, the configuration in server.xml is used rather than the server- and Tomcat-related configuration in application.properties. Specifically, none of your application's server.* properties will have any effect.

How to add Opneshift config maps files to a spring boot web built in tomcat

Below my project structure
I have multiple modules in one parent maven project.
Many of above modules can also run as independent project including some of them as Openshift applications. These are spring boot applications
Now I want to add one more module as Spring boot web application on Openshift that uses Spring web in build tomcat.
Also I have multiple property files which are specific to environment and I load them as Configmaps on Openshift.
Now when I am trying to deploy the new web application, it is not able to use the Configmaps. It error says property file not found. I am sure this is because the Configmaps are outside of the tomcat and application.
Can I have any work around as I don't want to put those property files in the war while building but should get added when I deploy or start the pod on Openshift.
In short I want something like I can put configmaps as files into the class path spring boot web application that uses in built tomcat.
Mount the ConfigMap-s as volumes at some path and then configure Spring Boot as described here.

What is the difference of Jetty Server and Embedding Jetty in my Application

I am building right now a Web Application, and generating a .war file.
How can I embed Jetty into it, so that this war can be a stand-alone application?
And that brings me to the key question, how is this different from deploying this war file into a Jetty Server?
Am I confusing something? Should I actually build a regular Java - jar -application, and start the server from a Main method, adding the Servlet and Handlers there?
My goal is to have a stand-alone java application, using a WebSocket Servlet, and I am using Jetty 8
Sounds like you want a self-executing WAR that has embedded-jetty in it.
The jetty project maintains such a project at
https://github.com/jetty-project/embedded-jetty-live-war

Spring boot application in windows server

I am planning to deploy a web application built on spring boot in windows server.
I want to use tomcat container.
Can I deploy the spring boot fat jar directly or is it recommended to deploy the war file.
please suggest how to deploy and the preferred method?
As Josh Long likes to say "Make Jar not War!" It really allows an application to have flexibility on where it can be run and allows for everything to be packaged as one artifact. Windows has no issue running the embedded Tomcat that is part of Spring Boot and that is exactly what it is doing when running it in your IDE. The one edge case to this is keeping the process running on the server. Normally in Windows you would do that by setting up a service and having that service run java -jar myapp.jar. I haven't personally seen it done so might take some playing around but it is possible.
A simple way to run a spring application in Windows Server is to run it as a service. You can do it using the winsw, that you download its .bin file here
winws download
Then, rename it to something like my-app.exe and create a XML file like this:
<service>
<id>my-app-service</id>
<name>my-app-service</name>
<description>Back end service for app</description>
<env name="HOME" value="YOUR_JAR_FILE_PATH"/>
<executable>java</executable>
<arguments>-Xrs -Xmx256m -jar "YOUR_JAR_FILE_PATH\YOUR_JAR_FILE.jar"</arguments>
<logmode>rotate</logmode>
</service>
Then, using the terminal, run:
my-app.exe install service
Your application is now a windows service and you can start\stop it in the tasks manager on the services tab.
Starting from the latest Windows versions, you could also deploy your Spring Boot app inside a Docker Windows Container. I wrote a complete guide: https://blog.codecentric.de/en/2017/04/ansible-docker-windows-containers-spring-boot/ (as already mentioned, Tomcat is already embedded in Spring Boot).
Spring boot internally has a tomcat server.
If you want to deploy it on tomcat then while building with maven build it as war.
If you want to deploy it has inependent application then build has jar and then place it in some folder and run it using below commands java -jar yourjarname.
Apache tomcat is a web container you cannot deploy a jar in tomcat server. If you created a web application then export your application as war file and put it in tomcat webapp directory, start the server and your war will be deployed.
How to deploy created .jar file in Apache Tomcat server in Eclipse IDE?

How to deploy JAR file on JBoss?

I have a Java application using Spring, Hibernate and JMX.
Now i wanna deploy it on JBoss. I exported it to a Jar file and copy to the deploy folder of JBoss. But when i start JBoss, that app isn't deployed.
The error I saw that JBoss can't find out library files of Spring and Hibernate.So guys how to deploy a file Jar on JBoss ?
Java enterprise applications are not deployed as jar files, but as .war files (for web application) or .ear files (for complete enterprise applications, including EJB).
You would probably be looking at packaging your app as a .war file. Even if it's not a "web application", it needs to be packaged as one to be deployed to a Java enterprise/web container like JBOSS.
I would suggest browsing the tutorial available from Oracle to learn more.

Categories

Resources