Override location of the local maven repo

When executing an Upload task, the generated artifacts are always also deployed to the local repository (whatever the actual target repo is), which by default is ~/.m2/repository.

I’d like to be override the location of this local repository through an env var or something. Is that possible?

This functionality is buried deep in the Maven libs we use for this so we can’t easily turn it off unfortunately. Our new publishing stuff doesn’t have this problem though. If you set M2_HOME to a junk space for the Gradle process it will write there. I’m less sure about changing the env var in flight. I’ll investigate and post back.

M2_HOME sounds exactly like what I need for now until we move to the new publishing stuff. thanks!

so I tried export M2_HOME=…

and this doesn’t work.

I’ll move to the new publishing system. thanks.

Are you sure? There are definitely more options to explore with the old approach.

I deleted my local repo and ran the build again and it was back with my artifacts in there.

With the new publishing stuff?

oh sorry, no.

I’m still using the old publishing stuff with the M2_HOME overriden and it still puts still in ~/.m2/repository instead of the path pointed to by M2_HOME.

It’s end of day for me, but I’ll look into this in the morning.

so I moved to the new publishing stuff and this is working fine when publishing to a local repo somewhere. ~/.m2 is not touched.

However I also had an upload task for MavenCentral defined with this:

uploadArchives {

repositories {

mavenDeployer {

beforeDeployment { MavenDeployment deployment ->

if (!project.has(“release”)) {

throw new StopExecutionException(“uploadArchives must be called with the release.gradle init script”)




repository(url: “https://oss.sonatype.org/service/local/staging/deploy/maven2/”) {

authentication(userName: project.ext.sonatypeUsername, password: project.ext.sonatypePassword)





and I’m not sure how to do this with the new plugin?

We don’t have a good solution for dealing with signing with the new stuff yet unfortunately. I think I have a workaround for you for the old stuff though.

The definition of where the local maven repository is for the maven ant tasks is effectively this: http://grepcode.com/file/repo1.maven.org/maven2/org.apache.maven/maven-ant-tasks/2.1.3/org/apache/maven/artifact/ant/AbstractArtifactTask.java#286

Long story short, it’s effectively: ‘new File(System.getProperty( “user.home” ) + “/.m2/repository”’.

This worked for me:

uploadArchives {
 repositories {
  mavenDeployer {
 def userHome = System.getProperty("user.home")
 doFirst {
  System.setProperty("user.home", file("$buildDir/fakem2home").absolutePath)
 doLast {
  System.setProperty("user.home", userHome)

This works for me to though I’m confused. my ~/.m2 folder stayed empty for the fakem2home folder in each sub project’s buildDir is empty too, which seems strange to me…

I don’t know why that would happen.

I know this is a old thread, but I wanted to point out what worked for me.

export M2_HOME=$HOME/builds/maven
export M2_REPO=$M2_HOME/repository

Then in $M2_HOME/conf create a settings.xml file with this content:


It works great for me. I am not 100% certain you need to export $M2_REPO, I have it and I can’t recall if I need it or not.

Thanks for sharing Peter.