【问题标题】:Making an executable jar with Maven/Spring/Eclipse使用 Maven/Spring/Eclipse 制作可执行 jar
【发布时间】:2022-02-01 16:01:23
【问题描述】:

所以我已经尝试了这些:

  • 在 pom.xml 中添加插件
  • 删除 .m2 文件夹以在作为 Maven 构建运行时执行干净的包...
  • 运行 mvn clean(在 Eclipse IDE 的项目文件夹上)
  • 再次关闭并运行 eclipse。

问题是:当我在 Eclipse 上将主类作为 Java 应用程序运行时,Eclipse 控制台显示应用程序以 spring 标志启动。但是当我进行 Maven 构建或将项目提取为 .jar 文件并尝试执行 java -jar 时,会发生异常:

线程“main”中的异常 java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication 在 br.com.bancodigimais.keypaypagamentos.portalpos.PortalPosApplication.main(PortalPosApplication.java:34) 引起:java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication at java.base

为什么会出现这种情况?我的意思是......有可能让这个项目将jar构建为可执行文件吗?当你使用java -jar 命令看到spring logo 启动应用等等...

pom.xml:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>Portal-POS</groupId>
    <artifactId>Portal-POS</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>            
                </configuration>
            </plugin>
     <plugin> 

                <!-- Building an executable jar -->

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                  <archive>
                    <manifest>

                    <!-- give full qualified name of your main class-->
                      <mainClass>br.com.bancodigimais.keypaypagamentos.portalpos.PortalPosApplication</mainClass>

                    </manifest>
                  </archive>
                </configuration>
    </plugin>
 
            
        </plugins>
    </build>

    <dependencies>

        <dependency>
            <groupId>com.liferay</groupId>
            <artifactId>com.liferay.gradle.plugins.jasper.jspc</artifactId>
            <version>2.0.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
            <version>2.1.3.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-slf4j-impl</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>2.1.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.9.7</version>
        </dependency>

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

        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10.1</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>


        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.4.0</version>
        </dependency>

        <dependency>
            <groupId>net.sf.supercsv</groupId>
            <artifactId>super-csv-java8</artifactId>
            <version>2.4.0</version>
        </dependency>

        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.7.0</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.3.6</version>
        </dependency>

        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.5</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
            <scope>provided</scope>
        </dependency>


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

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

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5.10</version>
        </dependency>

        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-core</artifactId>
            <version>3.3.6</version>
        </dependency>

        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
    </dependencies>
</project>

【问题讨论】:

    标签: spring-boot maven executable-jar


    【解决方案1】:

    maven-jar-plugin 将创建一个仅包含您的应用程序类和资源的 jar 文件。 jar 文件将不包含运行应用程序所需的其他代码,例如包含 org.springframework.boot.SpringApplication 类的 Spring Boot 代码。为此,您需要使用 java -jar &lt;jar-file-path&gt; -cp &lt;list of all dependency jar files&gt; 列出所有依赖项 jar 文件。

    Spring Boot 通过将应用程序代码和所有依赖的 jar 打包到一个 jar 文件中来支持可执行 jar 文件。它还会自动创建MANIFEST.MF 条目,因此您不需要在pom.xml 中进行该配置。在Spring Boot documentationSpring Boot Maven Plugin documentation 中有更多相关信息。

    【讨论】:

      猜你喜欢
      • 2015-12-10
      • 1970-01-01
      • 2019-08-05
      • 2017-11-21
      • 2017-08-30
      • 1970-01-01
      • 1970-01-01
      • 2014-09-20
      • 1970-01-01
      相关资源
      最近更新 更多