program tip

상위 모듈에서 Maven 플러그인 목표를 실행하지만 하위 모듈에서는 실행하지 않습니다.

radiobox 2020. 12. 29. 06:57
반응형

상위 모듈에서 Maven 플러그인 목표를 실행하지만 하위 모듈에서는 실행하지 않습니다.


buildnumber-maven-plugin정의하는 프로필을 사용 하여 빌드 번호를 증가시킨 다음 소스 제어에 체크인 하는 다중 모듈 maven 프로젝트가 있습니다.

부모 pom.xml에 플러그인을 정의하면 모든 자식 빌드에 대해서도 실행됩니다.

여기 내 부모 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.webwars</groupId>
  <artifactId>parent</artifactId>
  <packaging>pom</packaging>
  <properties>
    <buildNumber.properties>${basedir}/../parent/buildNumber.properties</buildNumber.properties>
  </properties>
  <version>1.0-SNAPSHOT</version>
  <name>Parent Project</name>
  <profiles>
    <profile>
      <id>release</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
              <debug>false</debug>
              <optimize>true</optimize>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>buildnumber-maven-plugin</artifactId>
            <version>1.0-beta-3</version>
            <executions>
              <execution>
                <phase>validate</phase>
                <goals>
                  <goal>create</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <buildNumberPropertiesFileLocation>${buildNumber.properties}</buildNumberPropertiesFileLocation>
              <getRevisionOnlyOnce>true</getRevisionOnlyOnce>
              <doCheck>false</doCheck>
              <doUpdate>false</doUpdate>
              <format>{0, number}</format>
              <items>
                <item>buildNumber</item>
              </items>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-scm-plugin</artifactId>
            <executions>
              <execution>
                <phase>install</phase>
                <goals>
                  <goal>checkin</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <basedir>${basedir}</basedir>
              <includes>buildNumber.properties</includes>
              <message>[Automated checkin] of ${basedir} Build version: ${major.version}.${minor.version}.${buildNumber}</message>
              <developerConnectionUrl>...</developerConnectionUrl>
            </configuration>
          </plugin>         
        </plugins>
      </build>
    </profile>
  </profiles>

  <modules>

    <module>../common</module>
    <module>../data</module>
    <module>../client</module>
    <module>../webplatform</module>
  </modules>
 ...
</project>

pom 참조 플러그인 섹션에 설명 된대로 :

groupId : artifactId : version의 표준 좌표 외에 플러그인을 구성하는 요소가 있거나 플러그인과의 상호 작용을 구축합니다.

  • inherited : true 또는 false,이 플러그인 구성이이 구성에서 상속되는 POM에 적용되어야하는지 여부.

So just add <inherited>false</inherited> to the buildnumber-maven-plugin configuration to avoid inheritance in children POMs:

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>buildnumber-maven-plugin</artifactId>
        <version>1.0-beta-3</version>
        <inherited>false</inherited>
        ...
      </plugin>

You can add <inherited>false</inherited> to the plugin configuration to avoid inheritance in children POMs:

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>buildnumber-maven-plugin</artifactId>
        <version>1.0-beta-3</version>
        <inherited>false</inherited>
        ...
      </plugin>

Or, if your plugin has multiple executions, you can control which executions are inherited and which are not by adding the inherited tag to the execution body:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
      <execution>
        <id>parent-only</id>
        <phase>initialize</phase>
        <inherited>false</inherited>
        <configuration>
          <target>
            <echo message="Echoed only by this module."/>
          </target>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
      <execution>
        <id>all-modules</id>
        <phase>initialize</phase>
        <inherited>true</inherited> <!-- Defaults to true, so you could leave this line out -->
        <configuration>
          <target>
            <echo message="Echoed in this module and each child module."/>
          </target>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

There is a built-in maven option: mvn --help ... -N,--non-recursive Do not recurse into sub-projects


Just an addition to the great answers here: note that per-execution inheritance is broken in Maven 2: http://jira.codehaus.org/browse/MNG-3959


If the plugin is custom one and you have access to plugin MOJO code, you can mark the plugin as aggregator; if the expected behavior is applicable for all projects where plugin is to be used.

As mentioned in Mojo API Specification ,

Flags this Mojo to run it in a multi module way, i.e. aggregate the build with the set of projects listed as modules.

Example,

@Mojo(name = "createHF", inheritByDefault = false, aggregator = true)
public class CreateHFMojo extends AbstractMojo {

..

public void execute() throws MojoExecutionException, MojoFailureException {
 ....
}

..

}

Detailed example on github.

ReferenceURL : https://stackoverflow.com/questions/1670900/execute-maven-plugin-goal-on-parent-module-but-not-on-children

반응형