有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何为spring应用程序创建包含所有依赖项的胖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) 个答案

  1. # 1 楼答案

    如果它是一个spring boot项目(我假设它是,因为异常声明:类org/springframework/boot/SpringApplication丢失;这个类显然属于spring boot项目),那么您不需要使用maven shade插件

    而是使用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项目,那么显然在类路径中根本不需要spring boot JAR。在这种情况下,您也不需要spring-boot-maven-plugin