skip to Main Content

I have an application in java that is deployed inside a docker container, but every time I make a new release I have to manually update the version of the app in the docker file, is there any way to automate this by taking the version directly from the pom?

Here my dockerfile

FROM openjdk:17
COPY target/projectName-x.x.x.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

obviously instead of x.x.x is the correct version number

and here the 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>3.1.5</version>
    <relativePath/> 
</parent>
<groupId>com.groupName</groupId>
<artifactId>projectArtifactId</artifactId>
<version>x.x.x-SNAPSHOT</version>
<name>projectName</name>
<properties>
    <java.version>17</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</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-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
        <version>3.1.5</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <image>
                    <builder>paketobuildpacks/builder-jammy-base:latest</builder>
                </image>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>it.ivannotarstefano.ultimateserver.UltimateServerApplication</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

2

Answers


  1. There are couple of ways you can try,

    1. If you are using gitlab ci/cd pipeline then, you can write below steps in you .gitlab-ci.yaml file,

    .gitlab-ci.yaml

    stages:
       - stage_1
       - stage_2
       - versioning
       - build
     variable:
        APP_VERSION: "1.0"
     versioning:
        stage: "versioning"
        script: 
          - if [! -f version.txt]; then echo "$APP_VERSION" > version.txt fi
          - APP_VERSION=$(echo "$APP_VERSION + 1.0" | bc)
          - echo "$APP_VERSION" > version.txt
      build:
        stage: "build"
        script: 
           - docker build --build-arg APP_VERSION="$APP_VERSION" -t projectName:"$APP_VERSION" .
    

    But only two changes you have to do in you above dockerfile,

    FROM openjdk:17
    ARG APP_VERSION
    COPY target/projectName-${APP_VERSION}.jar app.jar
    ENTRYPOINT ["java","-jar","/app.jar"]
    
    1. If you are doing it locally you can create you own build.sh file and do below steps,

      # check if version.txt file exist, otherwise create one and store initial version
         if [ ! -f version.txt]; then echo "1.0" > version.txt fi
      # declare a variable to store current version
         CURRENT_VERSION=$(<version.txt)
      # create a new version
         UPDATED_VERSION=$(echo "$CURRENT_VERSION + 1.0" | bc)
      # write new version back to file
         echo "$UPDATED_VERSION" > version.txt
      # then build docker image with new version
         docker build --build-arg APP_VERSION="$UPDATED_VERSION" -t projectName:"$UPDATED_VERSION" .
      

    Hope this will resolve your issue

    Login or Signup to reply.
  2. Look, this approach is not very good. Docker can’t take something and put "variable" in it to reuse.
    What you need is to retrieve as compile argument

    FROM openjdk:17
    ARG ARTIFACT_VERSION
    COPY target/projectName-$ARTIFACT_VERSION.jar app.jar
    ENTRYPOINT ["java","-jar","/app.jar"]
    

    When compiling, pass the version.

    docker build --build-arg ARTIFACT_VERSION=1 .
    

    You can retrieve the version with this command:

    ARTIFACT_VERSION_FROM_POM=$(mvn org.apache.maven.plugins:maven-help-plugin:3.2.0:evaluate -Dexpression=project.version -q -DforceStdout)
    

    all together

    docker build --build-arg ARTIFACT_VERSION=$ARTIFACT_VERSION_FROM_POM .
    

    Another option would be to set the artifact name to fixed.
    Putting the lines in pom.xml:

       <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <finalName>${project.parent.artifactId}-fixed-version</finalName>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search