如何使用maven插件正确构建Spring Boot远jar?

时间:2018-01-06 19:42:50

标签: java spring maven pom.xml

我试图使用maven-assembly-plugin来构建一个包含我的Spring Boot应用程序的全包胖罐。 我运行mvn clean包,没有创建jar。 我做错了什么?

我的pom:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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>com.github.vanroy</groupId>
    <artifactId>spring-data-jest-build</artifactId>
    <version>3.1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>Spring Data Jest Build</name>
    <description>Build configuration of Spring Data Implementation for Jest</description>
    <url>https://github.com/vanroy/spring-data-jest</url>

    <developers>
        <developer>
            <id>vanroy</id>
            <name>Julien Roy</name>
            <email>julien.vanroy@gmail.com</email>
            <url>http://github.com/vanroy</url>
            <timezone>+1</timezone>
            <roles>
                <role>Java Developer</role>
            </roles>
        </developer>
    </developers>

    <licenses>
        <license>
            <name>Apache License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0</url>
        </license>
    </licenses>

    <scm>
        <url>https://github.com/vanroy/spring-data-jest/</url>
        <connection>scm:git:ssh://git@github.com/vanroy/spring-data-jest.git</connection>
        <developerConnection>scm:git:ssh://git@github.com/vanroy/spring-data-jest.git</developerConnection>
    </scm>

    <properties>

        <!-- Java Version -->
        <java.version>1.8</java.version>
        <encoding>UTF-8</encoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>

        <!-- Dependencies version -->
        <springboot>2.0.0.M4</springboot>
        <spring>5.0.0.RELEASE</spring>
        <springdataes>3.0.0.RELEASE</springdataes>
        <jest>5.3.2</jest>
        <gson>2.8.0</gson>
        <awssigning>0.0.16</awssigning>
        <springcloudaws>1.2.1.RELEASE</springcloudaws>
        <jna>4.2.2</jna>
        <hamcrest>1.3</hamcrest>
        <junit>4.12</junit>
        <lombok>1.16.8</lombok>
        <mockito>1.10.19</mockito>
        <logback>1.1.9</logback>
        <aws>1.11.132</aws>

        <elasticsearch>5.5.0</elasticsearch>

        <!-- Version of maven plugins -->
        <version.plugin.maven-compiler-plugin>3.6.1</version.plugin.maven-compiler-plugin>
        <version.plugin.maven-deploy-plugin>2.8.2</version.plugin.maven-deploy-plugin>
        <version.plugin.maven-surefire-plugin>2.20</version.plugin.maven-surefire-plugin>
        <version.plugin.maven-source-plugin>2.1.2</version.plugin.maven-source-plugin>
        <version.plugin.maven-javadoc-plugin>2.7</version.plugin.maven-javadoc-plugin>
        <version.plugin.maven-release-plugin>2.5</version.plugin.maven-release-plugin>
        <version.plugin.nexus-staging-maven-plugin>1.6.7</version.plugin.nexus-staging-maven-plugin>
        <version.plugin.maven-gpg-plugin>1.6</version.plugin.maven-gpg-plugin>
        <version.plugin.maven-spring-boot-plugin>1.5.3.RELEASE</version.plugin.maven-spring-boot-plugin>

        <!-- Source encoding -->
        <source.encoding>UTF-8</source.encoding>

    </properties>

    <dependencyManagement>
        <dependencies>

            <!-- Spring Data Elasticsearch -->
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-elasticsearch</artifactId>
                <version>${springdataes}</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.elasticsearch</groupId>
                        <artifactId>elasticsearch</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <version>${elasticsearch}</version>
            </dependency>

            <!-- JEST -->
            <dependency>
                <groupId>io.searchbox</groupId>
                <artifactId>jest</artifactId>
                <version>${jest}</version>
                <exclusions>
                    <exclusion>
                        <groupId>commons-logging</groupId>
                        <artifactId>commons-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>${gson}</version>
            </dependency>

            <!-- Test -->
            <dependency>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-library</artifactId>
                <version>${hamcrest}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-core</artifactId>
                <version>${hamcrest}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
                <version>${mockito}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>${logback}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring}</version>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>commons-logging</groupId>
                        <artifactId>commons-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>com.jolira</groupId>
                <artifactId>onejar-maven-plugin</artifactId>
                <version>1.4.4</version>
            </dependency>


        </dependencies>

    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${version.plugin.maven-compiler-plugin}</version>
                <configuration>
                    <compilerArgs>
                        <arg>-Xlint</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>${version.plugin.maven-deploy-plugin}</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${version.plugin.maven-surefire-plugin}</version>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${version.plugin.maven-spring-boot-plugin}</version>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-libs-snapshot</id>
            <url>https://repo.spring.io/libs-snapshot</url>
        </repository>
    </repositories>

    <!-- Deploy snapshots to the Sonatype OSSRH (OSS Repository Hosting) -->
    <distributionManagement>
        <downloadUrl>https://github.com/vanroy/spring-data-jest</downloadUrl>
        <snapshotRepository>
            <id>ossrh</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </snapshotRepository>
        <repository>
            <id>ossrh</id>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>

    <profiles>
        <profile>
            <id>default</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <modules>
            </modules>
        </profile>
        <profile>

            <id>release</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <version>${version.plugin.maven-source-plugin}</version>
                        <executions>
                            <execution>
                                <id>attach-sources</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>${version.plugin.maven-javadoc-plugin}</version>
                        <executions>
                            <execution>
                                <id>attach-sources</id>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-release-plugin</artifactId>
                        <version>${version.plugin.maven-release-plugin}</version>
                        <configuration>
                            <autoVersionSubmodules>true</autoVersionSubmodules>
                            <useReleaseProfile>false</useReleaseProfile>
                            <releaseProfiles>release</releaseProfiles>
                            <goals>deploy</goals>
                        </configuration>
                    </plugin>
                    <plugin>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>com.dfs.ace.catalog.api.SampleJestApplication.class</mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                        <executions>
                            <execution>
                                <id>make-assembly</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>

1 个答案:

答案 0 :(得分:2)

要构建一个包含启动Spring Boot应用程序所需内容的JAR,最简单的方法是依赖于repackage的目标。 spring-boot-maven-plugin
当然你可以做同样的事情&#34;在手边&#34; (例如使用maven-shade-plugin)但它可能容易出错并且在您的情况下是否真的需要?

所以这是pom.xml中所需的配置:

<?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">
    <modelVersion>4.0.0</modelVersion>
    <!-- ... -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.9.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

repackage绑定到package阶段,因此将在默认package任务后执行。
因此,运行此命令将完成这项工作:

mvn package 

请注意,您还可以创建应用程序的可执行JAR 通过从命令行指定目标,不在repackage中包含pom.xml执行:

mvn package spring-boot:repackage