【问题标题】:Spring Boot maven plugin cant find main class, Could not exec javaSpring Boot maven插件找不到主类,无法执行java
【发布时间】:2019-01-23 05:44:52
【问题描述】:

我有一个多模块 maven spring-boot maven 项目。其中一个子模块是一个存根(一个 Spring Boot 应用程序),我想在 myRealApp 的集成测试期间启动然后停止它。

Parent Pom
|   
|----myRealApp module(spring boot app)
|----stub-of-some-remote-rest-api module(This is also a spring-boot app)

这是 myRealApp 模块中 pom 文件的样子。其中也有所有的集成测试。它试图在集成测试阶段之前启动存根模块。但是当我在子模块目录中运行 maven 目标时出现错误:

 mvn integration-tests -X

错误:无法找到或加载主类 io.swagger.MyStub

org.apache.maven.lifecycle.LifecycleExecutionException: 失败 执行 目标 org.springframework.boot:spring-boot-maven-plugin:1.5.8.RELEASE:run (启动启动)项目:无法执行 java

我可以在调试模式下看到工作目录设置正确。

myRealApp 的 Pom:

         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>${spring.boot.mainclass}</mainClass>
            </configuration>
            <executions>
                <execution>
                    <configuration>
                        <workingDirectory>${project.parent.basedir}/module-of-my-stub/src/main/java</workingDirectory>
                        <mainClass>io.swagger.MyStub</mainClass>
                    </configuration>
                    <id>start-boot</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
                <execution>
                    <id>stop-boot</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>stop</goal>
                    </goals>
                </execution>
                <execution>
                    <goals>
                        <goal>build-info</goal>
                    </goals>
                </execution>
            </executions>

        </plugin>

也许我不允许在单个执行中设置workingDirectory,或者引用其他模块?

存根就像我的应用程序所依赖的远程休息服务一样,我在开发过程中使用了它,因为实际远程服务的测试环境不可靠并且有一半的时间下降了。所以决定在集成测试中也使用它。

【问题讨论】:

  • 为什么要配置 spring-boot-maven-plugin 不同的执行?如果您喜欢编写集成测试,这很容易做到……此外,您为什么要使用这样一个奇怪的目录 /my-stub/src/main/java ?请遵循 Maven 的约定和 Spring Boot 的约定....此外,您应该阅读有关测试 Spring Boot 应用程序的指南,因为不需要为 Spring Boot 应用程序编写存根...
  • @khmarbaise 存根就像我的应用程序所依赖的远程休息服务一样,我在开发过程中使用它,因为实际远程服务的测试环境不可靠并且下降了一半时间。那么为什么不将它也用于集成测试呢?为什么它是一个奇怪的目录?我试图从我的应用程序中找到存根模块的主类。恐怕你没听懂我的问题。
  • @khmarbaise 我更新了问题
  • 理想情况下,有一种方法可以将 module-of-my-stub 依赖项添加到 spring-boot-maven-plugin:run 目标使用的类路径中。尽管有一个useTestClasspath 选项,但看起来没有直接的方法。 This link 还讨论了另一种方法。
  • 反之亦然,但这是猜测。想法是通过将module-of-my-stub 的测试依赖项添加到myRealApp,然后将useTestClasspath 添加到现有的spring-boot-maven-plugin:run 配置中,这将使MyStub 在类路径中对其可见。否则,上面链接中的classesDirectory技巧应该能够将其指向module-of-my-stub下的target/classes目录。

标签: java maven spring-boot spring-boot-maven-plugin


【解决方案1】:

为了巩固一些针对问题的cmets,这些基本上遵循了this other answer中的建议。

关键是将classesDirectoryspring-boot-maven-plugin:run 目标设置为指向另一个项目下的target\classes 目录。

  <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <mainClass>${spring.boot.mainclass}</mainClass>
        </configuration>
        <executions>
            <execution>
                <id>start-stub</id>
                <configuration>
                    <arguments>
                        <argument>--server.port=8090</argument>
                    </arguments>
                    <mainClass>io.swagger.Stub</mainClass>
                    <classesDirectory>../my-stub/target/classes</classesDirectory>
                </configuration>
                <goals>
                    <goal>start</goal>
                </goals>
                <phase>pre-integration-test</phase>
            </execution>

            <execution>
                <goals>
                    <goal>build-info</goal>
                </goals>
            </execution>
        </executions>
  </plugin>

【讨论】:

    【解决方案2】:

    spring-boot:run只适用于打包springboot应用的项目。完成这项工作的最佳选择:您需要使用exec-maven-plugin:exec,确保将async 设置为true。查看https://www.mojohaus.org/exec-maven-plugin/examples/example-exec-for-java-programs.html 了解您应该如何配置插件。

    【讨论】:

      猜你喜欢
      • 2019-01-17
      • 2020-03-26
      • 2018-01-30
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-28
      • 2021-11-04
      相关资源
      最近更新 更多