如何为Spring应用程序创建具有所有依赖关系的胖子

时间:2019-12-16 05:11:35

标签: java spring jar executable-jar

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

UPD: 我的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

1 个答案:

答案 0 :(得分:2)

如果它是一个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>

此插件背后的想法是,春季靴子实际上并不是一个胖子,而是更复杂的东西。它的内部结构看起来并不像jar(例如,所有依赖项都是BOOT-INT/lib中的JAR包,因此您有jar内部jar)

或者,如果它是一个没有Spring Boot的spring项目,那么显然您根本不需要在类路径中使用Spring Boot jar。在这种情况下,您也不需要spring-boot-maven-plugin