How to add an extra source directory for maven to compile and include in the build jar?

JavaMavenMaven 2

Java Problem Overview


In addition to the src/main/java, I am adding a src/bootstrap directory that I want to include in my build process, in other words, I want maven to compile and include the sources there in my build. How!?

Java Solutions


Solution 1 - Java

You can use the Build Helper Plugin, e.g:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>some directory</source>
                ...
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Solution 2 - Java

NOTE: This solution will just move the java source files to the target/classes directory and will not compile the sources.

Update the pom.xml as -
<project>	
 ....
    <build>
	  <resources>
	    <resource>
		  <directory>src/main/config</directory>
		</resource>
	  </resources>
     ...
    </build>
...
</project>

Solution 3 - Java

Solution 4 - Java

With recent Maven versions (3) and recent version of the maven compiler plugin (3.7.0), I notice that adding a source folder with the build-helper-maven-plugin is not required if the folder that contains the source code to add in the build is located in the target folder or a subfolder of it.
It seems that the compiler maven plugin compiles any java source code located inside this folder whatever the directory that contains them.
For example having some (generated or no) source code in target/a, target/generated-source/foo will be compiled and added in the outputDirectory : target/classes.

Solution 5 - Java

You can add the directories for your build process like:

    ...
   <resources>
     <resource>
       <directory>src/bootstrap</directory>
     </resource>
   </resources>
   ...

The src/main/java is the default path which is not needed to be mentioned in the pom.xml

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionchrisapotekView Question on Stackoverflow
Solution 1 - JavaPéter TörökView Answer on Stackoverflow
Solution 2 - JavaSaikatView Answer on Stackoverflow
Solution 3 - JavaKalpesh SoniView Answer on Stackoverflow
Solution 4 - JavadavidxxxView Answer on Stackoverflow
Solution 5 - JavaArunView Answer on Stackoverflow