如何允许 Spring Boot 应用程序使用具有 Spring Cloud 依赖关系的自定义 jar

     2023-02-27     233

关键词:

【中文标题】如何允许 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.gradlevault-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-boot更新我的对象?【英文标题】:HowcanIpreventinsertandallowonlyupdateofmyobjectusingspring-boot?【发布时间】:2019-08-0607:35:46【问题描述】:我知道save()将插入我的对象,如果它不存在于数据库中,如果... 查看详情

Spring Boot - 不允许使用 POST 方法

...:我正在解决这个问题...我有一个带有S2S通信的SpringBoot应用程序。我有一个@RestController方法,它应该接受POST请求。这是控制器@RestControllerpublicclassPaymentRestController@P 查看详情

如何使用 Spring Security 实现具有两级令牌认证的 Spring Boot 微服务?

】如何使用SpringSecurity实现具有两级令牌认证的SpringBoot微服务?【英文标题】:HowtoimplementSpringBootMicroservicewithtwolevelsoftokenauthenticationusingSpringSecurity?【发布时间】:2021-02-0705:28:40【问题描述】:团队您好,我正在开发一个SpringBoo... 查看详情

如何使用 Spring Boot 将 CSV 文件导入 MYSQL

】如何使用SpringBoot将CSV文件导入MYSQL【英文标题】:HowtoimportaCSVfiletoMSSQLusingSpringboot【发布时间】:2021-10-2815:30:44【问题描述】:我正在开发一个项目,该项目具有允许用户将文件(Excel或CSV)导入DB(MsSQL)的功能。我已经阅读了... 查看详情

允许用户使用 jwt 在 Spring Boot 中选择资源

...816:04:28【问题描述】:如标题所示,我正在尝试只让我的应用程序上的“ROLE_ADMIN”有权创建锦标赛。我的用户:@Id@GeneratedValue(strategy=GenerationType.AUTO)publiclon 查看详情

如何在具有 JDBC 安全性的 Spring Boot 中使用 Flyway?

】如何在具有JDBC安全性的SpringBoot中使用Flyway?【英文标题】:HowtouseFlywayinSpringBootwithJDBCSecurity?【发布时间】:2015-04-1623:40:29【问题描述】:我想在我的SpringBoot项目中使用Flyway作为首选的数据库迁移处理方式(使用当前的V1.2.1.R... 查看详情

如何:具有大型数据库和快速访问一小部分数据的 Spring Boot 应用程序

...何:具有大型数据库和快速访问一小部分数据的SpringBoot应用程序【英文标题】:Howto:SpringBootapplicationwithalargedatabaseandfastaccesstoasmallsubsetofdata【发布时间】:2020-06-1118:46:42【问题描述】:这是一个关于如何组织数据存储的一般建... 查看详情

如何在 Spring Boot 中更改允许的标头

】如何在SpringBoot中更改允许的标头【英文标题】:HowtoalterallowedheadersinSpringBoot【发布时间】:2017-03-0507:33:59【问题描述】:我目前正在使用Auth0(和Angular2GUI),它将请求中的"x-xsrf-token"类型的标头发送到SpringBootAPI。我得... 查看详情

具有多个用户的 Spring Boot REST 服务[关闭]

...StackExchange站点,请随时推荐或迁移。我正在编写一个REST应用程序,它将能够注册新用户并允许现有用户相互交互(例如,您可以想象一 查看详情

Spring Boot 安全配置忽略允许的端点

...5【问题描述】:我正在尝试通过JWT获得SpringSecurity以使用应用程序。我已经阅读了许多教程和示例,但没有什么真正适合我的用例。我们不通过用户名/密码进行授权,我们使用twilio来验证手机号码,然后我想创建 查看详情

如何构建 Spring Boot 微服务

...:2021-04-0903:37:10【问题描述】:我有一个关于我要构建的应用程序的架构问题。该应用程序将以Angular作为前端,并使用jhipster生成后端SpringBoot。我将拥有一个名为制造商的微服务,其中包含以下实体:卖方材料产品。材料与卖... 查看详情

如何使用 Spring Boot 在 DynamoDB 中添加具有自动增量键的新项目

】如何使用SpringBoot在DynamoDB中添加具有自动增量键的新项目【英文标题】:HowcanIaddnewitemwithauto-incrementkeyinDynamoDBwithSpringBoot【发布时间】:2021-12-3010:33:16【问题描述】:我想将DynamoDB中的主键设为自动增量键,例如SQL。并且生成... 查看详情

具有不同环境的关键云中的 Spring Boot 应用程序

】具有不同环境的关键云中的SpringBoot应用程序【英文标题】:SpringbootapplicationinpivotalCloudwithdifferentenvironments【发布时间】:2018-03-1521:09:32【问题描述】:我正在使用具有不同测试环境(测试、QA、UAT、PROD)Spring引导应用程序的Pi... 查看详情

在 Spring Boot 中使用 JPA 从具有 OneToMany 关系的实体中删除对象

...中我有一个包含许多歌曲的播放列表,为此我使用JPA以及允许我在两者之间建立关系的好处 查看详情

在两个不同端口上具有两个服务的 Spring Boot 应用程序

】在两个不同端口上具有两个服务的SpringBoot应用程序【英文标题】:Springbootapplicationwithtwoservicesontwodifferentports【发布时间】:2019-07-2904:32:32【问题描述】:我有一个带有两个服务的SpringBoot应用程序,但我需要在端口8080上运行... 查看详情

具有不同键的 Spring Boot 应用程序属性[重复]

】具有不同键的SpringBoot应用程序属性[重复]【英文标题】:Springbootapplicationpropertieswithdifferentkeys[duplicate]【发布时间】:2019-02-0115:49:56【问题描述】:我如何定义一个对象来按键读取我的application.mysql-databases:swaw:stage:devip:x.x.x.xp... 查看详情

您将如何使用 Spring Boot 处理仅具有可选查询参数的 REST API?

】您将如何使用SpringBoot处理仅具有可选查询参数的RESTAPI?【英文标题】:HowwouldyouhandleaRESTAPIwithonlyoptionalqueryparamswithSpringBoot?【发布时间】:2020-06-0423:58:40【问题描述】:我想构建一个返回Order对象的简单端点,我可以在其中通... 查看详情

具有 OAuth 2 和 JWT 安全性的 Spring Boot 微服务

...描述】:我正在开发一个使用微服务进行支付的SpringBoot应用程序,它将被移动应用程序和Web应用程序使用。1)用户需要通过身份验证才能访问移动应用程序2)使用我的服务的第三方移动应用需要经过身份验证(使用我 查看详情