如何允许 Spring Boot 应用程序使用具有 Spring Cloud 依赖关系的自定义 jar
Posted
技术标签:
【中文标题】如何允许 Spring Boot 应用程序使用具有 Spring Cloud 依赖关系的自定义 jar【英文标题】:How to allow spring boot applications to use custom jar having spring cloud dependency 【发布时间】:2021-06-25 20:09:30 【问题描述】:我有许多 Spring Boot 微服务,并且我开发了一个新项目,该项目具有 Spring-Vault
作为依赖项。开发这个新项目(比如vault-client-spring
)是为了有通用配置来设置 Vault 并在所有微服务中使用它,我已经在我组织的私有 maven 托管存储库中发布了 jar。
我的问题是当我将此 jar 作为依赖项添加到任何微服务中时,应用程序没有开始抛出以下错误。 Spring-Cloud-Vault
依赖项不会导入到我的消费项目中。我还在bootstrap.yml
文件中添加了以前缀spring.cloud.vault
开头的必要属性。
这是我的build.gradle
vault-client-spring
文件。
group = 'com.mygroup'
version = '0.0.1'
buildscript
ext
springBootVersion = '2.4.4'
springDepsMgmtVersion = '1.0.11.RELEASE'
repositories
mavenCentral()
dependencies
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
classpath "io.spring.gradle:dependency-management-plugin:$springDepsMgmtVersion"
apply plugin: 'java-library'
apply plugin: 'idea'
apply plugin: 'maven-publish'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = '1.8'
repositories
mavenCentral()
configurations
compileOnly
extendsFrom annotationProcessor
bootJar
enabled = false
jar
archivesBaseName = 'vault-client-spring'
enabled = true
ext
springCloudVersion = '2020.0.2'
dependencyManagement
imports
mavenBom "org.springframework.cloud:spring-cloud-dependencies:$springCloudVersion"
dependencies
implementation 'org.springframework.cloud:spring-cloud-starter-vault-config'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
test
useJUnitPlatform()
task sourcesJar(type: Jar, dependsOn: classes)
classifier('sources')
from sourceSets.main.allSource
publishing
publications
mavenJava(MavenPublication)
artifact jar
artifact sourcesJar
repositories
maven
credentials
username 'PRIVATE-USER'
password 'PRIVATE-PASSWORD'
url 'PRIVATE MAVEN URL'
这是使用此 jar 的微服务的 build.gradle
文件。
plugins
id 'org.springframework.boot' version '2.4.3'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
group = 'com.mygroup'
version = ''
sourceCompatibility = '1.8'
configurations
compileOnly
extendsFrom annotationProcessor
repositories
mavenCentral()
maven url 'PRIVATE MAVEN URL'
bootJar
setArchivesBaseName("microservice-one")
version("")
dependencies
implementation 'com.mygroup:vault-client-spring:0.0.1' // here's the dependency
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compileJava
compileJava.inputs.files(processResources)
test
useJUnitPlatform()
生成的 POM 文件。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mygroup</groupId>
<artifactId>vault-client-spring</artifactId>
<version>0.0.1</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2020.0.2</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.4</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
</project>
我遇到的错误如下..
Caused by: java.io.FileNotFoundException: class path resource [org/springframework/vault/config/AbstractVaultConfiguration.class] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180) ~[spring-core-5.3.4.jar:5.3.4]
at org.springframework.core.type.clas-s-reading.SimpleMetadataReader.getClassReader(SimpleMetadataReader.java:55) ~[spring-core-5.3.4.jar:5.3.4]
at org.springframework.core.type.clas-s-reading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:49) ~[spring-core-5.3.4.jar:5.3.4]
at org.springframework.core.type.clas-s-reading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:103) ~[spring-core-5.3.4.jar:5.3.4]
at org.springframework.boot.type.clas-s-reading.ConcurrentReferenceCachingMetadataReaderFactory.createMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:86) ~[spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.type.clas-s-reading.ConcurrentReferenceCachingMetadataReaderFactory.getMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:73) ~[spring-boot-2.4.3.jar:2.4.3]
at org.springframework.core.type.clas-s-reading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:81) ~[spring-core-5.3.4.jar:5.3.4]
at org.springframework.context.annotation.ConfigurationClassParser.asSourceClass(ConfigurationClassParser.java:696) ~[spring-context-5.3.4.jar:5.3.4]
at org.springframework.context.annotation.ConfigurationClassParser$SourceClass.getSuperClass(ConfigurationClassParser.java:1010) ~[spring-context-5.3.4.jar:5.3.4]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:341) ~[spring-context-5.3.4.jar:5.3.4]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250) ~[spring-context-5.3.4.jar:5.3.4]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:199) ~[spring-context-5.3.4.jar:5.3.4]
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:304) ~[spring-context-5.3.4.jar:5.3.4]
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250) ~[spring-context-5.3.4.jar:5.3.4]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:207) ~[spring-context-5.3.4.jar:5.3.4]
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:175) ~[spring-context-5.3.4.jar:5.3.4]
... 14 common frames omitted
如何解决这个错误?我是第一次在 Spring Cloud 原生项目中工作
【问题讨论】:
你能提供你的构建文件吗?我不认为引导程序中的属性与它有任何关系。损坏的 jar 下载? 添加了 build.gradle 文件 我根本不是 gradle 专家。客户端将什么列为依赖项***.com/questions/21645071/… 嘿@spencergibb 我今天自己找到了。无论如何,谢谢考虑。我将其发布为答案 【参考方案1】:我自己找到的。由于我是作为“库”开发的,因此我必须允许 Spring-Vault 依赖项包含在消费应用程序中。根据 Gradle 的 java-library 插件,我使用了 api
依赖项。消费应用程序可以访问和引导它们。
【讨论】:
以上是关于如何允许 Spring Boot 应用程序使用具有 Spring Cloud 依赖关系的自定义 jar的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Spring Boot 将 CSV 文件导入 MYSQL
具有多个用户的 Spring Boot REST 服务[关闭]