ChatGPT解决这个技术问题 Extra ChatGPT

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

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

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.
In my case I need to add a generated sources folder, which I prefer to reside inside target.
Alternatively, one might want to place integration-tests in directories other than src/{main,test}/ as told here
This has also been discussed here: stackoverflow.com/q/270445/1061929

t
t0r0X

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>

Does eclipse really recognize such a folder as one of the buildpaths for anyone? 'cos it doesn't for me. FWIW, I use m2e to import the project.
Great answer, just to clarify, you should replace your maven-compiler-plugin with the lines from above...
Works in IntelliJ as well.
Eclipse asked me to install a plugin for m2e, the build helper connector. I did it, and the lifecycle error is gone.
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
S
Saikat

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>

NetBeans picked it up like a charm :)
Seems you do need to remove the ** exclusion for the added folder on the Eclipse build path though :(
@Dormouse can you explain please?
Does 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.
This solution will just move the java source files to the target/classes directory and will not compile the sources
C
Community

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


This overrides the default src/main/java directory. If you want multiple directories, you have to specify the default one as well.
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.
d
davidxxx

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.


A
Arun

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


Why the downvotes people? Basically the same answer as saiky0's (which has upvotes) but earlier?
@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.