Michał Kalinowski
Michał Kalinowski

Reputation: 18013

Compiling only selected files in Maven

I want to compile only selected files or directories (including subdirectories) within source directory. I was pretty sure I can do this using <includes> of maven-compiler-plugin's configuration, but it seems to not work as I expect since it still compiles all classes into target/classes. What is really strange, Maven output suggest that the setting actually does its work, because with:

  <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.5.1</version>
    <configuration>
      <includes>
        <include>com/example/dao/bean/*.java</include>
      </includes>
    </configuration>
  </plugin>

I have:

[INFO] Compiling 1 source file to c:\Projects\test\target\classes

but with no compiler's configuration I have:

[INFO] Compiling 14 source file to c:\Projects\test\target\classes

In both cases however, all 14 classes are compiled into target/classes as I mentioned. Can you explain that or suggest another solution to compile only selected files?

Upvotes: 33

Views: 40756

Answers (4)

Mohd Waseem
Mohd Waseem

Reputation: 1374

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/*

Upvotes: 8

m1ld
m1ld

Reputation: 1200

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>

Upvotes: 2

Raghuram
Raghuram

Reputation: 52665

I had no difficulty including or excluding files for compilation with maven compiler plugin 2.5.1. Here is the dummy project I used for the purpose. Perhaps the include pattern that you use is different.

Upvotes: 0

Ilya
Ilya

Reputation: 29711

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?

Upvotes: 41

Related Questions