有没有一种方法可以在一个maven项目中编译多个java源目录?


当前回答

你可以用build-helper添加一个新的源目录:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/main/generated</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

其他回答

这可以分两步完成:

对于每个源目录,您应该创建自己的模块。 在所有模块中,您应该指定相同的构建目录:${build.directory}

如果你使用已启动的Jetty (Jetty:run),那么重新编译任何模块中的任何类(使用Maven、IDEA或Eclipse)都会导致Jetty重新启动。对于修改后的资源,您将获得相同的行为。

我很天真地这样做:

<build>
  <finalName>osmwse</finalName>
  <sourceDirectory>src/main/java, src/interfaces, src/services</sourceDirectory>
</build>

这适用于maven 3.5.4,现在Intellij Idea将此代码视为源代码:

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <version>3.3</version>
     <configuration>
         <generatedSourcesDirectory>src/main/generated</generatedSourcesDirectory>                    
     </configuration>
</plugin>

为了让它在intelliJ中工作,你还可以通过这种方式将generatedSourcesDirectory添加到编译器插件:

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

虽然来自evokk的答案基本上是正确的,但它缺少测试类。 你必须使用goal add-test-source添加测试类:

<execution>
    <phase>generate-sources</phase>
    <goals>
        <goal>add-test-source</goal>
    </goals>
    <configuration>
       <sources>
            <source>target/generated/some-test-classes</source>
        </sources>
    </configuration>
</execution>