商务网站规划与建设心得,庆阳网站建设,品质好坏质量,百度如何提交网站文章目录 背景一般的实现使用spring-boot-dependencies 更优雅的实现. 背景
有时候构建一个多模块maven项目其中某一个模块是web-service需要使用spring boot,其他模块跟spring boot 完全无关,本文总结一下在这个场景下maven项目最佳构建方式.
一般的实现
网上应该也看到过很… 文章目录 背景一般的实现使用spring-boot-dependencies 更优雅的实现. 背景
有时候构建一个多模块maven项目其中某一个模块是web-service需要使用spring boot,其他模块跟spring boot 完全无关,本文总结一下在这个场景下maven项目最佳构建方式.
一般的实现
网上应该也看到过很多人写的文章,思路非常简单,即是将整个项目作为spring-boot-starter-parent 的子项目. 这样整个项目里面所有模块都可以在dependencies里面添加上有parent内指定版本的spring boot 相关的起步依赖包. 而不需要使用spring的模块可以完全不用管这个parent pom.
比如要构建如下含有三个模块的maven project:
module namedescriptioncore存放一些公共的类的模块,可被其他模块使用web-servicespring boot service 对外提供rest apispark-jobspark 项目
project的pom.xml如下:
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.7.18/version/parentgroupIdorg.mytest/groupIdartifactIddemo1/artifactIdversion1.0-SNAPSHOT/versionpackagingpom/packagingmodulesmoduleweb-service/modulemodulespark-job/modulemodulecore/module/modulespropertiesmaven.compiler.source11/maven.compiler.sourcemaven.compiler.target11/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/properties/projectcore 模块的pom.xml如下:
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.mytest/groupIdartifactIddemo1/artifactIdversion1.0-SNAPSHOT/version/parentartifactIdcore/artifactIdpropertiesmaven.compiler.source11/maven.compiler.sourcemaven.compiler.target11/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/properties/projectweb-service的pom如下
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.mytest/groupIdartifactIddemo1/artifactIdversion1.0-SNAPSHOT/version/parentartifactIdweb-service/artifactIdpropertiesmaven.compiler.source11/maven.compiler.sourcemaven.compiler.target11/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build
/projectspark-job的pom如下(大致的pom不讨论细节)
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.mytest/groupIdartifactIddemo1/artifactIdversion1.0-SNAPSHOT/version/parentartifactIdspark-job/artifactIdpropertiesmaven.compiler.source11/maven.compiler.sourcemaven.compiler.target11/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/properties/project这样构建的项目由于使用spring-boot-starter-parent 作为parent pom所以整个项目其实是下图的结构: 这个结构看起来不是那么舒服,比如这里买的spark-job模块,跟spring完全没关, 不应该让spring-boot-started-parent作为其parent.那么有没有什么方法能够不使用spring-boot-starter-parent作为整个项目的parent也能完全解决spring boot依赖的问题呢?答案是有的.
使用spring-boot-dependencies 更优雅的实现.
spring 本身提供了这样一个pom 来管理所有依赖: https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies 且官方也给了怎么使用这个pom的方法
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion2.7.18/versiontypepom/typescopeimport/scope
/dependency有了这个pom引入到项目里面,就可以不用将spring-boot-starter-parent作为整个项目的parent. 改造一下项目的pom
project pom.xml 如下
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdorg.mytest/groupIdartifactIddemo2/artifactIdversion${revision}/versionpackagingpom/packagingmodulesmodulecore/modulemoduleweb-service/modulemodulespark-job/module/modulespropertiesmaven.compiler.source11/maven.compiler.sourcemaven.compiler.target11/maven.compiler.target!--revision统一版本号--revision1.0/revisionspring.boot.version2.7.18/spring.boot.versionproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependencyManagementdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion${spring.boot.version}/versiontypepom/typescopeimport/scope/dependency!--core模块依赖声明--dependencygroupIdorg.mytest/groupIdartifactIdcore/artifactIdversion${project.version}/version/dependency/dependencies/dependencyManagement/projectcore 模块 pom.xml 如下
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.mytest/groupIdartifactIddemo2/artifactIdversion${revision}/version/parentartifactIdcore/artifactIdpropertiesmaven.compiler.source11/maven.compiler.sourcemaven.compiler.target11/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/properties/projectweb-service的pom.xml如下:
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.mytest/groupIdartifactIddemo2/artifactIdversion${revision}/version/parentartifactIdweb-service/artifactIdpropertiesmaven.compiler.source11/maven.compiler.sourcemaven.compiler.target11/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependencies!--web-service中引入core模块--dependencygroupIdorg.mytest/groupIdartifactIdcore/artifactIdversion${project.version}/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency/dependencies/projectspark-job的pom.xml(大致的pom不讨论细节)如下
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdorg.mytest/groupIdartifactIddemo2/artifactIdversion${revision}/version/parentartifactIdspark-job/artifactIdpropertiesmaven.compiler.source11/maven.compiler.sourcemaven.compiler.target11/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/properties/project改进之后项目就不存在以spring-boot-starter-parent作为整个项目的parent实现spring boot依赖包的管理. 项目结构如下: