Maven - How to include single java files as sources/depedencies

Viewed 525

I have created a multi-module project in maven which is as follows:

root/pom.xml
|________client/pom.xml
         |________/src/main/java
         |________/src/main/resources
|________common/pom.xml
         |________/src/main/java
|________tools/pom.xml
         |________/src/main/java
|________server/pom.xml
         |________/src/main/java
         |________/src/main/resources

I would like to compile the "client" module code which depends on ALL java classes in "common" module but on SOME java classes in the "tools" module. Using the build-helper-maven-plugin as below, I was able to add all java source files under common/ path, but I need a way to define individual java files as sources which are under tools/.

<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
    <version>3.2.0</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>../common/src/main/java</source>
                    <!-- Adding single java files as below does not work -->
                    <source>../tools/log/Log.java</source>
                    <source>../tools/socket/SocketClient.java</source>
                    <!----------------------------------------------------->
                </sources>
            </configuration>
       </execution>
    </executions>
3 Answers

You should include module "common" and "tools" as dependency in "client" Then you can run build in the root of project: mvn clean install - it will build all modules.

Don't try to use parts of a module as dependency. Maven does not support this and it results in brittle and hard to maintain constructions.

If you only need parts of tools, then this is a clear signal that tools is too large and you need to split it up into two modules.

Ok, based on the comments received it seems that the ideal solution is to break the modules into sub-modules and use those as dependencies. However, as this is currently a time consuming task, I am posting a quick and dirty solution on how you can include one or more java files into your build by copying the files under a new source directory, src/main/external, and then add this directory as part of your sources.

<build>
    <plugins>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
               <execution>
                  <id>copy-source-java-files</id>
                  <phase>generate-resources</phase>
                  <goals>
                     <goal>copy-resources</goal>
                  </goals>
                  <configuration>
                     <outputDirectory>src/main/external</outputDirectory>
                     <overwrite>true</overwrite>
                     <resources>
                        <resource>
                           <!-- External source directory -->
                           <directory>../tools/src/main/java/</directory>
                           <includes>
                              <!-- Files to be copied along with their directory paths -->
                              <include>tools/log/Log.java</include>
                              <include>tools/socket/SocketClient.java</include>
                              <include>tools/client/reader/**</include>
                           </includes>
                        </resource>
                     </resources>
                  </configuration>
               </execution>
            </executions>
         </plugin>

         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/main/java</source>
                            <source>src/main/resources</source>
                            <source>../common/src/main/java</source>
                            <!-- Here we add the source with the files copied -->
                            <source>src/main/external</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
     </plugins>
  </build>

The output after compilation would be as follows:

root/pom.xml
|________client/pom.xml
         |________/src/main/java
         |________/src/main/resources
         |________/src/main/external  <----- This contains all external java files.
Related