1

我有 pom.xml 的结构如下:

<plugins>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.5</version>
    <configuration>
        <includes>
                 ... some directories with .java files to compile.

...

<plugin>  --- some my plugin that generates one more dir with .java files.

此时我想编译新生成的文件,所以我在这里重复第 1 步,使用不同的“包含”元素内容。第二次编译根本不会发生。请指教。

4

1 回答 1

1

您的插件应该分阶段运行generate-sources。然后您必须确保生成的源在正常compile阶段可用。

您的插件应配置如下:

<build>
    <plugins>
        <plugin>
            <groupId>your.group</groupId>
            <artifactId>your-generator-plugin</artifactId>
            <version>your-generator-version</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>your-generator-goal</goal>
                    </goals>
                    <configuration>
                        <!-- Here goes all the plugin configuration -->
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

你可以直接从你的插件(mojo)中添加编译源路径来使用。

以下是您必须添加到 mojo 中的示例:

/**
 * The current project representation.
 * @parameter expression="${project}"
 * @required
 * @readonly
 */
private MavenProject project;

/**
 * Directory wherein generated source will be put; main, test, site, ... will be added implictly.
 * @parameter expression="${outputDir}" default-value="${project.build.directory}/src-generated"
 * @required
 */
private File outputDir;

并且必须将这样的内容添加到您的execute()方法中:

if (!settings.isInteractiveMode()) {
    LOG.info("Adding " + outputDir.getAbsolutePath() + " to compile source root");
}
project.addCompileSourceRoot(outputDir.getAbsolutePath());

This will output the generated java source files into target/src-generated/ but that can either be changed to some other default value in your mojo OR by adding this to the configuration part of the plugin:

<outputDir>path/to/my/generated/source/</outputDir>

The generated java source files will automatically be included in the compile phase.

于 2012-10-26T08:33:20.230 回答