In which circumstances is it safe respectively unsafe to rm -rf ~/.m2/repository provided that I\'m working online and I have access to all needed artifacts via
When you retrieve all dependencies from remote repositories, then the local repo becomes nothing more than a cache.
Like all caches, the Maven local repo can occasionally become "dirty". In an acknowledged act of paranoia and overkill, I schedule a periodic cron job to purge the local repositories on my build machines. This forces a re-sync with my Nexus Maven repository.
What you could do is add a cron entry to regularly delete files in the local repository for a given period, say 90 days.
find ~/.m2/ -type f -atime +90 -delete && \
find ~/.m2/ -type d -empty -delete
This keeps your cache size down to only artifacts you've been using recently.
If you have access to all needed artifacts via remote repos, it is always safe to remove local Maven repository. All artifacts needed to the next build, including even basic Maven plugins (like compiler) will be basically fetched from remote repos. That's it.
So instead of doing the "brute force" way, there is a different option:
brute force:
rm -rf ~/.m2/repository
Here is the "mvn" way to do this.
"everything:
mvn dependency:purge-local-repository
some arguments that I find helpful
mvn dependency:purge-local-repository -DactTransitively=false -DreResolve=false
Here is what I typically do for my local machine:
mvn dependency:purge-local-repository -DmanualInclude="my.group.id" -DsnapshotsOnly=true -DactTransitively=false -DreResolve=false
you can investigate the command line options here:
https://maven.apache.org/plugins/maven-dependency-plugin/purge-local-repository-mojo.html