Maven 配置文件 - 如何为父级运行插件一次,为模块运行更多次?

Dim*_*ele 5 profile plugins module maven

我对 Jenkins 的输出有点困惑。

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

mvn deploy -Pprofile1
Run Code Online (Sandbox Code Playgroud)

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

  • 父/pom.xml
  • 父/模块1/pom.xml
  • 父/module2/pom.xml
  • 父/module3/pom.xml

我需要:

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

为什么:

  • first-maven-plugin: 将在阶段运行:初始化 --> 相当长的清理操作。不想这4次
  • second-maven-plugin: 将在 phase:package --> necesaary 中运行所有 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>
Run Code Online (Sandbox Code Playgroud)

jul*_*wki 9

您可以<inherited>false</inherited>在第一个插件配置中使用。所以它只会在父 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>
Run Code Online (Sandbox Code Playgroud)

/sf/answers/116982281/

  • 为什么这不是批准的答案? (2认同)

khm*_*ise 2

首先,如果您在父级中定义一个插件,该插件继承给所有子级,那么它的行为完全符合您的意愿,这意味着它在每个 pom 上执行(或者换句话说,在每个模块上执行,无论它是否是子级) )。

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

第二个插件second-maven-plugin应该为所有 pom 运行?这不是很准确,因为您所说的 pom 是指所有具有包装的子模块吗pom?但我假设你指的是所有有包装的儿童jar

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

根据上述结果,我得出的结论是您需要更改插件的实现。

如果您喜欢让插件仅在此类多模块结构的根级别上运行,则可以在插件中以非常简单的方式处理,如下所示:

public void execute()
    throws MojoExecutionException, MojoFailureException
{

    if (mavenProject.isExecutionRoot()) {

    } else {

    }

 ..
Run Code Online (Sandbox Code Playgroud)

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

所以你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        
 ..
Run Code Online (Sandbox Code Playgroud)

而你却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.
 ..
Run Code Online (Sandbox Code Playgroud)

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

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...
Run Code Online (Sandbox Code Playgroud)

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

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