top of page
Search
  • VIjay Jangir

Maven Wrapper: Maven behind proxy and internal repository using mvnw

TLDR:

Use maven wrapper to automatically resolve dependencies from internal nexus (or any other repository) if you're behind VPN, or from the internet.


One of the issues we usually face in a corporate setting is that the internet is blocked and only accessible using a proxy.. This works fine for browsing but poses challenges to developers during Maven build process, if dependencies has not already been downloaded

A usual workflow for java projects includes below steps:

  1. Cloning a repo from internal repository..

  2. Building project using maven pom.

    1. pom.xml will include some internal repositories hosted on private nexus

    2. pom.xml will also include public repositories (like springboot) from maven central

  3. Typically you'll disconnect your VPN to resolve public repositories

  4. You'll connect back to VPN to resolve private dependencies (eg: company-commans.jar)

  5. Everytime you introduce a new dependency, you'll have to deal with resolving dependencies again.


This is problem in some setups more than other, like one of my client had no internet access when connect to company network, hence the process was very cumbersome


What is the solution?

Most of the organizations implement private registry, but doesn't cascade this information to all developers to use it fully. Private registries have a feature called "proxy", which essentially acts as a mirror to public repository, the upside is that it's accessible on company network without needing access to internet.


I'll take example of Nexus here. In Nexus you can use maven-proxy, and hand it over to developers for resolving their dependencies over internet, the only change required is including settings.xml in

 ~/.m2/

Problem with this solution

This method works well, but every time you connect to internet or VPN, you'll have to switch your settings.xml, You may want to use mirrors of nearest repository tto your region when connected to internet. On would want to include your internal Nexus repository and it's credentials.


Better Solution.

We can easily create an automation script which handles all these scenarios for us and choose thee required settings.xml. For this purpose, I've used Maven Wrapper as it gives me flexibility to control maven version along with my custom automation.


Maven behind proxy and internal repository using mvnw

Maven Wrapper is a maven-provided plugin, this isolates maven installation requirements and gives control to developers as to which maven they need to build their code with.

the mvnw we're using is also customized to work on VPN as well as over the internet by choosing relevant settings.xml based on the below 3 parameters:

  • A. Is the build running on CICD tool in which case it'll use CICD default settings.xml controlled by devops

  • B. Is INTERNAL.REPO reachable (set in mvnw script), it'll create a custom settings.xml to be used with internal repo.

  • C. if not a and b, fall back to default maven (for dependency resolution over the internet), this will honour default settings.xml set by user

Steps to use mvnw for the first time:

  • copy .mvn and mvnw from repository.

  • Initial run for mvnw may requires an active internet connection as it downloads the required maven from maven central and may not work without internet unless you've set up your internal repository for it.

  • Run the below command to install the required dependencies

./mvnw -version   
  • Once mvnw has been run with active internet (without VPN). switch the network to office internet or VPN and run command 2.a again. this will setup the maven for vpn environment.

  • You'll get below prompt when you first run over VPN:

This is first time setting up nexus configuration. Press any button to open settings.xml and update your user and password

  • Press any key and it'll open new settings.xml, you'll have to update your OLM_ID and PASSWORD in the mentioned fields.

  • Save and close the file, return to the terminal where you ran mvnw command and press any key to continue. Your mvnw setup is completed!

Now you've set up your maven behind proxy and internal repository using mvnw






20 views0 comments

Recent Posts

See All

Commentaires


Post: Blog2_Post
bottom of page