How to disable certain enforcer rules in a child module in a multi-module maven project?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to disable certain enforcer rules in a child module in a multi-module maven project?

Debraj Manna
I am using a maven-enforcer plugin in a multi-module maven project. Let's
say my project structure is like below

main
  - query
  - storage

My enforcer plugin in main pom looks like below

<build><plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>3.0.0-M2</version>
                <executions>
                    <execution>
                        <id>default</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <DependencyConvergence/>
                                <requireJavaVersion>
                                    <version>[1.8,)</version>
                                    <message>*** This project requires
JDK 1.8/J2SE 8 or later. ***</message>
                                </requireJavaVersion>
                            </rules>
                            <fail>true</fail>
                        </configuration>
                    </execution>
                </executions>
            </plugin></builds>

In a child module (query) if I need to disable one of the enforcer rules
(let's say DependencyConvergence) can someone let me know how can this be
done?

Maven Version - 3.6.1
Reply | Threaded
Open this post in threaded view
|

How to disable certain enforcer rules in a child module in a multi-module maven project?

Falko Modler
You could split your config into multiple executions and define a custom skip property for each execution.
In a submodule you could then skip the executions you don't want (or vice versa) by adding the respective properties.
Skipping all enforcer rules (e.g. for a "quick" local run) is more complicated, but could be achieved if everything ia put in a profile that is activated by default. You could then opt-out of it via -P!my-profile or introduce yet another property for that.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: How to disable certain enforcer rules in a child module in a multi-module maven project?

John Patrick
I would do the following snippet, not sure if phase is needed, not
sure if it should be pluginManagement, but i tend to put all
configuration in pluginManagement then only the usage in plugins.

For the query, from what I'm aware I used the same id of default, it
will override that execution from main and use the updated execution
in the query.

I've not had to do this override with enforcer before, but have had to
do something similar with the compiler plugin when building a project
in java 1.8 and 11 and 14.

What my pom's might look like, i've not tested this setup and it might
need some tweaking;

main/pom.xml

<build><pluginManagement><plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>3.0.0-M2</version>
    <executions>
      <execution>
        <id>default</id>
        <phase>validate</phase>
        <goals>
          <goal>enforce</goal>
        </goals>
        <configuration>
          <rules>
            <DependencyConvergence/>
            <requireJavaVersion>
              <version>[1.8,)</version>
              <message>*** This project requires
JDK 1.8/J2SE 8 or later. ***</message>
            </requireJavaVersion>
          </rules>
          <fail>true</fail>
        </configuration>
      </execution>
    </executions>
  </plugin>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
  </plugin>
</plugins></builds>

query/pom.xml

<build><plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <executions>
      <execution>
        <id>default</id>
        <phase>validate</phase>
        <goals>
          <goal>enforce</goal>
        </goals>
        <configuration>
          <rules>
            <requireJavaVersion>
              <version>[1.8,)</version>
              <message>*** This project requires
JDK 1.8/J2SE 8 or later. ***</message>
            </requireJavaVersion>
          </rules>
          <fail>true</fail>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins></builds>

John

On Thu, 13 Aug 2020 at 11:13, Falko Modler <[hidden email]> wrote:
>
> You could split your config into multiple executions and define a custom skip property for each execution.
> In a submodule you could then skip the executions you don't want (or vice versa) by adding the respective properties.
> Skipping all enforcer rules (e.g. for a "quick" local run) is more complicated, but could be achieved if everything ia put in a profile that is activated by default. You could then opt-out of it via -P!my-profile or introduce yet another property for that.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: How to disable certain enforcer rules in a child module in a multi-module maven project?

Anders Hammar
IMO John's solution, or something similar, is the Maven Way. Any solution
involving profiles is bad and should be avoided at all costs.

/Anders

On Fri, Aug 14, 2020 at 10:53 AM John Patrick <[hidden email]>
wrote:

> I would do the following snippet, not sure if phase is needed, not
> sure if it should be pluginManagement, but i tend to put all
> configuration in pluginManagement then only the usage in plugins.
>
> For the query, from what I'm aware I used the same id of default, it
> will override that execution from main and use the updated execution
> in the query.
>
> I've not had to do this override with enforcer before, but have had to
> do something similar with the compiler plugin when building a project
> in java 1.8 and 11 and 14.
>
> What my pom's might look like, i've not tested this setup and it might
> need some tweaking;
>
> main/pom.xml
>
> <build><pluginManagement><plugins>
>   <plugin>
>     <groupId>org.apache.maven.plugins</groupId>
>     <artifactId>maven-enforcer-plugin</artifactId>
>     <version>3.0.0-M2</version>
>     <executions>
>       <execution>
>         <id>default</id>
>         <phase>validate</phase>
>         <goals>
>           <goal>enforce</goal>
>         </goals>
>         <configuration>
>           <rules>
>             <DependencyConvergence/>
>             <requireJavaVersion>
>               <version>[1.8,)</version>
>               <message>*** This project requires
> JDK 1.8/J2SE 8 or later. ***</message>
>             </requireJavaVersion>
>           </rules>
>           <fail>true</fail>
>         </configuration>
>       </execution>
>     </executions>
>   </plugin>
> <plugins>
>   <plugin>
>     <groupId>org.apache.maven.plugins</groupId>
>     <artifactId>maven-enforcer-plugin</artifactId>
>   </plugin>
> </plugins></builds>
>
> query/pom.xml
>
> <build><plugins>
>   <plugin>
>     <groupId>org.apache.maven.plugins</groupId>
>     <artifactId>maven-enforcer-plugin</artifactId>
>     <executions>
>       <execution>
>         <id>default</id>
>         <phase>validate</phase>
>         <goals>
>           <goal>enforce</goal>
>         </goals>
>         <configuration>
>           <rules>
>             <requireJavaVersion>
>               <version>[1.8,)</version>
>               <message>*** This project requires
> JDK 1.8/J2SE 8 or later. ***</message>
>             </requireJavaVersion>
>           </rules>
>           <fail>true</fail>
>         </configuration>
>       </execution>
>     </executions>
>   </plugin>
> </plugins></builds>
>
> John
>
> On Thu, 13 Aug 2020 at 11:13, Falko Modler <[hidden email]> wrote:
> >
> > You could split your config into multiple executions and define a custom
> skip property for each execution.
> > In a submodule you could then skip the executions you don't want (or
> vice versa) by adding the respective properties.
> > Skipping all enforcer rules (e.g. for a "quick" local run) is more
> complicated, but could be achieved if everything ia put in a profile that
> is activated by default. You could then opt-out of it via -P!my-profile or
> introduce yet another property for that.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: How to disable certain enforcer rules in a child module in a multi-module maven project?

