Spring Boot Maven 如何同时构建 War 和 JAR 文件



您好,我需要知道如何通过 mavan 安装一次构建 war 和 jar 文件

我需要使用战争文件在谷歌应用程序引擎上部署,并使用 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可以创建战争和jar文件,但是如果我尝试通过这个命令java -jar执行jar文件...它显示错误

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

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

当 Spring 引导重新打包战争文件时,它将被设置为可执行。这意味着,与其尝试同时构建罐子和战争,不如使用 java -jar 运行战争。

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

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

步骤

  1. 使包装值等于主工件("jar")的包装类型。
  2. 添加具有标准配置的 spring-boot-maven-plugin。
  3. 为 maven war 和 jar 插件添加显式声明。
  4. 为每个插件创建执行配置,否则只会运行与项目打包匹配的插件。只需将配置放置在该插件的执行中。
  5. 将"分类器"添加到辅助项目。这很重要,所以我们知道哪个是主要的,哪个是次要的工件(如"源"罐)。

最终结果

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

做。

最新更新