包装弹簧子项目分成一个罐子 [英] Packaging spring sub-projects into one jar
问题描述
我正在使用基于Spring的网络应用程序,我正在尝试将其捆绑到一个jar中。
I'm having Spring based web-app that I'm trying to bundle into a single jar.
我对该项目进行布局的想法是包含多个子项目的根项目,其中有一个子项目是一个spring-boot应用程序,而其他子项目可能是我自己编写的代码处理某些业务逻辑。
My thoughts on laying out the project was to have a root project that contains multiple sub-projects, where there would be one sub-project that is a spring-boot application, and others sub-projects maybe my own-written code handle certain business logic let's say.
所以结构可能如下所示:
So the structure may look something like:
/root
build.gradle <- the problem is here, how I should write root project build script
/spring-backend-subproject
/other-business-subproject
拥有所有这些子项目后,我想将它们全部打包成一个JAR,这样我才能做到
Having all these sub-projects, I want to package them all into one single JAR, so that I can just do
java -jar build/libs/RootJar.jar
否则我需要运行:
./gradlew bootRun
将运行spring-boot子项目中的特定主类
which will run the particular main class in the "spring-boot" sub-project
我一直在网上搜索如何将所有子项目罐子捆绑到一个罐子里,例如:可以Gradle将多个项目放到一个jar中吗?
但是我经常最终会遇到像main-class这样的错误(如果我在spring子项目文件夹中专门运行jar)或者找不到SpringApplication类(如果在子项目文件夹中运行jar,则不会发生)
I've been searching online for how to bundle all subproject jars into one jar, such as this one: Can Gradle jar multiple projects into one jar? but I often just end up having errors such as main-class not found(which does not happen if I run the jar specifically in the "spring sub-project" folder) or SpringApplication class not found (again does not happen if run jar in the sub-project folder)
我如何解决这个问题?
谢谢!
推荐答案
我广泛使用这种模式。而不是尝试将部署类放入根目录,添加一个模块(我通常称之为 -launcher
),将其他模块列为依赖项并包含您的主类和相关应用程序代码,例如您可能拥有的任何 application.properties
。
I use this pattern extensively. Instead of trying to put the deployment classes into the root, add a module (I usually call it -launcher
) that lists the other modules as dependencies and contains your main class and related application code, such as any application.properties
you may have.
此模块将生成工件 my-project-launcher.jar
,然后将其部署到您正在使用的任何平台上。
This module will produce an artifact my-project-launcher.jar
, and you deploy this to whatever platform you're using.
这篇关于包装弹簧子项目分成一个罐子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!