One Spring Boot project, deploy to both JAR or WAR

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.


What's wrong with a WAR file that's executable? Isn't that what you really need?

P.S. like
java -jar name.war


We've recently had a similar requirement, where an existing Spring Boot based project that was originally packaged as an executable Jar needed to support Tomcat and WildFly deployments.

Due to some dependencies used in this project (for example WebJars), a simple switch to WAR package wasn't an option since some of those dependencies were required for WildFly (VFS support) but not for other deployment.

The solution was to restructure the project modules in a way that core module contained the actual project but without having Spring Boot’s plugin applied, while several package modules would depend on core module and configure deployment artifact specifics (Boot and other plugins, deployment specific dependencies etc.).

That way project build was able to generate multiple deployment artifacts (Boot's executable JAR, traditional WAR and WildFly specific WAR) in a single build run.

In case anyone finds this useful, the sample project to demonstrate the approach is available on Github. The project can be built by either Gradle or Maven.