Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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>
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

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.
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!