Here is a solution with the Maven help:evaluate but with a regex (see the "-E" in command) :
export VERSION=$(mvn --non-recursive help:evaluate -Dexpression=project.version | grep -E '^[0-9]+.[0-9]+.[0-9]+(-[A-Z]+)?$')
echo $VERSION
It will be able to find for example 5.0.1-SNAPHOT or only 5.0.1 or again 5.0.1-GA in the output of the Maven command.
Tested on this kind of realistic Maven output :
Downloaded from fodfin: http://your.server/artifactory/com/thoughtworks/xstream/xstream/1.4.11.1/xstream-1.4.11.1.jar (621 kB at 369 kB/s)
[INFO] No artifact parameter specified, using 'be.test.super.package:the-application:pom:5.0.1-SNAPSHOT' as project.
[INFO]
5.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15.127 s
[INFO] Finished at: 2022-10-06T14:40:29+02:00
[INFO] ------------------------------------------------------------------------
before_script
that wouldsed
the version from your pom file. In Jenkins there are plugins that can simplify such tasks, however GitLab CI doesn't really have such. – Frantz