【问题标题】:Running jar reacts differently than running in Intellij运行 jar 的反应不同于在 Intellij 中运行
【发布时间】:2018-08-25 09:41:29
【问题描述】:

我正在使用 maven 在 Spring Boot 中开发一个项目。 首先让我告诉你,在 intellij 中,一切运行良好,我可以打开网站,但是: 现在我要创建一个测试环境,所以我使用maven生成了一个Jar。 我已经选择了正确的弹簧配置文件,现在我正在命令行中使用此命令运行 jar:

java -jar .\pegusapps-dashboard-0.4.0-SNAPSHOT.jar --spring.profiles.active=ext-api-dev,dev

当我运行它时,spring boot 会启动并开始执行 intellij 中的正常工作,但是当他们编译我们使用 rest api 的部分时。它给了我这个错误:

Description:

Parameter 0 of constructor in com.pegusapps.dashboard.integration.tempo.TempoServiceImpl required a bean of type 'com.pegusapps.dashboard.integration.tempo.TempoRestApi' that could not be found.


Action:

Consider defining a bean of type 'com.pegusapps.dashboard.integration.tempo.TempoRestApi' in your configuration.

让我解释一下api的结构。 (这是一个jira rest api)

  • 我们使用一个接口来执行对其余 api 的所有调用。
  • 那么我们就有了api服务层的接口。
  • 然后我们有了服务实现,他们可以在其中访问对 api 进行所有调用的接口。

在服务的实现内部,我们自动装配构造函数中的第一个接口,这就是错误的来源(来自 cmd 窗口)。

提前致谢!

【问题讨论】:

  • 你检查过intellij-idea在运行jar时执行了什么命令吗?它应该在控制台输出的顶部。
  • @krizajb 它运行了一个非常长的命令,而我可以从中得到的,是将所有依赖项添加到命令行。
  • 看起来您没有使用 spring-boot-maven-plugin 创建 jar。该插件创建了一个包含所有依赖项的胖 jar。
  • @cool 这确实是答案,我在网上找到了这个,现在将尝试。

标签: java spring-boot intellij-idea cmd jira-rest-api


【解决方案1】:

您可能想尝试使用 Apache Maven Shade 插件 构建您的 JAR,如下所示。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>fully.qualified.MainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

【讨论】:

    【解决方案2】:

    如果你没有使用已经使用以下

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-19
      • 2021-06-12
      • 2020-09-30
      • 1970-01-01
      • 2019-06-17
      • 1970-01-01
      • 2017-05-14
      • 1970-01-01
      相关资源
      最近更新 更多