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
There are couple of ways you can try,
.gitlab-ci.yaml
But only two changes you have to do in you above dockerfile,
If you are doing it locally you can create you own build.sh file and do below steps,
Hope this will resolve your issue
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
When compiling, pass the version.
You can retrieve the version with this command:
all together
Another option would be to set the artifact name to fixed.
Putting the lines in pom.xml: