MAVEN Basics

June Chung
2 min readOct 30, 2020

--

Maven Life Cycles

Compile : .java → .class (/target)

Test

Package : .class → .jar (/target/sample-1.0.2-SNAPSHOT.jar) — same as .exe zip

Install : .jar → copies in local mvn repository ( local — /.m2/…)

deploy: .jar → push to public server — for other developers

Cash: cash something for other job(stages) to use

cf. How to change Java version
/usr/libexec/java_home -V
export JAVA_HOME=`/usr/libexec/java_home -v <version you want>`
Permanently changing
Add the liner to ~/.bash_profile(bash) or ~/.zshrc (zsh) — $SHELL

  • /.M2 : Home directory of where the mvn commands are running ~/.m2
    This is where .jar and .class are stored/cached
  • Mvn needs to bring from public repository?

2. TEST
→ done by surefire plugin (default, no need to download or configure)
Results are generated … /target/surefire-reports/TEST-*.xml
Results are .txt & .xml … html (by surefire report plugin)

3. PACKAGE
mvn package -Dmaven.test.skip=true

This will build jar files(.exe) in the /target directory
uses maven-jar-plugin(JAR) or maven-war-plugin(WAR)
Final JAR and WAR file = SNAPSHOT (project-4.2.5-SNAPSHOT)
+ can also skip tests

4. DEPLOY
→ save the war (snapshot) in ‘registry’

ex. in gitlab to use ‘deploy’ stage → save in package registry(snapshot)
>create ci-settings.xml in project directory (with job-token)
>change pom.xml file with the url & project ID
>gitlab-ci.yml file change → mvn deploy -s ci-settings.xml -DskipTests
Refer to this website : https://docs.gitlab.com/ee/user/packages/maven_repository/index.html#authenticate-with-a-ci-job-token-in-maven

5. CASH
Refer to : https://docs.gitlab.com/ee/ci/caching/
Cash → multiple PATHs ( — /target, — .m2/…) → PATH is what you want to cache …. normally pushes to external repository googleapi something..
Jobs pick up the cash by KEY of cash

Maven Repository

Maven uses 3 repositories

  • Local repository (.m2)
  • Central repository (Maven Central)
  • Remote repository (Nexus, Gitlab)

Sequence >>
1. Search in local repository → .m2
2. Search in Central repository
3. Remote repository

To manually input a local repository (if not default will be $HOME/.m2)
- Change the [maven installation directory]/conf/settings.xml
There is a <localrepository> section → change this!
- Or input by parameter during mvn command execution
→ mvn clean installorwhatever -Dmaven.repo.local=/home/ec2-user/.m2/repository

--

--