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

时间:2016-01-25 02:56:22

标签: maven spring-boot pom.xml

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

我需要在google app引擎和jar文件上使用war文件进行部署,以便在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文件但是如果我尝试通过这个命令执行jar文件java -jar ... 它显示错误

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

2 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:2)

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

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

STEPS

  1. 使包装值等于主要工件('jar')的包装类型。
  2. 使用标准配置添加spring-boot-maven-plugin。
  3. 为maven war和jar插件添加显式声明。
  4. 为每个插件创建执行配置,否则只运行与项目包装匹配的插件。配置只需要放在该插件的执行中。
  5. 向辅助工件添加“分类器”。这很重要,所以我们知道哪个是主要的,哪个是辅助工件(比如'sources'jar)。
  6. 最终结果

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

    完成。