119

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!?

chrisapotek
  • 6,007
  • 14
  • 51
  • 85
  • Simple question: Why not moving the source code to src/main/java ? Or create a separate maven project/module which contains the code and define it as dependency. – khmarbaise Mar 17 '12 at 21:14
  • 7
    In my case I need to add a generated sources folder, which I prefer to reside inside target. – djjeck Jul 10 '13 at 21:02
  • Alternatively, one might want to place *integration-tests* in directories other than `src/{main,test}/` as told [here](https://dzone.com/articles/integration-tests-with-maven) – y2k-shubham Aug 21 '18 at 07:57
  • This has also been discussed here: https://stackoverflow.com/q/270445/1061929 – bjmi Feb 09 '20 at 09:38

6 Answers6

171

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>
t0r0X
  • 4,212
  • 1
  • 38
  • 34
Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • 1
    Does eclipse really recognize such a folder as one of the `buildpath`s for anyone? 'cos it doesn't for me. FWIW, I use `m2e` to import the project. – mystarrocks Oct 30 '14 at 13:21
  • Great answer, just to clarify, you should replace your maven-compiler-plugin with the lines from above... – Colin Nov 04 '14 at 16:11
  • Works in IntelliJ as well. – Alexander Klimetschek Dec 20 '14 at 01:45
  • So if (in Eclipse Luna) i added (linked) in my BuildPath (clicking the right mouse button on my MavenProject) a source folder where its directory is "/Users/MyName/Desktop/source", i have to write `/Users/MyName/Desktop/source` ?? And everything goes right? – Aerox Apr 29 '15 at 18:07
  • I don't know why, but i simply removed the linked source and re-added on the build path and everything has gone right. If I have any other problem I will post that here. Thanks anyway :-) – Aerox May 03 '15 at 22:08
  • The question is about Maven, which has no knowledge of (nor should it) of the IDE (e.g., eclipse). Builds should work on Jenkins or whatever build server without any IDE-specific references. The IDE must invoke Maven (build tool) and thus IDE-related questions should probably be taken out separately. – Darrell Teague Jan 21 '16 at 19:02
  • 1
    Eclipse asked me to install a plugin for m2e, the build helper connector. I did it, and the lifecycle error is gone. – Alexis Dufrenoy Feb 01 '16 at 16:42
  • This requires me to move to java 7, this solution does not work for java 6 ? Or am I missing something ? – MithunS Feb 24 '16 at 22:59
  • is there a solution without having to install a plugin ? – JavaDev Jun 30 '16 at 12:24
  • 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 – Katie Sep 20 '16 at 18:00
  • Can someone explain to me how this is better than recursive make? xD – Alex Reinking Oct 07 '16 at 01:30
36
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>
Saikat
  • 14,222
  • 20
  • 104
  • 125
17

http://maven.apache.org/guides/mini/guide-using-one-source-directory.html

<build>
<sourceDirectory>../src/main/java</sourceDirectory>

also see

Maven compile with multiple src directories

Community
  • 1
  • 1
Kalpesh Soni
  • 6,879
  • 2
  • 56
  • 59
  • 12
    This overrides the default src/main/java directory. If you want multiple directories, you have to specify the default one as well. – Natix Dec 09 '15 at 14:16
  • 6
    The 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. – João Matos Oct 03 '16 at 13:56
2

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!

Ahmed Tawfik
  • 1,159
  • 1
  • 8
  • 13
  • 1
    This is exactly what was required for me. I am working with protobuf and it defaults its compiled code in generatedSource dir. For some reason intellij, doesnt implicitly pick the class def files for me to use them in core app class files. I had to give the above plugin to make it look into the target folder. Thanks for this solution. It made a quick work. – Sunil K-Standard Chartered Mar 19 '23 at 16:33
  • 1
    Seems to be the most correct solution. In my case `generatedSourcesDirectory` is `target/generated-sources`. – Andrey Aug 15 '23 at 11:58
1

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.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
-4

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

Arun
  • 2,562
  • 6
  • 25
  • 43
  • 2
    Why the downvotes people? Basically the same answer as saiky0's (which has upvotes) but earlier? – Friso Jan 26 '15 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* into `target/classes`, not copied. – Darth Android May 26 '15 at 20:39