简体   繁体   English

无法使用 docker 构建具有自定义依赖项的 Spring Boot 项目

[英]Unable to build the spring boot project with custom dependency using docker

I am working on the spring boot project and I have multiple microservices.我正在从事 Spring Boot 项目,并且我有多个微服务。 In order to keep the redundant code in all modules, I separated them and made a single project, and added this as a dependency in every module.为了保留所有模块中的冗余代码,我将它们分开并制作了一个项目,并将其作为依赖项添加到每个模块中。

My scenario is, let's say我的情况是,让我们说

I have a commons spring boot project and A and B projects.我有一个 commons spring boot 项目和 A 和 B 项目。 Now I am able to add commons dependency in project A and project B as现在我可以在项目 A 和项目 B 中添加公共依赖项

<dependency>
 <groupId>com.example </groupId>
 <artifactId>commons</artifactId>
 <version>1.0.0</version>
</dependency>

and the commons pom.xml has the below并且 commons pom.xml 具有以下内容

     <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

If I run the below command on A and B projects, I can see build success mvn clean install如果我在 A 和 B 项目上运行以下命令,我可以看到构建成功mvn clean install

Now the problem arises for me If I try to create the docker images for projects A and B. In this case, the commons module is not taking it as a dependency in both.现在问题出现在我身上,如果我尝试为项目 A 和 B 创建 docker 映像。在这种情况下,commons 模块不会将其作为两者的依赖项。

I have used the below sequence of docker command to create an image我使用以下 docker 命令序列来创建图像

#
# Build stage
#
FROM maven:3.6.0-jdk-8-slim AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package -Dmaven.test.skip=true

#
# Package stage
#
FROM openjdk:8-jre-slim
COPY --from=build /home/app/target/A.jar /usr/local/lib/A-0.0.1-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/local/lib/A-0.0.1-SNAPSHOT.jar"]

Could anyone help me to add the commons dependency to project A?任何人都可以帮我将公共依赖项添加到项目 A 中吗?

Note: we are using AWS managed container image registry service to store the images.注意:我们使用 AWS 托管的容器镜像注册服务来存储镜像。

Thanks in advance :)提前致谢 :)

As i understand it, you haven't pushed your dependency to a remote maven artifact repository, hence it's only available in your local machine under ~/.m2/repository/com/example/commons/1.0.0/ .据我了解,您尚未将依赖项推送到远程 maven 工件存储库,因此它仅在您的本地计算机中可用~/.m2/repository/com/example/commons/1.0.0/

When you try to compile, maven will check your local machine first to see if the dependency is available, else it will reach out to maven central (or any other configured repo) to resolve and retrieve the artifact.当您尝试编译时,maven 将首先检查您的本地计算机以查看依赖项是否可用,否则它将联系 maven Central(或任何其他配置的 repo)来解析和检索工件。 Since you do this inside a docker container where the artifact isn't available, it will fail.由于您在工件不可用的 docker 容器中执行此操作,因此它将失败。

So my suggestion is to try:所以我的建议是尝试:

Option 1选项1

Add a row in your Dockerfile to copy this artifact into your docker container before running mvn clean package:在运行 mvn clean package 之前,在 Dockerfile 中添加一行以将此工件复制到 docker 容器中:

COPY ~/.m2/repository/com/example/commons/1.0.0/ ~/.m2/repository/com/example/commons/1.0.0/

Option 2选项 2

Setup an artifact repository and store your artifacts there设置工件存储库并将您的工件存储在那里

Finally, figure out the solution with the below set of commands最后,使用以下命令集找出解决方案

#
# Build stage
#
FROM maven:3.6.0-jdk-8-slim AS build

COPY /commons/src /home/commons/src
COPY /commons/pom.xml /home/commons
RUN mvn -f /home/commons/pom.xml clean install -Dmaven.test.skip=true

COPY A/src /home/A/src
COPY A/pom.xml /home/A/
RUN mvn -f /home/A/pom.xml clean package install:install-file -Dfile=/home/commons/target/commons-0.0.1-SNAPSHOT.jar -DgroupId=com.example -DartifactId=commons -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar -DgeneratePom=true -Dmaven.test.skip=true
#
# Package stage
#
FROM openjdk:8-jre-slim
COPY --from=build /home/A/target/A.jar /usr/local/lib/A.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/local/lib/A.jar","--spring.profiles.active=test"]

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

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