简体   繁体   English

将春季子项目包装到一个罐中

[英]Packaging spring sub-projects into one jar

I'm having Spring based web-app that I'm trying to bundle into a single jar. 我有一个基于Spring的Web应用程序,试图将其捆绑到一个jar中。

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. 我对项目进行布局的想法是要有一个包含多个子项目的根项目,其中会有一个子项目是spring-boot应用程序,而其他子项目也许是我自己编写的代码来处理某些业务逻辑可以说。

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

Having all these sub-projects, I want to package them all into one single JAR, so that I can just do 拥有所有这些子项目后,我想将它们全部打包到一个JAR中,这样我就可以

java -jar build/libs/RootJar.jar

otherwise I need to run: 否则我需要运行:

./gradlew bootRun

which will run the particular main class in the "spring-boot" sub-project 它将在“ spring-boot”子项目中运行特定的主类

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? 我一直在网上搜索如何将所有子项目jar捆绑到一个jar中,例如这样的: Gradle可以将多个项目打包到一个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) 但是我经常会遇到一些错误,例如找不到主类(如果我专门在“ spring sub-project”文件夹中运行jar,则不会发生)或找不到SpringApplication类(如果在jar中运行jar,同样不会发生)子项目文件夹)

How may I solve this problem? 我该如何解决这个问题?

Thanks! 谢谢!

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. 与其尝试将部署类放到根目录中, -launcher添加一个模块(我通常称其为-launcher ),该模块将其他模块列为依赖项,并包含您的主类和相关的应用程序代码,例如您可能拥有的任何application.properties

This module will produce an artifact my-project-launcher.jar , and you deploy this to whatever platform you're using. 这个模块将产生一个工件my-project-launcher.jar ,并将其部署到您正在使用的任何平台上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM