Compiling only selected files in Maven

I have faced a similar situation. We needed to hot swap only modified files to our remote docker container in order to improve changes-deploy time. This is how we solved it.

Add includes option in build plugin with command line argument. Note since we wanted to add multiple files, so we have used includes and not include

         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <compilerVersion>1.8</compilerVersion>
                <source>1.8</source>
                <target>1.8</target>
                <includes>${changed.classes}</includes>
            </configuration>
        </plugin>

Now run compile phase with argument, example:

mvn compile -Dchanged.classes=com/demo/ClassA.java,com/demo/ClassB.java,com/demo2/*

maven-compiler-plugin using Ant-like inclusion/exclusion notation. You can see examples in Ant documentation Ant FileSet Type

If you are want include only files from one directory, you need write it like you did:

<include>com/example/dao/bean/*.java</include>

To include also subdirectories from path, it will be:

<include>com/example/dao/bean/**/*.java</include>

Simple app with 3 classes.

com/company/Obj1.java
com/company/Obj2.java
com/company/inner/Obj3.java  

build in pom.xml

<build>
         <plugins>
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-compiler-plugin</artifactId>
               <version>2.0.2</version>
               <configuration>
                  <source>1.6</source>
                  <target>1.6</target>
                  <includes>
                     <include>com/company/inner/*.java</include>
                  </includes>
               </configuration>
            </plugin>
          </plugins>

   </build>  

result: 1 class is compiled.
And any combination of includes is working well
or you mean something else?

Tags:

Java

Maven