简体   繁体   English

如何从spring boot项目创建jar,我们要在另一个spring boot应用中使用的jar?

[英]how to create jar from spring boot project , this jar we want to use in another spring boot app?

We are planning to create one spring boot project , which we create as jar file , and this jar we will use as dependency for another spring boot application , point is the first jar must contains the classes we code and its related jar files , is it possible like that ?can any one help us please . 我们计划创建一个以jar文件形式创建的spring boot项目,并将这个jar作为另一个spring boot应用程序的依赖项,这是第一个jar必须包含我们编写的类及其相关的jar文件,是否那样的可能吗?有人可以帮助我们吗?

Problem is when i create the jar , only my classes which i coded are packing into jar , not the dependencies of that app 问题是当我创建jar时,只有我编码的类才打包到jar中,而不是该应用程序的依赖项

Just use spring-boot-maven-plugin or spring-boot-gradle-plugin depending on your build system. 只需使用spring-boot-maven-pluginspring-boot-gradle-plugin具体取决于您的构建系统。 Purpose of these plugins is exactly your use case. 这些插件的用途正是您的用例。

Spring Guides are great place to start exploring: Maven Guide , Gradle Guide . Spring Guides是开始探索的好地方: Maven GuideGradle Guide

The only caveat is that you want to exclude tomcat internal container from such shared library: 唯一的警告是您想从此类共享库中排除tomcat内部容器:

Maven: Maven:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- ... -->
</dependencies>

Gradle: 摇篮:

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
    ...
}

You can do that by using "maven shade" plugin for creating a Fat jar, also called uber-jar of the parent project, which you want to use in child project. 您可以使用“ maven shade”插件创建一个Fat罐,也就是您要在子项目中使用的父项目的uber-jar来做到这一点。 This Fat jar contains the code you have written along with all the dependencies of the project. 此Fat jar包含您编写的代码以及项目的所有依赖项。 You can check here for more details : https://maven.apache.org/plugins/maven-shade-plugin/index.html . 您可以在此处查看更多详细信息: https : //maven.apache.org/plugins/maven-shade-plugin/index.html The Fat jar created will be placed in your local repository. 创建的Fat jar将放置在您的本地存储库中。

In your child project, you can use parent project as a dependency by specifying following properties: 在子项目中,可以通过指定以下属性来将父项目用作依赖项:

<dependency>
  <groupId>parent_project_group_id</groupId>
  <artifactId>parent_project_artifact_id</artifactId>
  <version>parent_project_version</version>
<dependency>

Make sure that you build parent project with maven shade plugin and not with spring boot default plugin. 确保您使用Maven Shade插件而不是Spring Boot默认插件构建父项目。 Also, use "clean install" as goal while building the parent project. 另外,在构建父项目时,请以“全新安装”为目标。

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

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