mojohaus / flatten-maven-plugin

Flatten Maven Plugin
https://www.mojohaus.org/flatten-maven-plugin/
Apache License 2.0
201 stars 85 forks source link

flattenMode=bom removes pluginManagement #386

Open austin-beer opened 9 months ago

austin-beer commented 9 months ago

When flattenMode is set to bom the plugin keeps the properties and dependencyManagement sections of the POM. But as of version 1.5.0 it removes the pluginManagement section in bom mode. I believe the behavior of bom mode should be changed to keep the pluginManagement section as well, since it is very similar to dependencyManagement and serves a very similar purpose. See https://www.baeldung.com/maven-plugin-management#plugin-management.

slawekjaranowski commented 9 months ago

I'm not sure if we should preserve pluginManagement for 'bom' mode.

Why do you think that should be fixed?

Current behavior meet documentation for bom: https://www.mojohaus.org/flatten-maven-plugin/flatten-mojo.html#flattenMode

Does any previous version of plugin preserve pluginManagement for bom?

austin-beer commented 9 months ago

No, no previous version of this plugin preserves it for "bom" mode. However, pluginManagement is similar to dependencyManagement in that both are used to provide configuration information to child POMs. See https://www.baeldung.com/maven-plugin-management#plugin-management. Thus it makes sense to keep both dependencyManagement and pluginManagement in a BOM, which acts as a parent for other POMs. So what I'm suggesting and requesting is that "bom" mode be changed to also keep pluginManagement. Does that make sense?

austin-beer commented 9 months ago

I've updated the description of the ticket to better reflect what I'm requesting.

slawekjaranowski commented 9 months ago

only dependencyManagement can be imported as bom.

pluginManagement can not be imported as is in dependencyManagement https://issues.apache.org/jira/browse/MNG-5588

I have doubt for such change, I would like to see more opinions on it.

rezanirumand commented 9 months ago

i managed to keep the pluging and plugin management:

           <plugin>
                <!-- https://www.mojohaus.org/flatten-maven-plugin/flatten-mojo.html -->
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                <version>1.5.0</version>
                <configuration>
                    <keepCommentsInPom>true</keepCommentsInPom>
                    <flattenMode>bom</flattenMode>
                    <flattenDependencyMode>all</flattenDependencyMode>
                    <pomElements>
                        <build>interpolate</build>
                        <properties>keep</properties>
                        <dependencyManagement>interpolate</dependencyManagement>
                    </pomElements>
                </configuration>
                <executions>
                    <execution>
                        <id>flatten</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>flatten</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>flatten.clean</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
austin-beer commented 9 months ago

i managed to keep the pluging and plugin management:

           <plugin>
                <!-- https://www.mojohaus.org/flatten-maven-plugin/flatten-mojo.html -->
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                <version>1.5.0</version>
                <configuration>
                    <keepCommentsInPom>true</keepCommentsInPom>
                    <flattenMode>bom</flattenMode>
                    <flattenDependencyMode>all</flattenDependencyMode>
                    <pomElements>
                        <build>interpolate</build>
                        <properties>keep</properties>
                        <dependencyManagement>interpolate</dependencyManagement>
                    </pomElements>
                </configuration>
                <executions>
                    <execution>
                        <id>flatten</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>flatten</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>flatten.clean</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

Does this solution keep all of the elements in the build section or just the pluginManagement section? The desired behavior is to keep only the pluginManagement section with the build section, which is what I believe my submitted pull request does.

rezanirumand commented 9 months ago

you can do it like:

also if you do not want to resolve properties, use keep instead interpolate.

                    <pomElements>
                        <pluginManagement>interpolate</pluginManagement>
                        <properties>keep</properties>
                        <dependencyManagement>interpolate</dependencyManagement>
                    </pomElements>
austin-beer commented 9 months ago

Thanks @rezanirumand! I created some examples showing the behavior of flattenMode=bom both without and with your suggestion. It does produce the desired behavior!

This feature request and associated PR still stand, though. Should this be made the default behavior of the flattenMode=bom mode, since I believe it is the expected behavior most of the time? It was definitely the expected behavior for me.

