Search code examples
javamavengitlab-ci

Gitlab CI Pipeline - Java and Maven


i was asked in a test to create a CI pipeline with Gitlab for a backend project that had to have two stages: compile and test.

They told me that i was provided with a docker image "java-tests-repository:latest" that i had to use two stages: compile and test; that i had to run 3 tests: registration, payments and orders; that i had to run the compile with the following command "javac -d /src/tests/suites/ -cp /lib/junit-4.12.jar /data/sets/users.java"; that i had to run the tests with maven "mvn clean test -Dtest=""-Ddata_sets=users" that i had to run the tests in parallel; and that i had to run mvn install.

Now, i'm quite new at all of this, but i used Gitalb CI for other more infrastructure related projects. I came out with this:

stages:
  - compile
  - test

image: docker.io/library/java-tests-repository:latest

before_script:
  - mvn install

build:
  stage: compile
  tags:
    - runner01
  script:
    - javac -d /src/tests/suites/ -cp /lib/junit-4.12.jar /data/sets/users.java

registration:
  stage: test
  tags:
    - runner01
  script:
    - mvn clean test -Dtest=registration -Ddata_sets=users
  needs:
    - job: build

payments:
  stage: test
  tags:
    - runner01
  script:
    - mvn clean test -Dtest=payments -Ddata_sets=users
  needs:
    - job: build    

orders:
  stage: test
  tags:
    - runner01
  script:
    - mvn clean test -Dtest=orders -Ddata_sets=users
  needs:
    - job: build

My question is does it make sense to run mvn install in the before script ? Based on my understanding i need to run maven install to get the dependecies that are declared in the pom.xml, so before compiling it could make sense, but does it need to be run before the "mvn clean test" as well ?


Solution

  • The whole script is completely wrong.

    You never call javac manually, and you never separate unit tests into different jobs.

    Whoever told you to run that javac command for compilation has zero knowledge of Maven or Java builds in general.

    What you do instead: You define just one job that only runs mvn clean test. To run your tests in parallel, look into the documentation of the surefire maven plugin.