16

Is there a way to configure Windows Maven and WSL Maven to use the same .m2/repository and settings.xml?

In my .bashrc from WSL I added the M2_HOME to use the same installation that Windows 10 use:

.bashrc:

export M2_HOME=/mnt/c/development/maven

Windows PATH:

C:\development\maven

But when I execute mvn clean install on Windows Bash, a .m2 folder is created in ~/.m2 (WSL folder system).

vitorvr
  • 605
  • 1
  • 5
  • 19

3 Answers3

21

Found a possible solution.

I created a symbolic link from ~/.m2 to /mnt/c/Users/<username/.m2

Command: $ ln -s /mnt/c/Users/<username>/.m2 ~/.m2

Until the moment is working fine.

Felipe Mosso
  • 3,907
  • 11
  • 38
  • 61
vitorvr
  • 605
  • 1
  • 5
  • 19
  • 3
    This 'works', but the performance is dreadful reading the windows filesystem from the WSL. I get build times of: * 3s if I build from the command prompt, using a .m2 cache in the windows Filesystem * 3s if I build from WSL with the .m2 cache in the WSL file system * 20s if I build from WSL with the above symlink – agentgonzo Feb 05 '21 at 18:19
  • 1
    WSL and Win are two filesystem and building across filesystem is very very slow. My suggestion is just have 2 copies of .m2, or move everything to WSL. – WesternGun May 03 '23 at 06:15
1

Just an amendment to @Vitor 's response: When you created the symbolic link once, the next time (e.g. if you specified a wrong directory and want to retry) you cannot create it and you will get failed to create a symbolic link: file exists error.

As a workaround when you want to recreate the symbolic link, you can use $ ln -f -s /mnt/c/Users/<username>/.m2 ~/.m2

[OPTION] 
-f, --force
          remove existing destination files
Balu
  • 522
  • 6
  • 16
0

I did it by setting the local repository path in (WSL) ~/.m2/settings.xml
<localRepository>/mnt/c/Users/<username>/.m2/repository</localRepository>

Normal maven build is a bit slow when repository is in windows file system, however, when it comes to spring-boot native compile or build-image (mvn -Pnative native:compile), it takes lot of time, more than the double as compared to when the local repository is in WSL file system.

sanjeev k
  • 23
  • 5