【问题标题】:How to create fat jar with all dependencies for spring application如何为spring应用程序创建包含所有依赖项的胖jar
【发布时间】:2020-04-08 13:48:48
【问题描述】:

我需要创建包含所有依赖项的 jar。我有使用数据库的spring项目,我需要在另一台只有java的计算机上启动spring boot应用程序。我只想拥有 jar 文件并以 java -jar jarname.jar 开头 如何创建这个 jar?

更新: 我现在的pom:

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from importer.app.pack.repository -->
    </parent>

    <properties>
        <!-- The main class to start by executing java -jar -->
        <start-class>app.App</start-class>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.2</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer
                                            implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                                    <transformer

                                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>app.App</mainClass>
                                    </transformer
>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <dependencies>

        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.8</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.1.RELEASE</version>
        </dependency>
    </dependencies>

当我尝试在终端中java -jar jarname.jar 我有例外: Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication

【问题讨论】:

  • 是spring boot应用吗?
  • 可能重复here
  • 这是一个 maven 项目吗?
  • 是的,在 intellij 中的 maven 项目
  • 删除pluginManagement 部分。添加一个包含spring-boot-maven-pluginplugins 部分,这正好。

标签: java spring jar executable-jar


【解决方案1】:

如果它是一个 spring boot 项目(我假设它是,因为异常状态:缺少类 org/springframework/boot/SpringApplication;这个类显然属于 spring-boot 项目)那么你不需要使用 maven阴影插件。

改为使用spring-boot-maven-plugin。有关可能的选项和配置的示例,请参阅 here

<build>
  ...
  <plugins>
    ...
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>2.2.2.RELEASE</version> // or your version
      <executions>
        <execution>
          <goals>
            <goal>repackage</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    ...
  </plugins>
  ...
</build>

这个插件背后的想法是,spring boot 并不是一个真正的胖罐子,而是更复杂的东西。它的内部结构看起来并不像 jar(例如,所有依赖项都是 BOOT-INT/lib 中的 JAR 包,所以你有 jar insider jar)

或者,如果它是一个没有 spring boot 的 spring 项目,那么显然你根本不需要 classpath 中的 spring boot jars。在这种情况下,您也不需要spring-boot-maven-plugin

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 2019-10-08
    • 1970-01-01
    • 2021-01-04
    • 2011-08-10
    相关资源
    最近更新 更多