当前位置: 首页 > 知识库问答 >
问题:

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

赵昊阳
2023-03-14

我正在尝试使用maven-assembly-plugin构建我的Spring Boot应用程序的一个包罗万象的fat jar。我运行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个答案

姬康平
2023-03-14

要构建一个包含启动Spring Boot应用程序所需的所有内容的JAR,最简单的方法是依赖于Spring-boot-maven-pluginrepackage目标。
当然,您可以“手边”做同样的事情(例如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,而不包括pom.xml中的repackage执行,方法是从命令行指定目标:

mvn package spring-boot:repackage
 类似资料:
  • 我试图使用maven插件为maven java项目添加一个源文件夹到Eclipse。 从http://mojo.codehaus.org/build-helper-maven-plugin/usage.html上的文档来看,这应该是正确的吧?文件夹target/sources/mygeneratedfiles存在。

  • 问题内容: 我需要能够构建go应用程序的不同版本;“调试”版本和普通版本。 这很容易做到;我只是有一个const DEBUG,它控制应用程序的行为,但是每次我需要在构建类型之间进行交换时,都必须编辑配置文件,这很烦人。 我正在阅读有关go build(http://golang.org/pkg/go/build/)和标签的信息,我想也许我可以这样做: config.go: config.debug

  • 我试图开发一个定制的Maven插件。我当前在执行单元测试时遇到了这个问题:堆栈跟踪如下: 根据我的研究,在为Maven 3.1.x构建时可能会发生这种情况,因为Maven从sonatype aether实现切换到了eclipse实现。 还有,有人知道是否有可能构建一个适用于Maven 3.0.x和3.1.x的插件吗?我不想在将来的某一天,当我们转向一个新版本的Maven时,再经历这一切。 我的PO

  • 我已经用Xtext 2.12编写了一个DSL,我的Xted2.12代码生成器从中产生了Java8的代码。我正在使用Eclipse Oxygen.3a。我从创建一个Xtext项目作为Eclipse插件开始,一切都很好,但是我现在希望一切都由Gradle构建。为此,我已经将Gradle性质添加到我的Xtext项目中,并启动了“gradle init”来生成build.gradle和settings.g

  • 请注意:在泽维尔的回答之后编辑的回答 我试图在Android Studio中为一个相同的应用程序项目使用不同的构建风格。然而,我似乎有一个可怕的时间配置它适当地工作。 步骤: null **我检查了分级设置,显然使用自动导入已经启用。尽管如此,对build.gradle文件进行更改不会自动更新构建变体。注意:我还尝试使用Build-Rebuild项目和/或Build-Make项目。我仍然必须关闭项

  • 所以这就是我试过的。有更好的方法吗?