Falko Modler
I agree that John's solution is more how Maven is expecting you to do things in this case.
But it is also more verbose in the submodules and it forces you to repeat yourself (I really like the DRY principle), since you basically have to redefine rules.

For me it depends how much you want to manage/maintain centrally (in a root parent or so) and how much you want override or redefine in the submodules.

Sure, if the rule config is simple, redefinition won't hurt much but if you have complex stuff, it can become painful to have those configs scattered over multiple modules.

Just my 2 cents.

PS: Haven't checked, but theoretically you should also be able to prepare all executions in pluginManagement, but set each phase to none. Then in each concrete build plugins section you should then be able to bind the executions you want/need to the right phase.

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: How to disable certain enforcer rules in a child module in a multi-module maven project?

John Patrick
So something like the following if all configuration is managed via a
pluginMangement section;

<build><pluginManagement><plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>3.0.0-M2</version>
    <executions>
      <execution>
        <id>alpha</id>
        <phase></phase>
        <goals>
          <goal>enforce</goal>
        </goals>
        <configuration>
          <rules>
            <DependencyConvergence/>
            <requireJavaVersion>
              <version>[1.8,)</version>
              <message>*** This project requires
JDK 1.8/J2SE 8 or later. ***</message>
            </requireJavaVersion>
          </rules>
          <fail>true</fail>
        </configuration>
      </execution>
      <execution>
        <id>bravo</id>
        <phase></phase>
        <goals>
          <goal>enforce</goal>
        </goals>
        <configuration>
          <rules>
            <requireJavaVersion>
              <version>[1.8,)</version>
              <message>*** This project requires
JDK 1.8/J2SE 8 or later. ***</message>
            </requireJavaVersion>
          </rules>
          <fail>true</fail>
        </configuration>
      </execution>
    </executions>
  </plugin>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <executions>
      <execution>
        <id>alpha</id>
        <phase>validate</phase>
      </execution>
    </executions>
  </plugin>
</plugins></builds>

query/pom.xml

<build><plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <executions>
      <execution>
        <id>alpha</id>
        <phase></phase>
      </execution>
      <execution>
        <id>bravo</id>
        <phase>validate</phase>
      </execution>
    </executions>
  </plugin>
    </executions>
  </plugin>
</plugins></builds>

you might also be able to do it via a property and in query define the
bravo execution instead of the alpha. I've used a similar technique
with maven-surefire-plugin, where i define the plugin version using a
property and have a default in the root/parent pom, and in one
specific child pom i define a different surefire version. so this
might work...

<build><pluginManagement><plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>3.0.0-M2</version>
    <executions>
      <execution>
        <id>alpha</id>
        <phase></phase>
        <goals>
          <goal>enforce</goal>
        </goals>
        <configuration>
          <rules>
            <DependencyConvergence/>
            <requireJavaVersion>
              <version>[1.8,)</version>
              <message>*** This project requires
JDK 1.8/J2SE 8 or later. ***</message>
            </requireJavaVersion>
          </rules>
          <fail>true</fail>
        </configuration>
      </execution>
      <execution>
        <id>bravo</id>
        <phase></phase>
        <goals>
          <goal>enforce</goal>
        </goals>
        <configuration>
          <rules>
            <requireJavaVersion>
              <version>[1.8,)</version>
              <message>*** This project requires
JDK 1.8/J2SE 8 or later. ***</message>
            </requireJavaVersion>
          </rules>
          <fail>true</fail>
        </configuration>
      </execution>
    </executions>
  </plugin>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <executions>
      <execution>
        <id>${which-enforcer-id}</id>
        <phase>validate</phase>
      </execution>
    </executions>
  </plugin>
</plugins></builds>
<properties>
  <which-enforcer-id>alpha</which-enforcer-id>
</properties>

query/pom.xml

<properties>
  <which-enforcer-id>bravo</which-enforcer-id>
</properties>

John


On Fri, 14 Aug 2020 at 11:23, Falko Modler <[hidden email]> wrote:

>
> I agree that John's solution is more how Maven is expecting you to do things in this case.
> But it is also more verbose in the submodules and it forces you to repeat yourself (I really like the DRY principle), since you basically have to redefine rules.
>
> For me it depends how much you want to manage/maintain centrally (in a root parent or so) and how much you want override or redefine in the submodules.
>
> Sure, if the rule config is simple, redefinition won't hurt much but if you have complex stuff, it can become painful to have those configs scattered over multiple modules.
>
> Just my 2 cents.
>
> PS: Haven't checked, but theoretically you should also be able to prepare all executions in pluginManagement, but set each phase to none. Then in each concrete build plugins section you should then be able to bind the executions you want/need to the right phase.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]