Narasimha
Narasimha

Reputation: 1707

How to create a single executable war in spring boot

We are working on spring cloud project using spring boot. Our goal is to create an executable war that can be run using java -jar .

I followed couple of posts on SO and was able to generate the executable war by 1) adding "boot" classifier tag in . 2) Adding Repackage goal in execution phase for spring-boot-maven-plugin

Now with this approach I'm getting two war files : one war that is not executable but just deployable and another war with boot classifier that suits my requirements

Is there a way to just generate only the executable war?

I'm attaching the pom.xml for easy reference

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<artifactId>discovery-service</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>

<description>
    Discovery microservice to provide a service registry using Spring Cloud
    and Netflix Eureka for cloud native microservices.
</description>

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.2.3.RELEASE</version>
            **<configuration>
                <classifier>boot</classifier>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>**
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <outputDirectory>target</outputDirectory>
                <warName>ROOT</warName>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Brixton.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-parent</artifactId>
            <version>Brixton.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

References: One Spring Boot project, deploy to both JAR or WAR

Failed to load Main-Class manifest attribute while running java -jar

Upvotes: 3

Views: 17129

Answers (1)

Narasimha
Narasimha

Reputation: 1707

Thanks to Stephane for suggesting to remove the classifier and for suggesting to use starter.io.

My original problem was I was getting two war files in the target:

  1. Root.war and the other
  2. discovery-service-boot.war

I guess, the devil was in the maven-war-plugin config. After I removed the xml tags for warName and outputDirectory, I'm getting the executable war.

I'm posting the final pom.xml to benifit others facing similar situation:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>


    <artifactId>discovery-service</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <description>
        Discovery microservice to provide a service registry using Spring Cloud
        and Netflix Eureka for cloud native microservices.
    </description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.2.3.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-parent</artifactId>
                <version>Brixton.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

Upvotes: 4

Related Questions