bounty将在5天后过期。回答此问题可获得+200声望奖励。Sandeep希望引起更多人对此问题的关注:我希望在这里的样本代码。因为已经检查了过去的答案。并正在寻找一些解决这个问题。
如此快速的澄清,因为我已经读了一些以前类似的问题:
1.我正在寻找编译和运行一个Spring Boot 代码库从一个运行的Spring应用程序。
1.我并不想把多个spring Boot jar嵌套或打包在一个jar中,第二个spring boot代码库在外面,甚至可能在github上。
我已经看过https://www.toptal.com/spring-boot/spring-boot-application-programmatic-launch了,它非常有用,但是我不知道如何编译和加载一个Spring Boot 应用程序。
我有一个暗示,这是在Tomcat Tomcat ServletWebServerFactory级别上完成的-基本上,Spring Boot “助手”应用程序将触发Tomcat加载外部jar并进行部署。
3条答案
按热度按时间jvidinwx1#
您不能简单地在从您的主Spring Boot应用程序中启动的 * 外部进程 * 中构建和运行您的辅助Sping Boot 应用程序吗?
我刚刚在一个非常简单的概念验证中尝试了这个方法,为了这个概念验证,我创建了两个虚拟的Sping Boot 应用程序,一个叫做
outer
,另一个叫做inner
,后者应该由前者构建和运行。以下是目录结构(为简洁起见,省略了两个Gradle项目中的Gradle 7.6 Wrapper文件):
两个
settings.gradle
文件都是空的,两个build.gradle
文件的内容也是一样的:“内部”应用程序是Spring Quickstart Guide的演示应用程序,即
inner/src/main/java/com/example/demo/DemoApplication.java
如下所示:inner/src/main/resources/application.properties
文件还包含server.port=8081
,以便其Web服务器运行在与“外部”端口不同的端口上。这就剩下
outer/src/main/java/com/example/demo/DemoApplication.java
了,它定义了以下(粗略的)应用程序:现在,您可以在
outer/
中运行./gradlew bootRun
,以启动“外部”Sping Boot 应用程序(Tomcat Web服务器)。该服务器会对POST请求做出React,启动“内部”Spring Boot应用程序的Gradle构建,该应用程序也会运行该应用程序(一旦构建完成)。up9lanfz2#
嗯-我知道你想要的样本代码,但我缺乏时间,目前尝试它由我自己,这可能不是一个真实的的答案-但太大的评论:
我从来没有做过这样的事情,但也许我可以把我的想法在这里-如果它很糟糕,随时忽略它。
我不知道您的“Main Sping Boot Project”是否使用了Maven Wrapper,但让我们假设它使用了。
所以让我们尝试以下概念:
git clone
)到它选择的一些目录中。(你可以使用JGit或使用**Runtime.getRuntime().exec(“你的git命令”)**或你想到的任何东西来完成)gradlew bootJar
)java -jar path/to/your/mySpringBoot.jar fully.qualified.package.Application
的命令来启动 Boot这听起来是不是有点像你想做的?最后,如果我们仔细想想-这是一样的,当你手动 checkout 你的项目,构建JAR并启动它-不是吗?
pxyaymoc3#
您可以使用
JavaCompiler
。另请参阅文档-〉Java Compiler