在构建期间忽略 Maven 插件

why*_*ite 1 maven-plugin maven

如何在构建或部署期间忽略我的插件 ( maven-antrun-plugin)?

我正在从 IDL 工具(用 C 编写)生成源文件。我使用maven-antrun-plugin来进行源生成并将其应用到generate-sources阶段。与 一起build-helper-maven-plugin,IDL 工具生成的 java 源代码存放在生成的源文件夹中,并最终作为源代码包含在 jar 包中。完美的!

这是我在 Maven 构建中使用的内容:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution>
                    <id>generate-sources</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <tasks>
                            <mkdir dir="target/generated-sources/" />
                            <!-- other task stuff here -->
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>target/generated-sources/gen-java</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

然而,现在的问题是我们的团队被指示在我们的持续集成 (CI) 环境中不使用 IDL 工具;由于未安装 IDL 工具,插件将在 CI 中失败。因此,我必须将生成的源代码(以及 src/main/java 下的其他代码)签入我们的 GIT 存储库。

我希望仍然能够运行maven-antrun-plugin,但是,将它与generated-sources阶段或任何生命周期分离,以便 CI 环境可以运行我的构建。当我进行更改时,我将手动/本地运行插件,以便生成源代码,然后将其签入 GIT 存储库。

这甚至可能吗?如何在构建或部署期间忽略 maven-antrun-plugin

why*_*ite 5

正如评论者 (Jarrod Roberson) 所建议的,maven配置文件可用于忽略maven-antrun-plugin插件中源的生成。

具体来说,添加profiles如下所示的块解决了这个问题。该default-profile配置文件默认激活; 它不包含maven-antrun-plugin块。但是,generate-source-profile确实包含maven-antrun-plugin将启动源生成的 。

<profiles>

    <profile>
        <id>default-profile</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

    <profile>
        <id>generate-sources-profile</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>generate-sources</id>
                            <phase>generate-sources</phase>
                            <configuration>
                                <tasks>
                                    <mkdir dir="target/generated-sources/" />
                                    <!-- other task stuff here -->
                                </tasks>
                            </configuration>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>

</profiles>
Run Code Online (Sandbox Code Playgroud)

因此,持续集成 (CI) 工具将执行正常的构建和部署 (mvn deploy)。当我想生成源时,我将通过执行以下操作来运行生成源配置文件,例如mvn clean install -P generate-sources-profile.