Override location of the local maven repo

(Xavier Ducrohet) #1

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?

(Luke Daley) #2

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.

(Xavier Ducrohet) #3

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

(Xavier Ducrohet) #4

so I tried export M2_HOME=…

and this doesn’t work.

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

(Luke Daley) #5

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

(Xavier Ducrohet) #6

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

(Luke Daley) #7

With the new publishing stuff?

(Xavier Ducrohet) #8

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.

(Luke Daley) #9

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

(Xavier Ducrohet) #10

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?

(Luke Daley) #11

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)

(Xavier Ducrohet) #12

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…

(Luke Daley) #13

I don’t know why that would happen.

(Peter Long) #14

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.

(Luke Daley) #15

Thanks for sharing Peter.