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!?
6 Answers
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>
-
1Does eclipse really recognize such a folder as one of the
buildpath
s for anyone? 'cos it doesn't for me. FWIW, I usem2e
to import the project. Oct 30, 2014 at 13:21 -
Great answer, just to clarify, you should replace your maven-compiler-plugin with the lines from above...– ColinNov 4, 2014 at 16:11
-
-
1Eclipse asked me to install a plugin for m2e, the build helper connector. I did it, and the lifecycle error is gone. Feb 1, 2016 at 16:42
-
1
Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:build-helper-maven-plugin:1.7:add-source (execution: add-source, phase: generate-sources)
getting this error– KatieSep 20, 2016 at 18:00
Update the pom.xml
as -
<project>
....
<build>
<resources>
<resource>
<directory>src/main/config</directory>
</resource>
</resources>
...
</build>
...
</project>
-
2
-
1Seems you do need to remove the ** exclusion for the added folder on the Eclipse build path though :(– DormouseJan 4, 2014 at 20:37
-
-
2Does not work with IntelliJ, it will see
<resource>
as "Resource" folder but not as "Source" folder, e.g. it won't compile Java source files inside them. Dec 20, 2014 at 1:42 -
9This solution will just move the java source files to the target/classes directory and will not compile the sources Feb 5, 2015 at 11:14
http://maven.apache.org/guides/mini/guide-using-one-source-directory.html
<build>
<sourceDirectory>../src/main/java</sourceDirectory>
also see
-
11This overrides the default src/main/java directory. If you want multiple directories, you have to specify the default one as well.– NatixDec 9, 2015 at 14:16
-
6The question is how to add multiple source directories and you are proposing a method to add only one source directory, which is the opposite of what is asked. Oct 3, 2016 at 13:56
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
.
To mark a folder as generated sources, AND have it picked up by IntelliJ, use the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<generatedSourcesDirectory>src/main/generated</generatedSourcesDirectory>
</configuration>
</plugin>
I spent an hour searching on how to avoid IntelliJ reverting after I manually marked target/generated-sources as a generated sources folder. The codehaus.mojo plugin didn't work. But this solution did!
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
-
2Why the downvotes people? Basically the same answer as saiky0's (which has upvotes) but earlier?– FrisoJan 26, 2015 at 10:24
-
16@Friso because it's not correct. Adding a resource directory will add resources (files copied to
target/classes
, but not compiled). This question is about adding a source directory, which holds files that will be compiled intotarget/classes
, not copied. May 26, 2015 at 20:39
src/{main,test}/
as told here