Is there a way to have a single Spring Boot project be packagable into both JAR and WAR without changing the pom.xml
or the application source?
I've read Converting a Spring Boot JAR Application to a WAR, but it converts the project to WAR and it loses the ability to be packaged as JAR.
I don't expect mvn package
to do both. What I want is something like mvn i-want-a-jar
and it would package the project as JAR. Or I could run mvn i-want-a-war
and it would package the project as WAR.
Is this possible?
Spring Boot can also be told to produce a WAR file, in which case you'll likely choose to deploy it to a web container such as Tomcat or Jetty.
JAR files allow us to package multiple files in order to use it as a library, plugin, or any kind of application. On the other hand, WAR files are used only for web applications. The structure of the archives is also different.
Run the mvn package command and you have successfully converted your spring boot application from JAR into a WAR file. You can safely deploy these WAR files on application servers like Tomcat, GlassFish etc. This article concentrates on the default form login implementation from Spring Boot and Spring Security.
There are spring-boot plugins for various build systems. Here is the one for maven: spring-boot-maven-plugin To execute the kind of fat *.jar you could simple run command: The other option is to ship your application as old-fashioned war file.
We can deploy these JARs with ease. But in unavoidable situations, you may need a WAR file. Each application runs on a separate port. That means maintaining a list of free ports for all the services to execute. It is annoying to stop the application. And then identify and execute kill -9 <PID>.
The war contains only binaries and resources that are required for web application The jar is a self-executable binary archive with an embedded tomcat server. It needs a pre-configured servlet container that runs a java web application either of Tomcat, Glassfish Server. How to generate builts jar or war in spring boot?
I managed to do it by adding
<packaging>${packaging.type}</packaging>
to the POM file and then setting different profiles for JAR and WAR:
<profiles> <profile> <id>jar</id> <properties> <packaging.type>jar</packaging.type> </properties> </profile> <profile> <id>war</id> <properties> <packaging.type>war</packaging.type> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> </dependencies> </profile> </profiles>
Now mvn package -P war
produces a WAR and mvn package -P jar
produces a JAR.
Another option is to create separate modules for JAR and WAR, but I didn't go that route.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With