11

My local maven repo is here /Users/power/.m2/repository.

But I got this error

[java] [ERROR] Could not create local repository at /var/root/.m2/repository -> [Help 1]

Seems Maven thinks that it should use a root user repo. How can I fix it? I don't need to run my maven tasks using root permissions.

JohnWinter
  • 1,003
  • 5
  • 12
  • 25

3 Answers3

14

The default maven repository is

${user.home}/.m2/repository/

but you can use settings.xml ( ${user.home}/.m2/settings.xml ) to change it to a folder that you have permissions on. Or conf/settings.xml in the ${MAVEN_HOME} and change:

<settings>
...
<localRepository>/path/to/local/repo/</localRepository>
...
</settings>

Ideally, you should run maven as yourself and not root to make sure you have permissions or doing 'ksu' first and then use command line.

ingrid.e
  • 531
  • 2
  • 11
5

This error can occur if there is a file called .m2. (Most probably created mistakenly. This happened to me when I copied settings.xml as .m2) If you can delete this and run mvn command again it will create the .m2 folder and you can proceed without a hassel.

Adiesha
  • 133
  • 2
  • 9
  • 1
    If you do this in a Dockerfile: `ADD settings.xml /root/.m2` it will do exactly this. Use: `ADD settings.xml /root/.m2/settings.xml` to avoid the error. – David Avendasora Aug 24 '20 at 06:20
4

Deleting the .m2 folder manually helps sometimes

Your .m2 folder might be corrupted and it doesn't permit you to create new or replace existing with new .m2 folder, so delete the existing .m2 folder manually by entering the below commands.

To view the existing .m2 folder

ls -ltra

To Delete it manually

sudo rm .m2
mnille
  • 1,328
  • 4
  • 16
  • 20