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.

この時点で、新しく生成されたファイルをコンパイルしたいので、「includes」要素の内容を変えてステップ 1 を繰り返します。2 番目のコンパイルはまったく行われません。ご意見をお聞かせください。

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) から直接、使用するコンパイル ソース パスを追加できます。

モジョに追加する必要があるものの例を次に示します。

/**
 * 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());

これにより、生成されたJavaソースファイルが出力さtarget/src-generated/ますが、mojoで他のデフォルト値に変更するか、これをプラグインの構成部分に追加することで変更できます:

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

compile生成された Java ソース ファイルは、フェーズに自動的に含まれます。

于 2012-10-26T08:33:20.230 に答える