skip to Main Content

I am working on the spring boot project and I have multiple microservices. 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. Now I am able to add commons dependency in project A and project B as

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

and the commons pom.xml has the below

     <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

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.

I have used the below sequence of docker command to create an image

#
# 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?

Note: we are using AWS managed container image registry service to store the images.

Thanks in advance 🙂

2

Answers


  1. Chosen as BEST ANSWER

    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"]
    

  2. 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/.

    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. Since you do this inside a docker container where the artifact isn’t available, it will fail.

    So my suggestion is to try:

    Option 1

    Add a row in your Dockerfile to copy this artifact into your docker container before running mvn clean package:

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

    Option 2

    Setup an artifact repository and store your artifacts there

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search