Maven wrapper is a library to make maven build easy. You do not need to install maven manually thanks to maven wrapper because it automatically install the specific maven version into your project. Since I usually use gradle wrapper and sbt-extras, it was very easy to use maven wrapper as well in my Java project.

But I had a trouble to build a project after switching user. For example, I wanted to create build environment by root user but building the project itself should be done by non-priviledged user. I initially thought it can be achieved by sudo -u lewuathe:

$ sudo -u lewuathe ./mvnw install 

But it failed due to FailNotFoundException.

Exception in thread "main" java.io.FileNotFoundException: /home/lewuathe/.m2/wrapper/dists/apache-maven-3.3.9-bin/2609u9g41na2l7ogackmif6fj2/apache-maven-3.3.9-bin.zip.part (No such file or directory)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:270)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:162)

I was disapponted of this because I believed maven wrapper automatically :( I read the source code of maven wrapper and finally found a solution. Maven wrapper gets the base directory where maven is installed from MAVEN_USER_HOME environment variable.

So finally what we need to do is this:

$ mkdir /home/lewuathe/.m2
$ MAVEN_USER_HOME=/home/lewuathe/.m2 sudo -u lewuathe ./mvnw ...

It worked!