Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling only selected files in Maven

Tags:

java

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?

like image 747
Michał Kalinowski Avatar asked Jun 15 '12 13:06

Michał Kalinowski


2 Answers

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?

like image 89
Ilya Avatar answered Oct 04 '22 10:10

Ilya


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/* 
like image 39
Mohd Waseem Avatar answered Oct 04 '22 08:10

Mohd Waseem