Maven Project with Multiple Source Directories – 具有多个源代码目录的Maven项目

最后修改: 2018年 8月 13日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

We sometimes need to use multiple source directories in a Java project. A common case example when there are classes that are generated automatically and placed in a different directory.

我们有时需要在一个Java项目中使用多个源代码目录。一个常见的例子是,当有自动生成的类被放置在不同的目录中。

In this short article, we’ll show how to set up Maven to work with additional source directories.

在这篇短文中,我们将介绍如何设置Maven以使用其他源码目录

2. Adding Another Source Directory

2.添加另一个来源目录

Assuming we have a Maven project already created, let’s add a new source directory called another-src in the src/main folder.

假设我们已经创建了一个Maven项目,让我们在src/main文件夹下添加一个名为another-src的新源代码目录。

After that, let’s create a simple Java class inside this folder:

之后,让我们在这个文件夹中创建一个简单的Java类。

public class Foo {
    public static String foo() {
        return "foo";
    }
}

Let’s now create another class in our src/main/java directory that uses the Foo class we’ve just created:

现在让我们在src/main/java目录下创建另一个类,它使用我们刚刚创建的Foo类。

public class MultipleSrcFolders {
    public static void callFoo() {
        Foo.foo();
    }    
}

Our project structure now looks like this:

我们的项目结构现在看起来像这样。

maven

If we try to compile this project with Maven we get a compilation error because the Foo class is not included in the project:

如果我们尝试用Maven编译这个项目,会得到一个编译错误,因为项目中没有包含Foo类。

[ERROR] .../MultipleSrcFolders.java:[6,9] cannot find symbol
[ERROR]   symbol:   variable Foo
[ERROR]   location: class com.baeldung.maven.plugins.MultipleSrcFolders

3. Using the Builder Helper Plugin

3.使用Builder Helper Plugin

With Maven, we can use the Builder Helper plugin to add more source directories. This plugin lets us customize the build lifecycle in different ways.

利用Maven,我们可以使用Builder Helper插件来添加更多的源代码目录。这个插件可以让我们以不同方式定制构建生命周期。

One of its goals is the add-sources, which is intended to add more src directories to the project during the generate-sources phase.

其目标之一是add-sources,目的是在generate-sources阶段向项目添加更多src目录。

We can use it in our project by adding it to our pom.xml:

我们可以通过将其添加到我们的pom.xml中,在我们的项目中使用它。

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

The latest version of the plugin can be found in Maven Central.

该插件的最新版本可在Maven中心找到。

If we compile now our project, the build succeeds.

如果我们现在编译我们的项目,构建成功了。

4. Conclusion

4.结论

We’ve seen in this article how to set up the Builder Helper Maven plugin to work with multiple src directories in a Maven project.

我们在本文中看到了如何设置Builder Helper Maven插件,以便在一个Maven项目中使用多个src目录。

As always, the full source code of the examples is available over on GitHub.

一如既往,这些示例的完整源代码可在GitHub上获得