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 {}

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)