【问题标题】:Maven profile - How to run plugin once for parent and more for modules?Maven 配置文件 - 如何为父级和模块运行一次插件?
【发布时间】:2016-03-25 08:15:07
【问题描述】:

我对我的 Jenkins 输出有点困惑。

在 Jenkins 上的工作:(在底部缩短 pom.xml)

mvn deploy -Pprofile1

我所有的插件都会运行 4 次:

  • 父/pom.xml
  • 父/module1/pom.xml
  • 父/module2/pom.xml
  • 父/module3/pom.xml

我需要:

  • first-maven-plugin:只在父 pom.xml 中运行 一次
  • second-maven-plugin:为 every pom.xml 运行

为什么:

  • first-maven-plugin:将在阶段运行:初始化 --> 相当长的清理操作。不要这样 4 次
  • second-maven-plugin:将在 phase:package 中运行 --> 所有 pom 都需要。

父 pom.xml

<project ...>

    <groupId>com.test.parent</groupId>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>parent</name>

    <modules>
        <module>module1</module>
        <module>module2</module>
        <module>module3</module>
    </modules>

    <profiles>
        <profile>
            <id>profile1</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>com.test.plugin</groupId>
                        <artifactId>first-maven-plugin</artifactId>
                        <version>1.0.0-SNAPSHOT</version>
                        <execution>
                            <id>execution1</id>
                            <phase>initialize</phase>
                            <goals>
                                <goal>doit</goal>
                            </goals>
                        </execution>
                    </plugin>
                    <plugin>
                        <groupId>com.test.plugin2</groupId>
                        <artifactId>second-maven-plugin</artifactId>
                        <version>1.0.0-SNAPSHOT</version>
                        <execution>
                            <id>another</id>
                            <phase>package</phase>
                            <goals>
                                <goal>goforit</goal>
                            </goals>
                        </execution>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>

【问题讨论】:

  • 这两个插件有skip选项吗?
  • 插件没有跳过选项。它们是通用的,因为它们可以在多个项目上运行。

标签: maven plugins module profile


【解决方案1】:

您可以在第一个插件配置中使用&lt;inherited&gt;false&lt;/inherited&gt;。所以它只会在父 pom 执行中运行。

<build>
    <plugins>
        <plugin>
            <groupId>com.test.plugin</groupId>
            <artifactId>first-maven-plugin</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <inherited>false</inherited>
            <execution>
                <id>execution1</id>
                <phase>initialize</phase>
                <goals>
                    <goal>doit</goal>
                </goals>
            </execution>
        </plugin>
        <plugin>
            <groupId>com.test.plugin2</groupId>
            <artifactId>second-maven-plugin</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <execution>
                <id>another</id>
                <phase>package</phase>
                <goals>
                    <goal>goforit</goal>
                </goals>
            </execution>
        </plugin>
    </plugins>
</build>

https://stackoverflow.com/a/1671175

【讨论】:

  • 为什么这不是批准的答案?
【解决方案2】:

首先,如果您在父代中定义了一个插件,该插件继承给所有子代,而不是完全按照您的意愿行事,这意味着它在每个 pom 上执行(或者换句话说,在每个模块上都无关紧要,如果它是孩子与否)。

您的插件的问题在于它们处理的用例不是很好。因为你说第一个first-maven-plugin 应该只在根级别运行(除此之外我不明白你所说的清理操作是什么意思......删除目标文件夹?)

第二个插件second-maven-plugin 应该为所有 pom 运行?哪个不是很准确的原因,你的意思是 pom 所有具有包装 pom 的子模块?但我假设你的意思是所有有包装的孩子jar

除上述之外,我不确定您的个人资料的使用是否仅基于缺乏正确处理用例。

以上结果我认为您需要更改插件的实现。

如果您希望插件仅在这种多模块结构的根级别上运行,则可以在您的插件中以非常简单的方式进行处理,如下所示:

public void execute()
    throws MojoExecutionException, MojoFailureException
{

    if (mavenProject.isExecutionRoot()) {

    } else {

    }

 ..

通过使用上述方法,您的插件可以决定它是否在根级别上运行。

所以您的first-maven-plugin 可以使用以下内容:

public void execute()
    throws MojoExecutionException, MojoFailureException
{

    if (!mavenProject.isExecutionRoot()) {
       getLog().info("Not running at root level");
       return;  
    } 
    // here the time consuming operations        
 ..

而你的second-maven-plugin 则相反:

public void execute()
    throws MojoExecutionException, MojoFailureException
{

    if (mavenProject.isExecutionRoot()) {
       getLog().info("Not running at root level");
       return;  
    } 
    // here the operation on the childs.
 ..

可以通过以下方式改进该行为:

public void execute()
    throws MojoExecutionException, MojoFailureException
{

    if (!mavenProject.isExecutionRoot()) {
       getLog().debug("Not running at root level");
       return;  
    } 

    if ("pom".equals(project.getPackaging())) {
        getLog().debug("Ignoring pom packaging.");
        return;
    }
    // ..now the operations you would like to do...

因此,如果您有多个级别的模块层次结构,您可以忽略 pom 包装部件或其他部件等。

最后但并非最不重要。你的插件存档什么?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多