How to clean all Maven projects

My “dev” folder usually contains dozens of current projects and I don’t clean them regularly. But when I backup my directory, it’s needless to archive the target folders because they can be regenerated at any time. So mvn clean needs to be called on every project, this is how I automatize it with Bash:

find . -name "target" -type d \
  | sed s/target/pom.xml/ \
  | tee /dev/stderr \
  | xargs -I {} mvn -q clean -f {}
  • find lists all target folders,
  • with sed I transform the path to the POM’s path,
  • tee just prints out the path to inform me about which project is being cleaned,
  • and finally xargs passes the path as argument to the Maven command.

Since find walks the directory tree recursively, I can call the above script from the root of my “dev” folder or even from the root of my drive, and all of my projects will be cleaned. 😎

(Developing on Windows? Git for Windows contains a Bash terminal or you can use the Linux subsystem)

I'm Zsolt, a full-stack developer.
I develop crawlers, web applications, static websites and interactive visualizations.
Copyright © 2020 Zsolt Jurányi | All rights reserved.