简体   繁体   English

Maven:自定义外部 jar 作为微服务架构的依赖项

[英]Maven: Custom external jar as a dependency for a microservice architecture

I am trying to create a microservices java project with springboot.我正在尝试使用 springboot 创建一个微服务 java 项目。 My idea is to create a separate maven project which will contain all DTOs and utils classes common to all microservices.我的想法是创建一个单独的 maven 项目,该项目将包含所有微服务共有的所有 DTO 和 utils 类。 (I know many will criticize this choice but that's how I decided to proceed at the moment). (我知道很多人会批评这个选择,但这就是我目前决定继续进行的方式)。

So I created the lib project and the base project.所以我创建了lib项目和基础项目。 Working on the base project, my IDE can correctly read the classes contained in the lib module.在基础项目上工作,我的 IDE 可以正确读取lib模块中包含的类。 During the compilation phase, i make a clean install of the lib project and then a clean install of the base project: at this point I get the following error:在编译阶段,我对lib项目进行了全新安装,然后对基础项目进行了全新安装:此时我收到以下错误:

[ERROR] symbol:   class Test
[ERROR] location: interface slt.mytest.base.services.TestsService
[ERROR] /C:/workspace/mytest/slt-base-be/src/main/java/slt/mytest/base/services/impl/TestsServiceImpl.java:[8,47] package slt.mytest.libs.entities.base does not exist
[ERROR] /C:/workspace/mytest/slt-base-be/src/main/java/slt/mytest/base/services/impl/TestsServiceImpl.java:[9,45] package slt.mytest.libs.models.base does not exist
[ERROR] /C:/workspace/mytest/slt-base-be/src/main/java/slt/mytest/base/repositories/TestsRepository.java:[5,47] package slt.mytest.libs.entities.base does not exist
[ERROR] /C:/workspace/mytest/slt-base-be/src/main/java/slt/mytest/base/repositories/TestsRepository.java:[10,60] cannot find symbol
[ERROR] symbol: class TestEntity

How can I make the lib a base project visible?如何使lib成为可见的基础项目? Is there a way to automatically start compiling libs on every base install?有没有办法在每次基本安装时自动开始编译库?

in general... how would you manage this case?一般来说......你会如何处理这个案子?

Thanks谢谢

lib project pom.xml库项目 pom.xml

<?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.6.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>slt.mytest</groupId>
    <artifactId>libs</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>libs</name>
    <description>Libs Module of mytest</description>
    <properties>
        <java.version>1.8</java.version>
        <org.mapstruct.version>1.2.0.Final</org.mapstruct.version>
        <org.projectlombok.version>1.18.22</org.projectlombok.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
            <version>${org.projectlombok.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

base project pom.xml基础项目 pom.xml

<?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.6.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>slt.mytest</groupId>
    <artifactId>base</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>base</name>
    <description>Base Module of mytest</description>
    <properties>
        <java.version>1.8</java.version>
        <org.mapstruct.version>1.2.0.Final</org.mapstruct.version>
        <org.projectlombok.version>1.18.22</org.projectlombok.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
            <version>${org.projectlombok.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>${org.mapstruct.version}</version>
        </dependency>

        <dependency>
            <groupId>slt.mytest</groupId>
            <artifactId>libs</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

TL;DR: You may need to install your lib project to your local repository ( .m2/repository ). TL;DR:您可能需要将您的lib项目安装到本地存储库 ( .m2/repository )。 You can do this by executing mvn install (or use your IDE's GUI features) in the lib project.您可以通过在lib项目中执行mvn install (或使用 IDE 的 GUI 功能)来完成此操作。 If base still does not compile, execute mvn dependency:resolve on base .如果base仍然无法编译,请在base上执行mvn dependency:resolve

Your IDE may identify the dependency, and that is why there are no errors in your code editor, but Maven will not unless it can resolve the dependency.您的 IDE 可能会识别依赖关系,这就是为什么您的代码编辑器中没有错误的原因,但 Maven 不会,除非它可以解决依赖关系。 It will try to resolve from your local repository and remote repositories (which, in your case, since you have not defined any in your POM, is just Maven Central).它将尝试从您的本地存储库和远程存储库中解析(在您的情况下,由于您没有在 POM 中定义任何内容,因此它只是 Maven Central)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM