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.

Leave a Comment