Maven building only changed files

If you only call "mvn install" without "clean", the compiler plugin will compile only modified classes.


For GIT

mvn install -amd -pl $(git status | grep -E "modified:|deleted:|added:" | awk '{print $2}' | cut -f1 -d"/")

OR

In your .bashrc file (.bashrc can be found in home directory ~/.bashrc , or create it if doesn't exists) add the following function.

mvn_changed_modules(){
    [ -z "$1" ] && echo "Expected command : mvn_changed_modules (install/build/clean or any maven command)" && exit 0

        modules=$(git status | grep -E "modified:|deleted:|added:" | awk '{print $2}' | cut -f1 -d"/")

                if [  -z "$modules" ];
                then
                        echo "No changes (modified / deleted / added)  found"
                else
                        echo -e "Changed modules are : `echo $modules`\n\n"
                        mvn $1 -amd -pl $modules
                fi
}

**Then after re-starting your bash** (command prompt), you **can just use the following command** from the ROOT directory itself.

smilyface@machine>ProjectRootDir]$ mvn_changed_module install

How it works
As per the question mvn install -amd -pl services is the command when "some changes done in services module". So, first get module name from the changed file(s) and put it as input for mvn-install command

Say for example, below is a list of modified files (output of git status) -
services/pom.xml
services/ReadMe.txt
web/src/java/com/some/Name.java
Then services and web are the modules name which need to be build / compile / install


Within a multi-module build you can use:

mvn -pl ChangedModule compile

from the root module will compile only the given ChangedModule. The compiler plugin will only compile the files which have been changed. But it can happen that the module you have changed would cause a recompile of other module which are depending on the ChangedModule. This can be achieved by using the following:

mvn -amd -pl ChangedModule compile

where the -amd means also make dependents. This will work without installing the whole modules into the local repository by a mvn install.