如何在spring boot中创建单个可执行文件war

时间:2016-10-27 16:06:57

标签: maven spring-boot

我们正在使用spring boot开发spring cloud项目。我们的目标是创建一个可以使用java -jar运行的可执行战争。

我在SO上关注了几个帖子,并且能够通过1)添加“boot”分类器标签来生成可执行文件。 2)在spring-boot-maven-plugin

的执行阶段添加Repackage目标

现在通过这种方法我得到两个war文件: 一场不可执行但只能部署的战争 和另一个适合我要求的启动分类器的战争

有没有办法只生成可执行的战争?

我正在附加pom.xml以便于参考

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>

参考文献: One Spring Boot project, deploy to both JAR or WAR

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

1 个答案:

答案 0 :(得分:1)

感谢Stephane建议删除分类器并建议使用starter.io。

我原来的问题是我在目标中获得了两个war文件: 1)Root.war和另一个 2)discovery-service-boot.war

我猜,魔鬼是在maven-war-plugin配置中。我删除了warName和outputDirectory的xml标签后,我得到了可执行文件的战争。

我发布了最终的pom.xml,以帮助面临类似情况的其他人:

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