Answers: <p>To me the easiest route to this would be to have an instance of an artifactory running. There are several choices out there many of which have an open-source, community or free version that would support maven repositories.  Most options will support caching remote repositories (maven central for example) which can then be curated to enforce specific versions of the dependencies through your projects. It will also accept your own dependencies this way you can have a central unique access point for all maven dependencies in your team.</p>
  
  <p>I've personally had success with Sonatype's Nexus (<a href="https://www.sonatype.com/nexus-repository-oss" rel="nofollow noreferrer">OSS version</a>, or <a href="https://www.sonatype.com/nexus-repository-software-component-management" rel="nofollow noreferrer">Pro version</a>). There are <a href="https://binary-repositories-comparison.github.io/" rel="nofollow noreferrer">other choices</a> out there though check them out.</p>
  
  <p>That said, if what you want is to prime the local repository for a new agent you want to provision, you could do so. But frankly, unless you have some real hefty dependencies (say in the GB size range), I would not bother doing so really and just let Maven naturally pull from the local binary repository instead. It makes for a simpler solution that will be more robust in the long run.</p>
  
Answers: CC BY-SA 3.0
Answers: 2017-04-26T17:29:46.627Z
Answers: What I wanted was to use gitlab-ci's cache feature. Can you help me out with that.
Answers: radbrawler
Answers: CC BY-SA 3.0
Answers: 2017-04-26T17:30:17.507Z
Answers: though what I wanted was different but you've put a good option for me and my team to discuss upon. Thanks @Newtopian
Answers: radbrawler
Answers: CC BY-SA 3.0
Answers: 2017-04-26T18:04:10.390Z
Answers: I don't have  big experience with gitlab-ci so I cant really help there.  However when trying something similar with Team-City (artifacts in their lingo) we did end up with issues that the artifactory just naturally solved.  The killer sell point was that it enabled a whole lot of new options that we did not even consider before further simplifying the overall pipeline.  It just felt right as a whole swath of relative awkwardness found a natural home to settle in.
Answers: Newtopian
Answers: CC BY-SA 3.0
Answers: 2017-08-31T15:30:09.423Z
Answers: Sorry for the nitpicking ... and hijacking your answer to soon post a new question (hang on ...).
Answers: Pierre.Vriens
Answers: CC BY-SA 3.0
Answers: 2017-04-26T14:03:35.833Z
Answers: 2017-08-31T15:25:32.777Z
Answers: 2017-08-31T15:25:32.777Z
Answers: Newtopian
Answers: <p>GitLab CI includes caching of directories, <strong>relative to working directory</strong>. You can use this to cache maven local repository.</p>
  
  <pre><code>myjob:
    image: maven
    variables:
      MAVEN_OPTS: "-Dmaven.repo.local=${CI_PROJECT_DIR}/.repository/"
    cache:
      key: maven
      paths: [.repository/]
    script:
    - mvn clean verify
  </code></pre>
  
  <p>You'll see something like this in job log:</p>
  
  <pre><code>Checking cache for maven...
  Downloading cache.zip from https://storage.googleapis.com/gitlab-com-runners-cache/project/133221/maven 
  ...
  Creating cache maven...
   .repository/: found 948 matching files             
   Uploading cache.zip to https://storage.googleapis.com/gitlab-com-runners-cache/project/133221/maven 
  </code></pre>
  
  <p>See <a href="https://docs.gitlab.com/ee/ci/caching/" rel="nofollow noreferrer">GitLab caching documentation</a>.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-01-15T14:21:16.443Z
Answers: 2020-01-15T14:21:16.443Z
Answers: Étienne Bersac
Body: <p>We have a server running gitlab and gitlab-runners on same machine. I've defined a runner as below</p>
  
  <pre><code>[abc@xyz bin]# gitlab-ci-multi-runner register
  Running in system-mode.                            
  
  Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
  http://abc.xyz.com                                                                     
  Please enter the gitlab-ci token for this runner:
  9nev5wz3voabcdefgh
  Please enter the gitlab-ci description for this runner:
  [opera.ishisystems.com]: second-docker-remote-runner
  Please enter the gitlab-ci tags for this runner (comma separated):
  docker, remote
  Whether to run untagged builds [true/false]:
  [false]: 
  Whether to lock Runner to current project [true/false]:
  [false]: 
  Registering runner... succeeded                     runner=9nev5wz3
  Please enter the executor: docker, docker-ssh, ssh, docker+machine, docker-ssh+machine, parallels, shell, virtualbox, kubernetes:
  docker
  Please enter the default Docker image (e.g. ruby:2.1):
  node:4.2.2
  Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 
  </code></pre>
  
  <p>and my <code>.gitlab-ci.yml</code> is </p>
  
  <pre><code>image: maven:3-jdk-7
  
  services:
    - wurstmeister/kafka:0.8.2.1
  
  variables:
    MAVEN_REPO: "$CI_PROJECT_DIR/.m2"
  
  cache:
      key: "$CI_BUILD_STAGE"
      paths:
        - .m2
  
  build:
    script: "mvn -Dmaven.repo.local=$MAVEN_REPO test -B"
    tags:
    - docker
  </code></pre>
  
  <p>What I'm trying to do is setup a <strong>central maven repository</strong> on my gitlab-runner server so that every time a new job is fired <strong>instead of downloading</strong> all the maven dependencies it should <strong>use the cached repository</strong>.</p>
  
