[Hudson] How to set a private maven repository by job and easily be able to delete them
Par batmat le vendredi 9 octobre 2009, 10:17 - Technique - Lien permanent
When building maven projects with hudson, there's some common best practices about maven repositories handling :
- isolate maven repositories between jobs
- regularly purge repositories
The problem
The basic way to do it is to activate the hudson per-job option : "Use private Maven repository". But the thing is you have to do it for EVERY new job you add. There is no way inside hudson to activate it globally.
Documented solution
If you look at hudson help for this option, you'll see a link to a simple solution that specify the repository directly in the maven settings.xml file. The tip is to redefine the localRepository tag inside settings with this special value :
<localRepository>${env.WORKSPACE}/m2_repo</localRepository>
This way, you're done with the first best practice : isolate maven repositories. But not yet with the regularly purge repositories one.
Actually, using this option will put the m2_repo inside each hudson job workspace. So, finding and deleting them could become a bit cumbersome. You'd have to cron something like find . -name m2_repo -exec rm -rf "{}" \;.
Even better
As you might have understood, I was not thoroughly satisfied with this solution. I wanted to be able to really easily delete the repositories. So I just changed the option above to have them all inside the same root directory under ~/.m2/repositories, one per job.
Quite simple in fact, instead, just use :
<localRepository>/some/path/.m2/repositories/${env.JOB_NAME}/repository</localRepository>
This way, the only thing you have to put in the cron job is rm -rf /some/path/.m2/repositories/. A bit more straightforward, isn't it? 
Hope this helps.


Commentaires
Quoi Maven ?
Qu'est-ce qu'elle a Maven ?
Il est fou Afflelou il est fou !
PS : sorry to english readers, you're missing the top of french puns.
Pas mal, Raf. :). Chez nous, la blague la plus courante, c'est plutôt quand ça marche pas : "ah ben, c'est bien maveine, tiens !".
Bon, allez, je reprends mon traitement : un clown en comprimé, matin, midi et soir :-).
Cheers.
Ca ne marche pas très bien...
J'ai des jobs de type "Maven2", et il semble que Hudson essaye de parser les pom à sa sauce avant de lancer le build. Dans le cas où le projet référence un pom parent, Hudson essaye de le télécharger, mais ne remplace pas les variables.
J'ai donc un répertoire /some/path/.m2/repositories/${env.JOB_NAME}/repository qui est créé et qui contient uniquement le pom parent (${env.JOB_NAME} n'est pas remplacé).
Pour la suite du build, les autres dépendances sont bien récupérées au bon endroit.
@Julien, oui, j'ai vu que ça faisait pareil chez nous. Ce n'est pas très joli, mais je n'ai pas encore pris le temps de regarder pourquoi et comment résoudre le problème.
Alors chez moi ça ne fonctionne pas du tout.
Où faut-il mettre la ligne <localRepository> dans settings.xml ?
Selon l'endroit où je la mets, j'ai soit MavenEmbedderException: Error creating settings. / Unrecognised tag: 'localRepository',
ou alors l'erreur que j'avais déjà avant :
WARN repository metadata for: 'snapshot com.platform:application-parent:0.0.1.SNAPSHOT' could not be retrieved from repository: public-snapshots due to an error: Error transferring file
INFO Repository 'public-snapshots' will be blacklisted
Voici la ligne que j'ai ajoutée :
<localRepository>/var/lib/hudson/.m2/repositories/${env.JOB_NAME}/repository</localRepository>Après avoir lancé un job, le dossier /var/lib/hudson/.m2/ reste désespérément vide ...
@NBO: