Maven 配置文件 - 如何 运行 插件一次用于 parent 和更多模块?

Maven profile - How to run plugin once for parent and more for modules?

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

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

mvn deploy -Pprofile1

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

我需要:

原因:

Parent 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>

首先,如果你在 parent 中定义了一个插件,它被所有 children 继承,而不是它完全按照你希望的方式运行,这意味着它在每个 pom 上执行(或者换句话说每个模块上是否为 child 都没有关系。

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

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

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

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

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

public void execute()
    throws MojoExecutionException, MojoFailureException
{

    if (mavenProject.isExecutionRoot()) {

    } else {

    }

 ..

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

因此您的 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 包装部分或其他部分等。

最后但并非最不重要的一点。你的插件归档了什么?

您可以在第一个插件配置中使用<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>