0

我有许多 Spring Boot 微服务,并且我开发了一个新项目Spring-Vault作为依赖项。开发这个新项目(比如vault-client-spring)是为了有通用配置来设置 Vault 并在所有微服务中使用它,我已经在我组织的私有 maven 托管存储库中发布了 jar。

我的问题是当我将此 jar 作为依赖项添加到任何微服务中时,应用程序没有开始抛出以下错误。Spring-Cloud-Vault依赖项不会导入到我的消费项目中。我还在文件中添加了以前缀开头的必要spring.cloud.vault属性bootstrap.yml

这是我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'
        }
    }
}

这是build.gradle使用此 jar 的微服务的文件。

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.classreading.SimpleMetadataReader.getClassReader(SimpleMetadataReader.java:55) ~[spring-core-5.3.4.jar:5.3.4]
    at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:49) ~[spring-core-5.3.4.jar:5.3.4]
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:103) ~[spring-core-5.3.4.jar:5.3.4]
    at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.createMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:86) ~[spring-boot-2.4.3.jar:2.4.3]
    at org.springframework.boot.type.classreading.ConcurrentReferenceCachingMetadataReaderFactory.getMetadataReader(ConcurrentReferenceCachingMetadataReaderFactory.java:73) ~[spring-boot-2.4.3.jar:2.4.3]
    at org.springframework.core.type.classreading.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 原生项目中工作

4

1 回答 1

0

我自己找到了。由于我是作为“库”开发的,因此我必须允许 Spring-Vault 依赖项包含在消费应用程序中。根据 Gradle 的java-library插件,我使用了api依赖项。消费应用程序可以访问和引导它们。

于 2021-03-31T09:02:21.023 回答