本地启动/调试 Spring Boot 应用程序,并包含 SPI

huangapple go评论71阅读模式
英文:

Local start/debug Spring Boot Application from dependency and include SPI

问题

我们有一个大型项目,部署后运行,但我不知道如何在本地启动它(使用Eclipse、IntelliJ)。
在我们的核心项目中,有一个启动配置,其中包含一个主要方法,并标记为@SpringBootApplication。核心项目是我们的电子邮件项目和一些其他项目的依赖项。此外,我们还有一个依赖于目标系统的SPI。

我想一次调试这3个项目 - 核心项目、电子邮件项目和活动SPI。我有两个主要问题:

1)如何启动Spring应用程序?IDE无法找到依赖项中的主要方法。它不是一个Web服务器,而是使用Spring注解启动的应用程序。
2)如何添加具有源代码的SPI项目,以便我可以调试它?

此外,我必须提到我们使用Gradle进行单项目构建。我们将升级到Java 17,也许如果我们在Gradle中创建一个多项目,问题会更容易解决?针对这种情况,构建脚本应该是什么样子的?

英文:

We have a large project that runs after deploying, but I do not know how to start it local (Eclipse, Intellij).
We have in our core project a start configuration with a main method and it is marked as @SpringBootApplication. The core project is a dependency of our email project and some other projects. Also we have a SPI that depends on target system.

I want to debug all 3 projects at once - core, email and the active SPI. I have two major problems:

  1. How to start the spring application? The IDE can not find the main method from the dependency. It is not a web server but an application that uses spring annotations to start.
  2. How to add the SPI project with source code, so I can debug it?

Also I have to mention that we use Gradle with single project builds. We will update to Java 17 and maybe it is easier to solve if we create a multiproject in Gradle? What should a build script for this use case look like?

答案1

得分: 1

可以使用Gradle Composite Builds来调试多个项目。在settings.gradle中将核心项目和SPI项目添加到邮件项目中:

rootProject.name = 'projEmail'

includeBuild '../projCore'
includeBuild '../projLinux' //SPI

SPI项目还必须作为测试依赖项添加。然后,可以在测试源代码中编写一个启动类,类似于核心项目中的启动类:

public static void main(String[] args) 
{
    LOG.info("Start email project with Spring Boot");
    
    // 调用核心项目中启动Spring的类(带有@SpringBootApplication注解的类)
    SpringApplication app = new SpringApplication(MainAppSpringConfiguration.class);
    app.run(args);
}

所以没有什么特别需要做的。我不得不停止搜索类似问题的答案,而是按照Spring Boot文档的指导进行操作:“您可以从IDE中将Spring Boot应用程序作为Java应用程序运行”。

英文:

It is possible to debug multiple projects by using Gradle Composite Builds. Add the core project and the SPI project to the email project in settings.gradle:

rootProject.name = 'projEmail'

includeBuild '../projCore'
includeBuild '../projLinux'  //SPI

The SPI must be added as test dependency, too. Then it is possible to write a starter class in the test sources - similar to the one in the core project:

public static void main(String[] args) 
{
	LOG.info("Start email project with Spring Boot");
	
    //Call the class in core that starts spring (class with @SpringBootApplication)
	SpringApplication app = new SpringApplication(MainAppSpringConfiguration.class);
	app.run(args);
}

So there is nothing special to do. I had to stop searching for an answer at similar questions and follow the Spring Boot documentation: "You can run a Spring Boot application from your IDE as a Java application.".

huangapple
  • 本文由 发表于 2023年6月19日 22:46:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76507736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定