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

Pan*_*arn 3 pom.xml maven spring-boot

您好,我需要知道如何通过 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>
        ...
Run Code Online (Sandbox Code Playgroud)

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

Exception in thread "main" java.lang.NoClassDefFoundError: org.springframework/boot/SpringApplication
Run Code Online (Sandbox Code Playgroud)

And*_*son 5

问题是 Spring Boot 的 Maven 插件不参与 jar 的创建,因此它没有被设置为可执行文件。

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


Pyt*_*try 5

@andywilkenson 的答案绝对是我这样做的首选方法,但无论如何,这里是如何做到这一点:D

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

脚步

  1. 使包装值等于主要工件的包装类型(“jar”)。
  2. 添加标准配置的 spring-boot-maven-plugin。
  3. 为 Maven war 和 jar 插件添加显式声明。
  4. 为每个插件创建执行配置,否则只有与项目打包匹配的插件才会运行。配置只需放置在该插件的执行中。
  5. 将“分类器”添加到次要工件。这很重要,因此我们知道哪个是主要的,哪个是次要的工件(例如“源”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>
Run Code Online (Sandbox Code Playgroud)

完毕。