【问题标题】:Spring Boot Executable Jar File Without Dependencies没有依赖关系的 Spring Boot 可执行 Jar 文件
【发布时间】:2016-02-09 23:36:05
【问题描述】:

在没有依赖项的情况下构建 spring boot jar 文件的最简单方法是什么? 基本上我应该能够将依赖 jar 文件保存在单独的文件夹中。

目前我正在使用 spring boot maven 插件,但是,它会创建一个包含所有依赖项的 Fat jar 文件。

【问题讨论】:

    标签: spring-boot spring-boot-maven-plugin


    【解决方案1】:

    根本不要使用spring-boot-maven-plugin 并使用JAR 包装。这样构建不会将依赖项打包到 JAR 中。

    【讨论】:

      【解决方案2】:

      spring-boot-maven-plugin 具有重新打包选项,可将依赖项放入内部(制作 uber jar)

      您可以禁用重新打包或使重新打包的 .jar 与其他分类器一起使用 [2]

      1. http://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-maven-plugin.html

      2. http://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/repackage-classifier.html

      【讨论】:

        【解决方案3】:

        将 pom.xml 中的构建条目替换为

        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>3.1.1</version>
                    <executions>
                      <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                          <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                          <outputDirectory>${project.build.directory}/dependency_jar</outputDirectory>
                          <overWriteReleases>false</overWriteReleases>
                          <overWriteSnapshots>false</overWriteSnapshots>
                          <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                      </execution>
                    </executions>
                  </plugin>
            </plugins>
        

        在目标文件夹中会有一个dependency_jar 文件夹,其中包含所有依赖项jar,以及“project_name.jar”(fat jar)和“project_name.jar.original”(您的jar 文件代码)

        【讨论】:

        • 我已经尝试过了,但现在我得到了错误no main manifest attribute, in kiosk-core-0.1.0-SNAPSHOT.jar.original 我也尝试过运行java -Dspring.config.location=application.properties -jar kiosk-core-0.1.0-SNAPSHOT.jar --thin.root=./dependency_jar,但这也给出了同样的错误
        【解决方案4】:

        以下是我在How to Create an Executable JAR with Maven 上找到的解决方案, 您只需将它们放入您的插件中。

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>
                            ${project.build.directory}/libs
                        </outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>libs/</classpathPrefix>
                        <mainClass>
                            org.baeldung.executable.ExecutableMavenJar
                        </mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        

        【讨论】:

          猜你喜欢
          • 2017-11-26
          • 2011-04-14
          • 1970-01-01
          • 1970-01-01
          • 2022-08-20
          • 2016-05-12
          • 2018-08-27
          • 2018-10-03
          • 1970-01-01
          相关资源
          最近更新 更多