Here is an example parent POM used as a BOM:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.test</groupId>
  <artifactId>test-bom</artifactId>
  <version>0.0.0</version>
  <packaging>pom</packaging>

  <properties>
    <!-- This is used to set the version for all of the child projects -->
    <revision>2.0.2</revision>

    <!-- Dependency versions -->
    <commons-collections4.version>4.4</commons-collections4.version>
    <postgresql.version>42.6.0</postgresql.version>
    <jackson.version>2.15.3</jackson.version>

    <!-- Plugin versions -->
    <flatten-maven.version>1.5.0</flatten-maven.version>
    <maven-compiler.version>3.11.0</maven-compiler.version>
    <maven-surefire.version>3.1.2</maven-surefire.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-collections4</artifactId>
        <version>${commons-collections4.version}</version>
      </dependency>
      <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>${postgresql.version}</version>
      </dependency>
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>flatten-maven-plugin</artifactId>
          <version>${flatten-maven.version}</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>${maven-compiler.version}</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>${maven-surefire.version}</version>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>flatten-maven-plugin</artifactId>
        <configuration>
          <flattenMode>bom</flattenMode>
        </configuration>
        <executions>
          <execution>
            <id>flatten</id>
            <phase>process-resources</phase>
            <goals>
              <goal>flatten</goal>
            </goals>
          </execution>
          <execution>
            <id>flatten.clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

And this is the flatten result when running mvn flatten:flatten on the parent POM/BOM (you can see that pluginManagement is missing):

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>test-bom</artifactId>
  <version>0.0.0</version>
  <packaging>pom</packaging>
  <properties>
    <jackson.version>2.15.3</jackson.version>
    <maven-compiler.version>3.11.0</maven-compiler.version>
    <maven-surefire.version>3.1.2</maven-surefire.version>
    <revision>2.0.2</revision>
    <postgresql.version>42.6.0</postgresql.version>
    <flatten-maven.version>1.5.0</flatten-maven.version>
    <commons-collections4.version>4.4</commons-collections4.version>
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-collections4</artifactId>
        <version>${commons-collections4.version}</version>
      </dependency>
      <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>${postgresql.version}</version>
      </dependency>
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

Here is the same example parent POM used as a BOM that's been updated with the suggestion from @rezanirumand. I used keep and only specified pluginManagement, though:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.test</groupId>
  <artifactId>test-bom</artifactId>
  <version>0.0.0</version>
  <packaging>pom</packaging>

  <properties>
    <!-- This is used to set the version for all of the child projects -->
    <revision>2.0.2</revision>

    <!-- Dependency versions -->
    <commons-collections4.version>4.4</commons-collections4.version>
    <postgresql.version>42.6.0</postgresql.version>
    <jackson.version>2.15.3</jackson.version>

    <!-- Plugin versions -->
    <flatten-maven.version>1.5.0</flatten-maven.version>
    <maven-compiler.version>3.11.0</maven-compiler.version>
    <maven-surefire.version>3.1.2</maven-surefire.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-collections4</artifactId>
        <version>${commons-collections4.version}</version>
      </dependency>
      <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>${postgresql.version}</version>
      </dependency>
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>flatten-maven-plugin</artifactId>
          <version>${flatten-maven.version}</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>${maven-compiler.version}</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>${maven-surefire.version}</version>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>flatten-maven-plugin</artifactId>
        <configuration>
          <flattenMode>bom</flattenMode>
          <pomElements>
             <pluginManagement>keep</pluginManagement>
          </pomElements>
        </configuration>
        <executions>
          <execution>
            <id>flatten</id>
            <phase>process-resources</phase>
            <goals>
              <goal>flatten</goal>
            </goals>
          </execution>
          <execution>
            <id>flatten.clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

And this is the flatten result when running mvn flatten:flatten on the updated parent POM/BOM (note that pluginManagement is now present, which is what we want):

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>test-bom</artifactId>
  <version>0.0.0</version>
  <packaging>pom</packaging>
  <properties>
    <postgresql.version>42.6.0</postgresql.version>
    <flatten-maven.version>1.5.0</flatten-maven.version>
    <jackson.version>2.15.3</jackson.version>
    <commons-collections4.version>4.4</commons-collections4.version>
    <maven-compiler.version>3.11.0</maven-compiler.version>
    <maven-surefire.version>3.1.2</maven-surefire.version>
    <revision>2.0.2</revision>
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-collections4</artifactId>
        <version>${commons-collections4.version}</version>
      </dependency>
      <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>${postgresql.version}</version>
      </dependency>
      <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${jackson.version}</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>flatten-maven-plugin</artifactId>
          <version>${flatten-maven.version}</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>${maven-compiler.version}</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>${maven-surefire.version}</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>
slawekjaranowski commented 8 months ago

I will see as resolving this issue a new example page in documentation https://www.mojohaus.org/flatten-maven-plugin/ we have an Examples section which can be extended.