How to add an extra source directory for maven to compile and include in the build jar?
JavaMavenMaven 2Java 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.
pom.xml
as -
Update the <project>
....
<build>
<resources>
<resource>
<directory>src/main/config</directory>
</resource>
</resources>
...
</build>
...
</project>
Solution 3 - Java
http://maven.apache.org/guides/mini/guide-using-one-source-directory.html
<build>
<sourceDirectory>../src/main/java</sourceDirectory>
also see
https://stackoverflow.com/questions/270445/maven-compile-with-multiple-src-directories
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