Spring Boot Maven如何同时构建war和jar文件

3

您好,我需要知道如何通过maven install一次性构建war和jar文件。

我需要使用war文件在Google应用引擎上部署,同时使用jar文件通过命令(java -jar ....)在Jenkins上执行。

以下是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文件,但如果我尝试通过以下命令执行jar文件 java -jar...就会出现错误。

Exception in thread "main" java.lang.NoClassDefFoundError: org.springframework/boot/SpringApplication
2个回答

7
问题在于Spring Boot的Maven插件没有参与到jar包的创建中,因此它没有被设置为可执行文件。
当Spring Boot重新打包war文件时,它会被设置为可执行文件。这意味着,您不需要尝试构建jar和war两个文件,只需使用java -jar命令来运行war文件即可。

5

答案由@andywilkenson提供,绝对是我首选的方法,但无论如何,以下是如何做到的:

您需要决定哪个可交付成果是“主要”工件。我建议使用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>

完成。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接