How to best define .m2 local maven repo path in multi-branch projects/Jenkinsfile?

2,138 views
Skip to first unread message

ST

unread,
Aug 15, 2016, 12:36:30 PM8/15/16
to jenkins...@googlegroups.com
Hi!

I am migrating our build pipeline from a set of maven-type jobs to a multi-branch project defined in a Jenkinsfile, and I am wondering how people solve the .m2 problem ? I cannot see any way to configure every job/branch to have its own private .m2 repo through a comfortable boolean config option, did I miss something?

Otherwise I guess the solution is to set the .m2 path via -Dmaven.repo.local. But how to best define this path so it is different for every job/branch? Put it under /tmp/<GIT_BRANCH_NAME>? Any other more elegant options?

Best regards,
 stefan.


Álvaro Lobato

unread,
Aug 16, 2016, 4:25:32 PM8/16/16
to Jenkins Users
Hello Stefan,

An easy way to do it is to use the new Pipeline Maven plugin. It allows you to define a local maven repository, relative to to the workspace of the job or with an absolute path, you can also use shell variables expansion. In your case would be as easy as using a job similar to this one:

withMaven(mavenLocalRepo: '.repository') {
        // Run the maven build
        sh "mvn clean package" 
} 

This will execute the specified mvn command with a local repo on the folder .repository inside the workspace.

You can also use other parameters to customise you maven behaviour or even auto-install it.  If you want more information check the plugin wiki page or this thread from the forum.

It is still in beta phase, but working well so far.

Cheers
Alvaro

ST

unread,
Aug 19, 2016, 3:32:26 AM8/19/16
to Jenkins Users
Hi Alvaro,

Thanks for your reply. So in a multi-branch project, do you have any better proposal than to use the following?
withMaven(mavenLocalRepo: "/tmp/${env.BRANCH_NAME}/.repository") {
        
// Run the maven build
        sh "mvn clean package" 
} 

Since we're "mvn install"-ing snapshots, we cannot reuse the same .m2 repo for different branches...

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/5970854a-5e55-4475-bf75-5da0227202d9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Álvaro Lobato

unread,
Aug 19, 2016, 5:46:00 AM8/19/16
to jenkins...@googlegroups.com

I think that multibranch have a workspace for each branch (I'm away from my computer and can't check it) if that is the case it is safe to just use .repository, if not you can use what you propose or .reposotory_${env.BRANCH_NAME} which will make it relative and it should be persistent between executions.


You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/6NCL_RTSRDY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/CABwQARtxFg8f82BxxM_aToua8Q3hG_qUsZqjrQt871cVkQOEtA%40mail.gmail.com.

Nazarii Bardiuk

unread,
Mar 16, 2017, 7:32:48 AM3/16/17
to Jenkins Users
We also have similar situation with multiple branches and the same maven snapshot version.
Stefan, did you manage to run builds for separate branches without version clashes?
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "Jenkins Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jenkinsci-users/6NCL_RTSRDY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jenkinsci-use...@googlegroups.com.

David Karlsen

unread,
Mar 16, 2017, 10:47:05 AM3/16/17
to Jenkins Users
I keep it in the workspace so it disappears with it 

Reply all
Reply to author
Forward
0 new messages