0

I am trying to make a spring boot multistage build, I have a spring boot project X that contains two util projects as dependencies in other folder, when I try to make the build it fails because this dependencies does not exist.

I tried using multi module maven project but this utils will be used in other projects.

[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 08:23 min
[INFO] Finished at: 2019-09-27T01:29:09Z
[INFO] Final Memory: 44M/228M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project projectX: Could not resolve dependencies for project com.demo:projectX:jar:0.1: The following artifacts could not be resolved: com.util:util1:jar:0.1, com.util:util2:jar:0.1: Could not find artifact com.util:util1:jar:0.1 in spring-milestones (https://repo.spring.io/milestone) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
ERROR: Service 'projectX' failed to build: The command '/bin/sh -c ./mvnw dependency:go-offline -B' returned a non-zero code: 1

(edit) Here is dockerfile for multistage maven build

(edit) the docker build command and context path is from parent folder

FROM openjdk:8-jdk-alpine as build-util1
WORKDIR /util1
COPY ./components/util1/mvnw .
COPY ./components/util1/.mvn .mvn
COPY ./components/util1/pom.xml .
COPY ./components/util1/src src
RUN ./mvnw clean install

FROM openjdk:8-jdk-alpine as build-util2
WORKDIR /util
COPY --from=build-util1 /root/.m2 /root/.m2
COPY ./components/util2/mvnw .
COPY ./components/util2/.mvn .mvn
COPY ./components/util2/pom.xml .
COPY ./components/util2/src src
RUN ./mvnw clean install

FROM openjdk:8-jdk-alpine as build
WORKDIR /app
COPY --from=build-util2 /root/.m2 /root/.m2
COPY mvnw .
COPY .mvn .mvn
COPY pom.xml .
RUN ./mvnw dependency:go-offline -B
COPY src src
RUN ./mvnw -X package -DskipTests
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf ../*.jar)

FROM openjdk:8-jre-alpine
ARG DEPENDENCY=/app/target/dependency
COPY --from=build ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY --from=build ${DEPENDENCY}/META-INF /app/META-INF
COPY --from=build ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","com.demo.projectx.ProjectX"]

Here is the pom.xml for the project X

<?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 http://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.1.7.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>projectX</artifactId>
    <version>0.1</version>
    <name>projectX</name>
    <description>projectX</description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR3</spring-cloud.version>
        <docker.image.prefix>jxssw</docker.image.prefix>
        <docker.image.name>${project.artifactId}</docker.image.name>
        <docker.image.tag>${project.version}</docker.image.tag>
    </properties>

<dependencies>
        <!-- utils -->
        <dependency>
            <groupId>com.util</groupId>
            <artifactId>util1</artifactId>
            <version>0.1</version>
        </dependency>

        <dependency>
            <groupId>com.util</groupId>
            <artifactId>util2</artifactId>
            <version>0.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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>
            <!-- Dockerfile Maven -->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.10</version>
                <configuration>
                    <googleContainerRegistryEnabled>false</googleContainerRegistryEnabled>
                    <repository>${docker.image.prefix}/${docker.image.name}</repository>
                    <tag>${project.version}</tag>
                </configuration>
                <executions>
                    <execution>
                        <id>build</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>build</goal>
                            <goal>tag</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>push</id>
                        <phase>deploy</phase>
                        <goals>
                            <goal>push</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Dockerfile Maven -->
        </plugins>
        <finalName> ${project.artifactId}</finalName>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

(edit) add the folder structure:

.(parent)
├── projects
|   └── projectX
│       ├── pom.xml
│       ├── Dockerfile
│       └── src
│           └── main
├── components
|   └── util1
│       ├── pom.xml
│       └── src
│           └── main
|   └── util2
│       ├── pom.xml
│       └── src
│           └── main
jhuamanchumo
  • 385
  • 2
  • 7
  • 21
  • if your repo requires authentication you should add .m2 folder with settings.xml – LinPy Sep 27 '19 at 05:28
  • In which repo are util1,util2 published? – leopal Sep 27 '19 at 06:20
  • Util1 and 2 are maven projects in other folders next to this project, to run locally i make clean install un util1 y 2 and artifacts are saved in my local repo ~/.m2, then i make install un this project to compile successfully but how can achieve this in Docker multistage build ? – jhuamanchumo Sep 27 '19 at 12:46
  • Can you publish util1,2 artifacts on a remote repository so that they can be fetched from the build container? That's the simplest approach. If you cant, add another build stage for installing util1,2 and then copy `~/.m2` to the build stage of project X. – leopal Sep 27 '19 at 14:04
  • it may work adding other build stage for all the projects that uses the utils, but how can I install util1,2 in one stage ? FROM openjdk:8-jdk-alpine as build-utils WORKDIR /util COPY ../components/util1/mvnw . COPY ../components/util1/.mvn .mvn COPY ../components/util1/pom.xml . RUN ./mvnw dependency:go-offline -B COPY ../components/util1/src src RUN ./mvnw clean install – jhuamanchumo Sep 27 '19 at 15:21
  • @leopal see the edit, supose that's not a fancy way but how can improve that? – jhuamanchumo Sep 27 '19 at 16:32
  • @jhuamanchumo You could perform all build stuff in one step and copy generated Project's X jar to the final step. – leopal Sep 30 '19 at 06:06

0 Answers0