title: Gitlab-Ci. Transfer maven m2 local repository between stages
body: <p>Good day collegues. I use gitlab ci in production. I have a lot of stages. 
  1)Build artifact
  2)Deploy to external server
  3)Using jfrog cli to deploy to artifactory</p>
  
  <p>I have a problem with caching maven local repository. My runners download all dependicies in first step(build) and do the same in the last step(deploy to artifactory). Also my  runner delete all data from m2 folder before final stage:</p>
  
  <pre><code>Removing .m2/antlr/
  Removing .m2/aopalliance/
  Removing .m2/asm/
  Removing .m2/avalon-framework/
  Removing .m2/backport-util-concurrent/
  Removing .m2/ch/
  Removing .m2/classworlds/
  Removing .m2/com/
  Removing .m2/commons-beanutils/
  Removing .m2/commons-chain/
  Removing .m2/commons-cli/
  Removing .m2/commons-codec/
  Removing .m2/commons-collections/
  Removing .m2/commons-digester/
  Removing .m2/commons-io/
  Removing .m2/commons-lang/
  Removing .m2/commons-logging/
  Removing .m2/commons-validator/
  Removing .m2/dom4j/
  Removing .m2/io/
  Removing .m2/javax/
  Removing .m2/junit/
  Removing .m2/log4j/
  Removing .m2/logkit/
  Removing .m2/net/
  Removing .m2/org/
  Removing .m2/oro/
  Removing .m2/sslext/
  Removing .m2/xml-apis/
  Removing .m2/xmlunit/
  Removing jfrog
  Removing target/
  </code></pre>
  
  <p>my gitlav-ci yaml(withou second step):</p>
  
  <pre><code>stages:
  - build
  - deploy-artifactory
  
  variables:
    MAVEN_OPTS: "-Dmaven.repo.local=${CI_PROJECT_DIR}/.m2"
    MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
  
  cache:
    key: "$CI_JOB_NAME"
    paths:
    - .m2/
  
  build:
    image: maven:latest
    stage: build
    tags:
    - build
    script:
    - adduser --disabled-password --gecos '' mynonrootuser
    - chmod --recursive 777 .
    - git version
    - su --command='mvn versions:set -DgenerateBackupPoms=false -DnewVersion="$CI_COMMIT_REF_SLUG-SNAPSHOT"' mynonrootuser
    - su --command='mvn $MAVEN_CLI_OPTS clean install -B -f ./pom.xml' mynonrootuser
    artifacts:
      expire_in: 5 mins
      paths:
      - target/*.jar
  
    only:
    - develop
  
  deploy-artifactory-snapshot:
    retry: 2
    image: maven:latest
    stage: deploy-artifactory
    tags:
    - release
    before_script:
    -  curl -fL https://getcli.jfrog.io | sh
    - ./jfrog rt config --url=${ARTIFACTORY_URL} --user=${ARTIFACTORY_USER} --password=${ARTIFACTORY_PASSWORD}
    - ./jfrog rt c show
    - export M2_HOME=/usr/share/maven
    - sed -i 's,MAVEN_REPO_SNAPSHOT_DEPLOYER,'"$MAVEN_REPO_SNAPSHOT_DEPLOYER"',g' configuration.yml
    - sed -i 's,MAVEN_REPO_RELEASES_DEPLOYER,'"$MAVEN_REPO_RELEASES_DEPLOYER"',g' configuration.yml
    - sed -i 's,MAVEN_REPO_SNAPSHOT_RESOLVER,'"$MAVEN_REPO_SNAPSHOT_RESOLVER"',g' configuration.yml
    - sed -i 's,MAVEN_REPO_RELEASES_RESOLVER,'"$MAVEN_REPO_RELEASES_RESOLVER"',g' configuration.yml
    script:
    - ./jfrog rt mvn "versions:set -DgenerateBackupPoms=false -Dartifactory.publish.artifacts=false -DnewVersion="$CI_COMMIT_REF_SLUG-SNAPSHOT"" configuration.yml --build-name=scdfrestrunner --build-number=$CI_JOB_ID
    - ./jfrog rt mvn "clean install" configuration.yml --build-name=scdfrestrunner --build-number=$CI_JOB_ID
    - ./jfrog rt bce scdfrestrunner $CI_JOB_ID
    - ./jfrog rt bp scdfrestrunner $CI_JOB_ID
  
    only:
    - develop
  </code></pre>