使用公共库的微服务出错,找不到共享库的pom和jar

nkkqxpd9  于 2021-06-26  发布在  Java
关注(0)|答案(2)|浏览(369)

我正在构建这个由几个微服务组成的应用程序,使用公共类和可重用类,所有这些类都共享在一个名为commons的包中,同时在每个微服务上导入,以使用任何cladsses公共共享的东西。出于某种原因,需要一个微服务(微管道)来生成一个新的jar,但是在这个过程中,我得到了一个警告:首先在pom中找不到公共库,其次,抛出了一个错误,所有这些都是我在docker映像上部署每个微的。
我的工作sapce结构包含所有组件,包括公共和所有其他微服务,如下所示:

假设我的公共库设计如下:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.commons</groupId>
    <artifactId>commons</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>commons</name>
    <description>Demo project for commons</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--PARA EL MANEJO DE JPA-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!--        SPRING REST-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>

        <!--TEST-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
    </dependencies>

    <!--INACTIVANDOSE EL PLUGIN AL SER ESTO SOLO UNA LIBRERIA -->
    <build>

    </build>

</project>

由于可共享资源的ist功能而禁用其主类:

package com.commons;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class CommonsApplication {
}

然后在我的一个微服务(microproducts)上,我首先在pom上导入公共库,然后在它的主类中扫描所有组件、实体、基类所需的包,这些包来自公共以便工作
微导管聚甲醛

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.microproducts</groupId>
    <artifactId>microproducts</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>microproducts</name>
    <description>Demo project for microproducts</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
    </properties>

    <dependencies>
        <!--LIBRERIA COMUN DE COMMONS CREADA-->
        <dependency>
            <groupId>com.commons</groupId>
            <artifactId>commons</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!--        SWAGGER 2-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>

        <!--        SWAGGER UI-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

        <!--        JPA-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!--        SPRING REST-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>

        <!--        SPRING WEB-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--        EUREKA CLIENT-->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
                </dependency>

       <!--        SLEUTH TRACE CONEXION-->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-sleuth</artifactId>
                </dependency>

        <!--        ZIPKIN-->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-zipkin</artifactId>
                </dependency>

        <!-- DEPENDENCIA CONNECTORA DE RABBIT MQ  PARA EL MANEJO DE RESPUESTAS Y LOGS SOBRE
        TRAZAS EN ZIPKING SERVER-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
            <version>2.2.11.RELEASE</version>
        </dependency>

        <!--DEVTOOLS-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!--        CONNEXION A MYSQL-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!--        TEST-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

        </plugins>
    </build>

</project>

如您所见,公共库作为第一个导入
然后主类的设计是这样的:

package com.microproducts;

import com.microproducts.repositories.ImgSubTypeRepository;
import com.microproducts.repositories.ProductRepository;
import com.microproducts.repositories.RenterRepository;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@EnableEurekaClient
@SpringBootApplication
@ComponentScan({"com.commons.dtos","com.commons.exceptions","com.commons.responses","com.commons.repositories","com.commons.responses"})
@EntityScan("com.commons.entities")
@ComponentScan(basePackageClasses = {ProductRepository.class, RenterRepository.class, ImgSubTypeRepository.class})
public class MicroproductsApplication  {
    public static void main(String[] args) {
        SpringApplication.run(MicroproductsApplication.class, args);
    }
}

你知道为什么这个行为不让我构建一个jar,因为没有“找到”我以前导入的公共jar吗?

nom7f22z

nom7f22z1#

正如khbmarbaise所说:
如果你使用 spring-boot-starter-parent 作为父级,模块/项目不能用作依赖项。

to94eoyn

to94eoyn2#

那么,删除.m2存储库,重建所有项目(包括公共项目)就可以了

相关问题