Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One Spring Boot project, deploy to both JAR or WAR

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?

like image 772
imgx64 Avatar asked May 26 '14 10:05

imgx64


People also ask

Does spring boot create jar or war?

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.

Should I use jar or war?

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.

How to deploy a Spring Boot application from Jar to WAR file?

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.

How do I run a fat jar application in Spring Boot?

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.

Do I need a WAR file to deploy my jar?

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>.

What is the difference between Jar and war in Java?

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?


1 Answers

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.

like image 77
imgx64 Avatar answered Sep 20 '22 00:09

imgx64