Spring Boot的Maven插件无法找到主类,无法执行Java。

4

我有一个多模块的Maven Spring Boot项目。其中一个子模块是一个桩(一个Spring Boot应用程序),我希望在我的真实应用程序的集成测试期间启动并停止它。

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

以下是模块myRealApp的pom文件示例,其中包含所有集成测试。在before integration-tests阶段,其尝试启动stub模块。但是,在子模块目录中运行maven goal时,我遇到了错误:

 mvn integration-tests -X

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

org.apache.maven.lifecycle.LifecycleExecutionException: 在项目上执行目标 org.springframework.boot:spring-boot-maven-plugin:1.5.8.RELEASE:run (start-boot) 失败,无法执行 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或给其他模块引用?该存根充当了我的应用程序所依赖的远程rest服务,在开发过程中使用,因为实际的远程服务测试环境不可靠,一半时间都处于停机状态。因此决定在集成测试中也使用它。

@khmarbaise,我更新了问题。 - Spring
理想情况下,有一种方法可以将module-of-my-stub依赖项添加到spring-boot-maven-plugin:run目标使用的类路径中。尽管似乎没有直接的方法,但是有一个useTestClasspath选项可用。此链接还讨论了另一种替代方法。 - df778899
@df778899 你的意思是我可以把插件配置放在存根的Pom中,然后通过使用useTestClasspath,在那里引用myRealApp测试类? - Spring
1
反过来说,但这只是猜测。思路是通过将module-of-my-stub作为myRealApp的测试依赖,并在现有的spring-boot-maven-plugin:run配置中添加useTestClasspath,这样就可以在类路径上看到MyStub。否则,上面链接中的classesDirectory技巧应该能够将其指向module-of-my-stub下的target/classes目录。 - df778899
@df778899 第二个选项似乎是启动存根,但它没有遵守存根的属性文件,例如无法设置端口或Spring安全密码。我能修复吗? - Spring
显示剩余6条评论
2个回答

1
为了整合一些评论并回答这个问题,基本上遵循了另一个答案中的建议。
关键是将spring-boot-maven-plugin:run目标的classesDirectory设置为指向其他项目下的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>

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接