如何跳过 Maven 插件 parent pom 执行?

How to skip Maven plugin parent pom execution?

我有一个包含多个模块的 parent 聚合器 POM。我在 parent 中有一个 build/pluginManagement,因为每个子模块的插件执行都是相同的。

如果我在每个子模块 (mvn package) 中执行,它工作正常。如果我在parent中执行:mvn package -Pmytest,我想跳过parent模块中的插件执行,所以我添加了:

    <plugin>
        <groupId>org.wildfly.plugins</groupId>
        <artifactId>wildfly-maven-plugin</artifactId>
        <configuration>
            <skip>true</skip>
        </configuration>
    </plugin>

但是,它不起作用。

Parent pom.xml:

<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xyz.jboss.config</groupId>
    <artifactId>jboss-config</artifactId>
    <packaging>pom</packaging>
    <version>1.2-SNAPSHOT</version>
    <name>jboss-config</name>
    <url>http://maven.apache.org</url>

    <profiles>
        <profile>
            <id>mytest</id>
            <modules>
                <module>jboss-system-properties</module>
                <module>jboss-security</module>
            </modules>
        </profile>
    </profiles>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>process-package</id>
                            <phase>package</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${basedir}/processed/scripts</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>${basedir}/src/main/resources/scripts</directory>
                                        <filtering>true</filtering>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                        <execution>
                            <id>process-clean</id>
                            <phase>clean</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${basedir}/processed/scripts</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>${basedir}/src/main/resources/scripts</directory>
                                        <filtering>true</filtering>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.wildfly.plugins</groupId>
                    <artifactId>wildfly-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>add-${project.artifactId}</id>
                            <phase>package</phase>
                            <goals>
                                <goal>execute-commands</goal>
                            </goals>
                            <configuration>
                                <execute-commands>
                                    <scripts>
                                        <script>${basedir}/processed/scripts/add-${project.artifactId}.cli</script>
                                    </scripts>
                                </execute-commands>
                            </configuration>
                        </execution>
                        <execution>
                            <id>remove-${project.artifactId}</id>
                            <phase>clean</phase>
                            <goals>
                                <goal>execute-commands</goal>
                            </goals>
                            <configuration>
                                <execute-commands>
                                    <scripts>
                                        <script>${basedir}/processed/scripts/remove-${project.artifactId}.cli</script>
                                    </scripts>
                                </execute-commands>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.wildfly.plugins</groupId>
                <artifactId>wildfly-maven-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Child pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.xyz.jboss.config</groupId>
        <artifactId>jboss-config</artifactId>
        <version>1.2-SNAPSHOT</version>
    </parent>
    <artifactId>jboss-system-properties</artifactId>
    <packaging>ear</packaging>
    <name>jboss-system-properties</name>
    <url>http://maven.apache.org</url>
</project>

我收到一个找不到文件的错误。该文件不应存在于此处。问题是为什么它不跳过 parent 模块执行。

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] jboss-config ....................................... FAILURE [ 20.238 s]
[INFO] jboss-system-properties ............................ SKIPPED
[INFO] jboss-security ..................................... SKIPPED
[INFO] ------------------------------------------------------------------------

[ERROR] Failed to execute goal org.wildfly.plugins:wildfly-maven-plugin:1.0.2.Final:
    execute-commands (add-jboss-config) on project jboss-config: 
    Execution add-jboss-config of goal org.wildfly.plugins:wildfly-maven-plugin:1.0.2.Final:execute-commands failed: 
    Failed to process file 'H:\projects\xyz\jboss\trunk\jboss-configuration\processed\scripts\add-jboss-config.cli': 
    H:\projects\xyz\jboss\trunk\jboss-configuration\processed\scripts\add-jboss-config.cli 
    (The system cannot find the path specified) 

我发现了问题。如果执行id被参数化,在我的例子中:

   <plugin>
       <groupId>org.wildfly.plugins</groupId>
       <artifactId>wildfly-maven-plugin</artifactId>
       <executions>
           <execution>
               <id>add-${project.artifactId}</id>
...
...
       <executions>
           <execution>
               <id>remove-${project.artifactId}</id>
...
...

要跳过插件执行,您必须明确指定执行 ID 和阶段:

        <plugin>
            <groupId>org.wildfly.plugins</groupId>
            <artifactId>wildfly-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>add-jboss-config</id>
                    <phase/>
                </execution>
                <execution>
                    <id>remove-jboss-config</id>
                    <phase/>
                </execution>
          </executions>
          <configuration>
                <skip>true</skip>
            </configuration>
        </plugin>

现在,它跳过父模块,在所有子模块中执行,我只有一个地方可以配置插件。

注意:我尝试使用静态执行 ID,但它也有跳过子模块中执行的副作用。另外,我不确定所有这些问题是否都在 wildfly-maven-plugin 中。