Maven building only changed files

前端 未结 7 963
Happy的楠姐
Happy的楠姐 2020-12-13 01:59

Lets say i have module structure like below

     Modules
       ->utils
       ->domain
       ->client
       ->services
       ->deploy (th         


        
相关标签:
7条回答
  • 2020-12-13 02:37

    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.

    0 讨论(0)
  • 2020-12-13 02:38

    If you are using SVN and *nix, from the root module

    mvn install -amd -pl $(svn st | colrm 1 8 | sed 's /.*  ' | xargs echo | sed 's- -,:-g' | sed 's ^ : ') 
    
    0 讨论(0)
  • 2020-12-13 02:38

    The question and the answers posted so far do not take the dependency tree into account. What if the utils module is changed? We need to rebuild (retest at least) it and all the modules depending on it.

    Ways to do so:

    • https://github.com/avodonosov/hashver-maven-plugin/
    • https://github.com/vackosar/gitflow-incremental-builder/
    • Gradle Enterprise is a commercial service which provides build cache, in particular for maven
    • Migrate to newer build tools like Gradle or Bazel which support build caches out of box.
    0 讨论(0)
  • 2020-12-13 02:50

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

    0 讨论(0)
  • 2020-12-13 02:53

    After trying and using aforementioned advises, I've met following problems:

    1. Maven install (without clean) still takes a lot of time, which for several projects can be 10-20s extra time.
    2. Sebasjm's solution is fast and useful (I was using it for a couple of months), but if you have several changed projects, rebuilding them all the time (if you even hadn't change anything) is a huge waste of time

    What really worked for me is comparing source modification dates against .jar modification in local repository. And if you check only for VCS changed files (see sebasjm's answer), then date comparison won't take noticeable time (for me it was less than 1s for 100 changed files). Main benefit of such approach is very accurate rebuild of only really changed projects. Main problem is doing modification date comparison is a bit more than one-liner script.

    For those, who want to try it, but too lazy to write such script themself sharing my version of it: https://github.com/bugy/rebuilder (linux/windows). It can do some additional useful things, but the main idea and central algorithm is as explained above.

    0 讨论(0)
  • 2020-12-13 02:57

    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 "Changed modules are : `echo $modules`"
                            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>MainDir]$ mvn_changed_module install


    How it works?
    As per 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

    0 讨论(0)
提交回复
热议问题