【问题标题】:spring boot maven how to build both war and jar filespring boot maven如何构建war和jar文件
【发布时间】:2016-05-01 06:41:13
【问题描述】:

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

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

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

【问题讨论】:

    标签: maven spring-boot pom.xml


    【解决方案1】:

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

    当 Spring Boot 重新打包一个 war 文件时,它是可执行的。这意味着,您可以使用java -jar 运行战争,而不是尝试同时构建 jar 和战争。

    【讨论】:

      【解决方案2】:

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

      完成。

      【讨论】:

        猜你喜欢
        • 2016-04-22
        • 2017-10-13
        • 2017-10-28
        • 2018-06-16
        • 2011-08-09
        • 2018-11-07
        • 1970-01-01
        • 2020-11-01
        • 1970-01-01
        相关资源
        最近更新 更多