Maven Tips

--

I have two tricks to share. Both of them came relevant to my recent task at work. So sharing it here to keep a reference for myself and anyone who might need it.

  1. Setting custom environment variables for each maven build

We all have multiple environment variables set in the `~/.bashrc`, `~/.bash_profile`, or for newer macs with ZSH terminal as default `~/zshrc`

However, we often build multiple projects from the same terminal and the java, maven or other toolchain components might be at different versions for different projects. In that case, setting them all in the bashrc/bash_profile etc. will lead to conflicts (Not exactly a conflict, But only the last setting in bashrc will be considered) inevitably.

For such a case above, what I ended up doing was creating a wrapper bash script around maven to have the individual variables set for the different projects. My wrapper script looks like below:

mvn.sh

(To be placed under the project source root or directory where you usually trigger the build from, so it is intuitive which mvn.sh is for what project)

#!/usr/bin/env bashexport P4CLIENT=<p4_client_name>
export TCDIR=/a/b/c
export ANOTHER_VARIABLE=/home/<user>/perforce/<folder>
export MAVEN_OPTS='-Xmx1024m -Xms256m -XX:ReservedCodeCacheSize=64m'
export M2_HOME=${TCDIR}/noarch/apache-maven-3.2.3
export M2_REPO=${HOME}/.m2/repository
export JAVA_HOME=${TCDIR}/lin32/jdk-1.8.0_71
export PATH=$JAVA_HOME/bin:$M2_HOME/bin:$PATH
mvn $@

You are ready to run the maven command as usual after that! Just append any other maven options after the ./mvn.sh. Like so:

./mvn.sh clean compile -Dmaven.test.skip=false  -Dmaven.repo.local=$M2_REPO --settings=../maven-settings.xml

2. Setting up a custom repository/folder for fetching the dependencies.

We sometimes have to restrict the source of dependencies to internal maven repositories, for compliance reasons. In such cases, we can use any general network share with HTTP directory access as a maven repository and configure maven to look for dependencies exclusively in that folder. For this, we can set up the <repository> tag in maven `settings.xml`. Like so:

<?xml version="1.0"?>
<settings>
<proxies>
<proxy>
<active>false</active>
<protocol>http</protocol>
<host>proxy.abc.com</host>
<port>3128</port>
<!-- Restrict internet through proxy, allow all internal sites to go through non-proxy hosts -->
<nonProxyHosts>*.abc.com|*.abc.com</nonProxyHosts>
</proxy>
</proxies>
<profiles>
<profile>
<id>root-profile</id>
<repositories>
<!-- All that is used should be this repository -->
<repository>
<id>build-maven-repo</id>
<name>Go Build Build specific Maven Repo</name>
<url>https://abc.com/build/m2-repository/</url>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>root-profile</activeProfile>
</activeProfiles>
</settings>

Note: you may ignore the <proxies> setting. this is just in case if you need to restrict internet access through a proxy.

Cheers!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response