Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wiping out Maven local repository on build machine

On a CI build server, the local Maven repository fills up the file system repetitively (after a few days). What strategy are others doing to trim the local repository in such a case? -Max

like image 411
Max Spring Avatar asked Aug 20 '09 19:08

Max Spring


People also ask

Can I delete Maven local repository?

To clear/delete your local maven repository cache, simply delete the . m2/repository folder. The local repository path can also be configured in Maven setting. xml (either the global or the user one).

How do I delete a local .m2 repository?

Just open eclipse-->window-->preferences-->maven-->user settings-->in there see "local repository location". then find the location for your . m2/repository folder then delete that folder if your maven doesn't work properly.

What is Maven clean command?

mvn clean: Cleans the project and removes all files generated by the previous build. mvn compile: Compiles source code of the project. mvn test-compile: Compiles the test source code.

Where is Maven local cache?

The first place that Maven looks for artifacts is in the local repository, which is the local cache where Maven stores all of the artifacts it has downloaded or found elsewhere. The default location of the local repository is the . m2/repository/ directory under the user's home directory.


2 Answers

The Maven dependency plugin has a purge-local-repository goal that allows you to delete the dependencies for a given project from the local repository, if this is run say once a day on each project the snapshots will not accumulate.


Alternatively there's a more scorched-earth approach you could take. As the problem is typically the timestamped snapshot artifacts, you could use the maven-antrun-plugin to delete all files that match the resource collection pattern.

For example (note this might need some tweaking as I've done it from memory):

<plugin>   <artifactId>maven-antrun-plugin</artifactId>   <executions>     <execution>       <phase>package</phase>       <configuration>         <tasks>           <delete>             <fileset dir="${settings.localRepository}">               <include name="**/*.jar"/>               <exclude name="**/*.pom"/>               <exclude name="**/*.war"/>               <exclude name="**/*.ear"/>               <exclude name="**/*.md5"/>               <exclude name="**/*.sha"/>               <!--any other extensions?...-->               <!--match the timestamp pattern-->               <containsregexp expression="[0-9]{8}.[0-9]{6}-[0-9]+"/>             </fileset>           </delete>         </tasks>       </configuration>       <goals>         <goal>run</goal>       </goals>     </execution>   </executions> </plugin> 
like image 194
Rich Seller Avatar answered Sep 22 '22 05:09

Rich Seller


If you're using hudson, you can set up a scheduled job to just delete the entire repository once a day or something like that. I've got a job called hudson-maven-repo-clean which has this configuration:

  • Build / Execute shell: rm -rf ~hudson/.m2/repository
  • Build Triggers / Build periodically: 0 0 * * *
like image 24
Dominic Mitchell Avatar answered Sep 18 '22 05:09

Dominic Mitchell