spring boot maven 如何同时构建 war 和 jar 文件

spring boot maven how to build both war and jar file

你好,我需要知道如何通过 mavan install

一次性构建 war 和 jar 文件

我需要使用 war 文件在 google 应用程序引擎上部署,并使用 jar 文件通过命令在 jenkin 上执行 (java -jar ....)

这是pom.xml文件

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

<groupId>org.demo</groupId>
<artifactId>gae-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.1.0.BUILD-SNAPSHOT</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-legacy</artifactId>
        <version>1.0.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>net.kindleit</groupId>
        <artifactId>gae-runtime</artifactId>
        <version>${gae.version}</version>
        <type>pom</type>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.appengine</groupId>
        <artifactId>appengine-api-labs</artifactId>
        <version>${gae.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.appengine</groupId>
        <artifactId>appengine-api-stubs</artifactId>
        <version>${gae.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.appengine</groupId>
        <artifactId>appengine-testing</artifactId>
        <version>${gae.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies> 
<properties>
    <start-class>demo.Application</start-class>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.7</java.version>
    <m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
    <gae.version>1.8.8</gae.version>
    <gae.home>${settings.localRepository}/com/google/appengine/appengine-java-sdk/${gae.version}/appengine-java-sdk-${gae.version}</gae.home>
    <gae.application.version>test</gae.application.version>
</properties>
<build>
    <plugins>
    ...
       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <executions>
                <execution>
                    <phase>deploy</phase>
                    <goals>
                        <goal>deploy-file</goal>
                    </goals>
                    <configuration>
                        <packaging>jar</packaging>
                        <generatePom>true</generatePom>
                        <url>${project.distributionManagement.repository.url}</url>
                        <artifactId>${project.artifactId}</artifactId>
                        <groupId>${project.groupId}</groupId>
                        <version>${project.version}</version>
                        <file>${project.build.directory}/${project.artifactId}-${project.version}.jar</file>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        ...

这个 pom.xml 可以创建 war 和 jar 文件但是如果我尝试通过这个命令 java -jar ... 它显示错误

Exception in thread "main" java.lang.NoClassDefFoundError: org.springframework/boot/SpringApplication

问题是 Spring Boot 的 Maven 插件没有参与 jar 的创建,所以它还没有成为可执行文件。

当 Spring Boot 重新打包一个 war 文件时,它可以执行。这意味着,与其尝试构建 jar 和 war,不如使用 java -jar.

运行 war

@andywilkenson 的回答绝对是我首选的方法,但这是如何做到的 anyways:D

您需要决定哪个可交付成果是 'main' 工件。我建议使用 jar,因为使用 war 文件作为依赖项没有意义(使用 SpringBoot 可执行 jar 作为依赖项也没有意义)。

步骤

  1. 使包装值等于主要工件的包装类型('jar')。
  2. 添加标准配置的spring-boot-maven-plugin。
  3. 为 maven war 和 jar 插件添加显式声明。
  4. 为每个插件创建执行配置,否则只有匹配项目打包的才会运行。该配置只需要放在该插件的执行中。
  5. 给副神器加一个"classifier"。这很重要,因此我们知道哪个是主要工件,哪个是次要工件(比如 'sources' jar)。

最终结果

<?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"> <!--snip--> <packaging>jar</packaging> <!--snip--> <build> <plugins> <!--snip--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <executions> <execution> <id>war</id> <goals> <goal>war</goal> </goals> <!--No Explicit phase needed--> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> <classifier>war</classifier> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <!-- Do not declare an execution for jar, since it is the main artifact. Doing so makes maven think you are trying to add another supplemental file. --> </plugin> </plugins> </build> <!--snip--> </project>

完成。