Explain Codes LogoExplain Codes Logo

How is "mvn clean install" different from "mvn install"?

java
mvn-clean-install
maven-lifecycle
build-process
Alex KataevbyAlex Kataev·Dec 22, 2024
TLDR

Just as you shower and dress to make a fresh start each day, mvn clean install is your build's chance to refresh and dress itself in the newest, cleanest code. It deletes the target directory to ensure there's no leftover mess from yesterday's pizza party (your previous build).

On the other hand, mvn install is your build's lazy Sunday when it decides to skip the shower and use yesterday's code if it exists. It's fast and does the job, but might be a bit smelly if there were a lot of changes since yesterday.

mvn clean install    // Shower, dress, and hit the town!
mvn install          // Snooze alarm, yawn, and keep working in your pj's

When to use each command

Fresh build with "mvn clean install"

Consider this as your build's new day, new me affirmation. Use it when:

  • You've made big changes to your project structure, like adding or removing modules.
  • You have new dependencies that could clash with the old, compiled classes.
  • Your previous build failed and left a mess that might spoil the new one.

Speedy build with "mvn install"

Imagine your build just got up and is rushing for the bus. Use it when:

  • You made a small change and want a quick feedback loop.
  • You need to save time for testing or deploying.
  • You know the residual artifacts are not going to stink up the new build.

Dangers lurking behind each command

There's a right time and place for using each command. But beware of these potential issues:

  • If your build seems to be acting out, running mvn install might be dragging some old grudge. Try mvn clean install to have a heart-to-heart and start from a clean slate.
  • Careful not to lose your favorite shirt. The clean command will dutifully clean out the target directory, even if there was something you wanted to keep. Backup crucial stuff.
  • For optimized builds in large projects, consider cleanup only certain modules and not the entire project directly.

Mastering the Maven Lifecycle

Maven lifecycle basics and build reproducibility with mvn clean install

  • Think of Maven lifecycle as a detailed recipe. Each goal in the command is an instruction, clean and install are steps in the process.
  • mvn clean install is akin to following the recipe to the exact measure. It helps achieve consistent and reproducible builds. No more "but it works on my computer!" surprise parties.
  • Embrace it in your CI/CD setups. Configure Jenkins, GitHub actions to run mvn clean install and ensure you're always deploying squeaky clean builds.

Supercharging the build process with phases and goals

Strategic use of plugin goals and optimizing build performance

  • Develop your own custom clean and build steps by binding specific plugin goals to the clean or install phases. Think of it as using your favorite seasoning in the recipe to make it your own.
  • When speed is essence, configure conditional cleaning steps using Maven profiles. It's the build equivalent of cleaning your kitchen only when you're expecting guests.
  • For large projects, consider using mvn clean sparingly and relying on Maven's smart incremental build feature or <dependencyManagement> block. It's knowing when you can get away with just brushing your hair and when you need a full shower.