Gitlab docker runner mvn: command not found

I am trying to build ci/cd withing gitlab runner docker image

build: script: - mvn install

but I face below error

$ mvn install bash: line 85: mvn: command not found

@ahmedyosry963 welcome to the forum! :wave:

In order to use Maven, you’ll need to select a Docker image to build inside of that has Maven included. To do that add an image: tag to your .gitlab-ci.yml. For a list of “official” Maven images, you can see the Maven listing on Docker Hub, as it will depend on the version of Java you are building for.

But in the end you’ll add something like this:

image: maven:3.6.3-jdk-11

You can also see a full working .gitlab-ci.yml example for Java here.

1 Like

Thanks a lot for you replay, and sorry for delay
i need to tell you that i have installed gitlab docker image and gitlab runner docker image , both of them different containers and runner registered with no problem.
what i need to do ?
-Build jar file
-Copy it to a special path to run it .

so i did the below but unfortunately it’s failed with error.

-------------------------------gitlab ci file-------------------------------
image: maven:latest

variables:
MAVEN_CLI_OPTS: “-s .m2/settings.xml --batch-mode”
MAVEN_OPTS: “-Dmaven.repo.local=.m2/repository”

cache:
paths:

  • .m2/repository/
  • target/

build:
stage: build
script:

  • mvn $MAVEN_CLI_OPTS compile

-------------------------------Error-------------------------------
Running with gitlab-runner 13.0.1 (21cb397c)
on java-runner s_9CedZv
Preparing the “shell” executor
00:00
Using Shell executor…
Preparing environment
00:00
Running on 0eb6dc3504ba…
Getting source from Git repository
00:01
Fetching changes with git depth set to 50…
Reinitialized existing Git repository in /home/gitlab-runner/builds/s_9CedZv/0/yousry/sample-ci/.git/
From http://X.X.X.X/yousry/sample-ci

[new ref] refs/pipelines/30 -> refs/pipelines/30
4affb7c…f2be837 master -> origin/master
Checking out f2be837b as master…
Skipping object checkout, Git LFS is not installed.
Skipping Git submodules setup
Restoring cache
00:00
Checking cache for default…
Runtime platform arch=amd64 os=linux pid=310 revision=21cb397c version=13.0.1
No URL provided, cache will not be downloaded from shared cache server. Instead a local version of cache will be extracted.
Successfully extracted cache
Downloading artifacts
00:00
Running before_script and script
00:00
$ mvn $MAVEN_CLI_OPTS compile
bash: line 87: mvn: command not found
Running after_script
00:00
Uploading artifacts for failed job
00:00
ERROR: Job failed: exit status 1

-------------------------------My issue now is --------------------------------------------------------------
My issue here why i cannot use mvn command inside maven container ???

It looks like you’ve set up the runner as a “shell” executor and not a docker executor, so the commands are actually running in the shell of the host, not inside of a Docker container.

See our docs on setting up a Docker based runner for more details on how to do that.

1 Like

I appreciate your support, I tried docker executor and it worked fine thanks a lot, however, the reason for using runner as Shall executor is that I need to build a normal jar file then copy it to a specific path on the host server.

After changing the runner to docker how I will copy the jar file ?

Makes sense!

There’s a few options here:

  1. Mount the place you need to copy the jar file to to the docker runner as a docker volume using the config.toml.
  2. You can run the mvn command on the shell executor if you want, but you’ll have to install Maven on the host machine.
  3. To copy the jar file you could make it an artifact, and then have a job after the build on the shell runner (by using runner tags) to copy it to the correct location.
1 Like

I need to highlight that my runner is on docker image below is my environment details

  • Gitlab on a docker container host on a local server.
  • Gitlab runner on a docker container host on a local server.
  • Java app running on the host server.

As I will not only need to copy the jar file I need to take backup and run it, I would prefer using the 2nd option (Maven already installed on host server).

The problem now is :

  • How I can user mvn command inside the gitlab runner container?
  • How I will execute sh script on the host to copy , backup and run the jar?

Please if you recommend using the 1st option let me know with the steps.

@olearycrew Your support is highly appreciated.

@olearycrew can you please help me ?

Hello

I have to same problem

mvn command do not found by Gitlab
despite the image is maven

image: maven:3.6.3-jdk-11

variables:
  MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
  MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"

cache:
  paths:
    - .m2/repository/
    - target/

build:
  stage: build
  script:
    - whereis mvn
    - whereis java
    - mvn compile

And this is the result
Notice whereis mvn return empty result

Running with gitlab-runner 12.3.0 (a8a019e0)
  on Ubuntu AWS eu-central-1 ngKQKuyR
Using Shell executor...
00:00
Running on ip-172-31-4-182...
00:00
Fetching changes with git depth set to 50...
00:00
 Reinitialized existing Git repository in /app/gitlab-runner/builds/n/base/applications/web/.git/
 From https://git.app/base/applications/web
  * [new ref] refs/pipelines/8 -> refs/pipelines/8856
  d3c67..8240d test-ci -> origin/test-ci
Checking out 85a24c as test-ci...
Skipping Git submodules setup
Checking cache for default...
00:00
Runtime platform arch=amd64 os=linux pid=12253 revision=a8a019e0 version=12.3.0
No URL provided, cache will not be downloaded from shared cache server. Instead a local version of cache will be extracted.
Successfully extracted cache
$ whereis mvn
mvn:
$ whereis java
java: /usr/share/java
$ mvn compile
bash: line 98: mvn: command not found
ERROR: Job failed: exit status 1

Have you an idea about this problem ?

I have the same problem, apparently it is that the maven image is not installed, in the same way I need help.

I solved it by installing maven and java on the same server.