Tags: <gitlab>
Title: Using gitlab-ci to cache maven repository
UserDisplayName: radbrawler
ThreadText: # Using gitlab-ci to cache maven repository
  
  (Asked by: username_0 on 2017-04-26)
  
  We have a server running gitlab and gitlab-runners on same machine. I've defined a runner as below
  
  ```
  [abc@xyz bin]# gitlab-ci-multi-runner register
  Running in system-mode.                            
  
  Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
  http://abc.xyz.com                                                                     
  Please enter the gitlab-ci token for this runner:
  9nev5wz3voabcdefgh
  Please enter the gitlab-ci description for this runner:
  [opera.ishisystems.com]: second-docker-remote-runner
  Please enter the gitlab-ci tags for this runner (comma separated):
  docker, remote
  Whether to run untagged builds [true/false]:
  [false]: 
  Whether to lock Runner to current project [true/false]:
  [false]: 
  Registering runner... succeeded                     runner=9nev5wz3
  Please enter the executor: docker, docker-ssh, ssh, docker+machine, docker-ssh+machine, parallels, shell, virtualbox, kubernetes:
  docker
  Please enter the default Docker image (e.g. ruby:2.1):
  node:4.2.2
  Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 
  
  ```
  
  and my `.gitlab-ci.yml` is
  
  ```
  image: maven:3-jdk-7
  
  services:
    - wurstmeister/kafka:0.8.2.1
  
  variables:
    MAVEN_REPO: "$CI_PROJECT_DIR/.m2"
  
  cache:
      key: "$CI_BUILD_STAGE"
      paths:
        - .m2
  
  build:
    script: "mvn -Dmaven.repo.local=$MAVEN_REPO test -B"
    tags:
    - docker
  
  ```
  
  What I'm trying to do is setup a **central maven repository** on my gitlab-runner server so that every time a new job is fired **instead of downloading** all the maven dependencies it should **use the cached repository**.
  
  **Tags:** gitlab
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-04-26. Score: 7
  
  To me the easiest route to this would be to have an instance of an artifactory running. There are several choices out there many of which have an open-source, community or free version that would support maven repositories. Most options will support caching remote repositories (maven central for example) which can then be curated to enforce specific versions of the dependencies through your projects. It will also accept your own dependencies this way you can have a central unique access point for all maven dependencies in your team.
  
  I've personally had success with Sonatype's Nexus ([OSS version](https://www.sonatype.com/nexus-repository-oss), or [Pro version](https://www.sonatype.com/nexus-repository-software-component-management)). There are [other choices](https://binary-repositories-comparison.github.io/) out there though check them out.
  
  That said, if what you want is to prime the local repository for a new agent you want to provision, you could do so. But frankly, unless you have some real hefty dependencies (say in the GB size range), I would not bother doing so really and just let Maven naturally pull from the local binary repository instead. It makes for a simpler solution that will be more robust in the long run.
  
  > **Comments:**
  > **username_0**: What I wanted was to use gitlab-ci's cache feature. Can you help me out with that.  
  > **username_0**: though what I wanted was different but you've put a good option for me and my team to discuss upon. Thanks @username_1  
  > **username_1**: I don't have  big experience with gitlab-ci so I cant really help there.  However when trying something similar with Team-City (artifacts in their lingo) we did end up with issues that the artifactory just naturally solved.  The killer sell point was that it enabled a whole lot of new options that we did not even consider before further simplifying the overall pipeline.  It just felt right as a whole swath of relative awkwardness found a natural home to settle in.  
  > **username_2**: Sorry for the nitpicking ... and hijacking your answer to soon post a new question (hang on ...).  
  
  ---
  
  ### Answer by username_3 on 2020-01-15. Score: 1
  
  GitLab CI includes caching of directories, **relative to working directory**. You can use this to cache maven local repository.
  
  ```
  myjob:
    image: maven
    variables:
      MAVEN_OPTS: "-Dmaven.repo.local=${CI_PROJECT_DIR}/.repository/"
    cache:
      key: maven
      paths: [.repository/]
    script:
    - mvn clean verify
  
  ```
  
  You'll see something like this in job log:
  
  ```
  Checking cache for maven...
  Downloading cache.zip from https://storage.googleapis.com/gitlab-com-runners-cache/project/133221/maven 
  ...
  Creating cache maven...
   .repository/: found 948 matching files             
   Uploading cache.zip to https://storage.googleapis.com/gitlab-com-runners-cache/project/133221/maven 
  
  ```
  
  See [GitLab caching documentation](https://docs.gitlab.com/ee/ci/caching/).
  
  
  ---
  
  