Author: khmarbaise Date: Sun Aug 9 12:43:37 2015 New Revision: 1694884 URL: http://svn.apache.org/r1694884 Log: [MENFORCER-229] Ban Distribution Management documentation example doesn't work The problem was to use the getModel() of the Maven Project which contains the interpolated inheritance hierarchy which results in having always an distributionManagement if somewhere in the higher levels distributionManagement has been defined. Using the getOriginalModel() shows the non interpolated model (pom file) which is the correct way to check the distributionManagement. Furthermore enhanced documentation.
Added: maven/enforcer/trunk/enforcer-rules/src/site/apt/Images.odp (with props) maven/enforcer/trunk/enforcer-rules/src/site/resources/ maven/enforcer/trunk/enforcer-rules/src/site/resources/images/ maven/enforcer/trunk/enforcer-rules/src/site/resources/images/module.png (with props) maven/enforcer/trunk/enforcer-rules/src/site/resources/images/root-with-modules.png (with props) maven/enforcer/trunk/enforcer-rules/src/site/resources/images/root-with-parent-and-modules.png (with props) maven/enforcer/trunk/enforcer-rules/src/site/resources/images/root-with-parent.png (with props) maven/enforcer/trunk/enforcer-rules/src/site/resources/images/root.png (with props) Modified: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDistributionManagement.java maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/DistributionManagementCheck.java maven/enforcer/trunk/enforcer-rules/src/site/apt/banDistributionManagement.apt.vm maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/BanDistributionManagementTest.java maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/ban-distribution-management-multi-module-build/module1/pom.xml maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/ban-distribution-management-multi-module-build/pom.xml Modified: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDistributionManagement.java URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDistributionManagement.java?rev=1694884&r1=1694883&r2=1694884&view=diff ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDistributionManagement.java (original) +++ maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/BanDistributionManagement.java Sun Aug 9 12:43:37 2015 @@ -40,6 +40,7 @@ public class BanDistributionManagement /** * If we turn on the <code>ignoreParent</code> the parent will be ignored. + * @deprecated */ private boolean ignoreParent = true; @@ -58,11 +59,6 @@ public class BanDistributionManagement */ private boolean allowSite = false; - /** - * Execute checking only in root of project. - */ - private boolean executeRootOnly = false; - private Log logger; /** @@ -77,21 +73,33 @@ public class BanDistributionManagement { MavenProject project = (MavenProject) helper.evaluate( "${project}" ); - if ( !project.isExecutionRoot() && !executeRootOnly ) + if ( project.isExecutionRoot() ) { + if ( project.getParent() == null ) + { + // Does it make sense to check something? If yes please make a JIRA ticket for it. + logger.debug( "We have no parent and in the root of a build we don't check anything," ); + logger.debug( "because that is the location where we defined maven-enforcer-plugin." ); + } + else + { + logger.debug( "We are in the root of the execution and we have a parent." ); - logger.debug( "distributionManagement: " + project.getDistributionManagement() ); - + DistributionManagementCheck check = new DistributionManagementCheck( project ); + check.execute( isAllowRepository(), isAllowSnapshotRepository(), isAllowSite() ); + } + } + else + { + logger.debug( "We are in a deeper level." ); DistributionManagementCheck check = new DistributionManagementCheck( project ); - check.execute( isAllowRepository(), isAllowSnapshotRepository(), isAllowSite() ); - if ( !isIgnoreParent() && ( project.getParent() != null ) ) + if ( !isIgnoreParent() ) { - DistributionManagementCheck checkParent = new DistributionManagementCheck( project.getParent() ); - checkParent.execute( isAllowRepository(), isAllowSnapshotRepository(), isAllowSite() ); + logger.warn( "You have configured not to ignore the parent." ); + logger.warn( "This configuration is deprecated and will be ignored." ); } - } } catch ( ExpressionEvaluationException e ) Modified: maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/DistributionManagementCheck.java URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/DistributionManagementCheck.java?rev=1694884&r1=1694883&r2=1694884&view=diff ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/DistributionManagementCheck.java (original) +++ maven/enforcer/trunk/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/DistributionManagementCheck.java Sun Aug 9 12:43:37 2015 @@ -25,7 +25,6 @@ import org.apache.maven.project.MavenPro /** * @author Karl Heinz Marbaise <a href="mailto:khmarba...@apache.org">khmarba...@apache.org</a> - * */ public class DistributionManagementCheck { @@ -33,7 +32,7 @@ public class DistributionManagementCheck public DistributionManagementCheck( MavenProject project ) { - this.distributionManagement = project.getDistributionManagement(); + this.distributionManagement = project.getOriginalModel().getDistributionManagement(); } public void execute( boolean isAllowRepository, boolean isAllowSnapshotRepository, boolean isAllowSite ) Added: maven/enforcer/trunk/enforcer-rules/src/site/apt/Images.odp URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/site/apt/Images.odp?rev=1694884&view=auto ============================================================================== Binary file - no diff available. Propchange: maven/enforcer/trunk/enforcer-rules/src/site/apt/Images.odp ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Modified: maven/enforcer/trunk/enforcer-rules/src/site/apt/banDistributionManagement.apt.vm URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/site/apt/banDistributionManagement.apt.vm?rev=1694884&r1=1694883&r2=1694884&view=diff ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/site/apt/banDistributionManagement.apt.vm (original) +++ maven/enforcer/trunk/enforcer-rules/src/site/apt/banDistributionManagement.apt.vm Sun Aug 9 12:43:37 2015 @@ -38,7 +38,7 @@ Ban Distribution Management * allowSite - You can allow site entry (default: false). - * ignoreParent - You can control if the parent will be checked or not (default: true). + * ignoreParent - You can control if the parent will be checked or not (default: true) (deprecated don't use it anymore). [] @@ -74,23 +74,101 @@ Ban Distribution Management </project> +---+ - The above configuration will prevent any declaration of distributionManagement - in your pom files except in the parent. +* Best Practice + + Usually you should define the <<distributionManagement>> only in a limited number of cases. + If you are in a corporate environment it makes usually only sense to define the <<distributionManagement>> + in the corporate pom and forbid the usage in any other pom's. Sometimes it makes sense to + allow for example the site repository definition in your other pom's which can be defined by using the + <<banDistributionManagement>> rule. For this use case the following has to be defined in your + corporate pom file: + + + +---+ +<project> + [...] + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-enforcer-plugin</artifactId> + <version>${project.version}</version> + <executions> + <execution> + <id>no-distribution-management-at-all</id> + <goals> + <goal>enforce</goal> + </goals> + <configuration> + <rules> + <banDistributionManagement> + <allowSite>true</allowSite> + </banDistributionManagement> + </rules> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + [...] +</project> ++---+ + + +* The Project Types + + If we take a closer look to the possible project structures you will find the following cases where + the green arrow will show our current position. + + * The Project without Parent. + + We have no parent and no modules at all. This could be the situation where we are creating + a corporate pom file or another simple Maven project. So the definition of maven-enforcer-plugin + is within this pom file. - Let use take a look at the following project which is a typical multi-module project. +[images/root.png] Root Project. + + In consequence it does not really make sense to check if the pom contains distributionManagement + entries or not. + + * Project with Parent. + + We have a project with a parent. The parent is likely a kind of a corporate pom file which + contains the definition of maven-enforcer-plugin. So in this case it makes sense to check + if distributionManagement entries are made or not. -+-----+ - +-- root (pom.xml) - +-- m1 (pom.xml) - +-- m2 (pom.xml) -+-----+ + <<Note>>: At the moment it is not possible to + check if the parent does not contain the definition of maven-enforcer-plugin which would change + the situation. + +[images/root-with-parent.png] Root project. + - Usually you should define the distributionManagement only once in your hierarchy. + * Project with Parent and Modules. + + This situation is more or less the same as the one before. So banDistributionManagement rule will check + the distributionManagement entries. - company-parent (pom.xml) +[images/root-with-parent-and-modules.png] Root project with parent and modules. + + + * Root Project With Modules. + + If we don't have a parent this means the definition of maven-enforcer-plugin + is likely done in the current pom file which means it does not make really sense + to check the distributionManagement. - - +-- root (pom.xml) - +-- m1 (pom.xml) - +-- m2 (pom.xml) +[images/root-with-modules.png] Root with Modules. + + + * Module of a Multi Module Build + + In this case we have the scenario that the module m1 has a parent <<p1>> which could contain + the definition of the maven-enforcer-plugin or the <<parent>> of which in consequence means to check the + distributionManagement entries. + +[images/module.png] Root with Modules. + + [] Added: maven/enforcer/trunk/enforcer-rules/src/site/resources/images/module.png URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/site/resources/images/module.png?rev=1694884&view=auto ============================================================================== Binary file - no diff available. Propchange: maven/enforcer/trunk/enforcer-rules/src/site/resources/images/module.png ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Added: maven/enforcer/trunk/enforcer-rules/src/site/resources/images/root-with-modules.png URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/site/resources/images/root-with-modules.png?rev=1694884&view=auto ============================================================================== Binary file - no diff available. Propchange: maven/enforcer/trunk/enforcer-rules/src/site/resources/images/root-with-modules.png ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Added: maven/enforcer/trunk/enforcer-rules/src/site/resources/images/root-with-parent-and-modules.png URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/site/resources/images/root-with-parent-and-modules.png?rev=1694884&view=auto ============================================================================== Binary file - no diff available. Propchange: maven/enforcer/trunk/enforcer-rules/src/site/resources/images/root-with-parent-and-modules.png ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Added: maven/enforcer/trunk/enforcer-rules/src/site/resources/images/root-with-parent.png URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/site/resources/images/root-with-parent.png?rev=1694884&view=auto ============================================================================== Binary file - no diff available. Propchange: maven/enforcer/trunk/enforcer-rules/src/site/resources/images/root-with-parent.png ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Added: maven/enforcer/trunk/enforcer-rules/src/site/resources/images/root.png URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/site/resources/images/root.png?rev=1694884&view=auto ============================================================================== Binary file - no diff available. Propchange: maven/enforcer/trunk/enforcer-rules/src/site/resources/images/root.png ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Modified: maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/BanDistributionManagementTest.java URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/BanDistributionManagementTest.java?rev=1694884&r1=1694883&r2=1694884&view=diff ============================================================================== --- maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/BanDistributionManagementTest.java (original) +++ maven/enforcer/trunk/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/BanDistributionManagementTest.java Sun Aug 9 12:43:37 2015 @@ -26,6 +26,7 @@ import org.apache.maven.enforcer.rule.ap import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; import org.apache.maven.model.DeploymentRepository; import org.apache.maven.model.DistributionManagement; +import org.apache.maven.model.Model; import org.apache.maven.model.Site; import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; @@ -115,7 +116,8 @@ public class BanDistributionManagementTe throws Exception { BanDistributionManagement rule = - setupProjectWithDistributionManagement( new DeploymentRepository(), new DeploymentRepository(), new Site() ); + setupProjectWithDistributionManagement( new DeploymentRepository(), new DeploymentRepository(), + new Site() ); rule.execute( helper ); // intentionally no assert cause we expect an exception. } @@ -184,7 +186,8 @@ public class BanDistributionManagementTe throws Exception { BanDistributionManagement rule = - setupProjectWithDistributionManagement( new DeploymentRepository(), new DeploymentRepository(), new Site() ); + setupProjectWithDistributionManagement( new DeploymentRepository(), new DeploymentRepository(), + new Site() ); rule.setAllowRepository( true ); rule.setAllowSnapshotRepository( true ); rule.setAllowSite( true ); @@ -192,19 +195,6 @@ public class BanDistributionManagementTe // intentionally no assert cause in case of an exception the test will be red. } - @Test( expected = EnforcerRuleException.class ) - public void shouldThrowExceptionCauseParentProjectHasDistributionManagement() - throws Exception - { - BanDistributionManagement rule = - setupProjectWithParentDistributionManagement( new DeploymentRepository(), null, null ); - - rule.setIgnoreParent( false ); - - rule.execute( helper ); - // intentionally no assert cause we expect an exception. - } - @Test public void shouldThrowExceptionCauseParentProjectHasDistributionManagementSnapshotRepository() throws Exception @@ -221,7 +211,7 @@ public class BanDistributionManagementTe private BanDistributionManagement setupProjectWithParentDistributionManagement( DeploymentRepository repository, DeploymentRepository snapshotRepository, Site site ) - throws ExpressionEvaluationException + throws ExpressionEvaluationException { project = setupProject( null ); @@ -231,7 +221,9 @@ public class BanDistributionManagementTe when( dmParent.getSite() ).thenReturn( site ); MavenProject parentProject = mock( MavenProject.class ); - when( parentProject.getDistributionManagement() ).thenReturn( dmParent ); + Model model = mock( Model.class ); + when( model.getDistributionManagement() ).thenReturn( dmParent ); + when( parentProject.getOriginalModel() ).thenReturn( model ); when( project.getParent() ).thenReturn( parentProject ); BanDistributionManagement rule = setupEnforcerRule(); @@ -252,7 +244,7 @@ public class BanDistributionManagementTe private BanDistributionManagement setupProjectWithDistributionManagement( DeploymentRepository repository, DeploymentRepository snapshotRepository, Site site ) - throws ExpressionEvaluationException + throws ExpressionEvaluationException { DistributionManagement dm = mock( DistributionManagement.class ); when( dm.getRepository() ).thenReturn( repository ); @@ -261,6 +253,9 @@ public class BanDistributionManagementTe project = setupProject( dm ); + when( project.getParent() ).thenReturn( mock( MavenProject.class ) ); + when( project.isExecutionRoot() ).thenReturn( true ); + BanDistributionManagement rule = setupEnforcerRule(); return rule; @@ -270,7 +265,9 @@ public class BanDistributionManagementTe { MavenProject project = mock( MavenProject.class ); when( project.getPackaging() ).thenReturn( "jar" ); - when( project.getDistributionManagement() ).thenReturn( distributionManagement ); + Model mavenModel = mock( Model.class ); + when( project.getOriginalModel() ).thenReturn( mavenModel ); + when( mavenModel.getDistributionManagement() ).thenReturn( distributionManagement ); return project; } Modified: maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/ban-distribution-management-multi-module-build/module1/pom.xml URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/ban-distribution-management-multi-module-build/module1/pom.xml?rev=1694884&r1=1694883&r2=1694884&view=diff ============================================================================== --- maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/ban-distribution-management-multi-module-build/module1/pom.xml (original) +++ maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/ban-distribution-management-multi-module-build/module1/pom.xml Sun Aug 9 12:43:37 2015 @@ -29,4 +29,14 @@ under the License. </parent> <artifactId>module1</artifactId> + <!-- + ! This entry will fail the build. + --> + <distributionManagement> + <repository> + <id>first</id> + <name>This is the name</name> + <url>file:///Test</url> + </repository> + </distributionManagement> </project> Modified: maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/ban-distribution-management-multi-module-build/pom.xml URL: http://svn.apache.org/viewvc/maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/ban-distribution-management-multi-module-build/pom.xml?rev=1694884&r1=1694883&r2=1694884&view=diff ============================================================================== --- maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/ban-distribution-management-multi-module-build/pom.xml (original) +++ maven/enforcer/trunk/maven-enforcer-plugin/src/it/projects/ban-distribution-management-multi-module-build/pom.xml Sun Aug 9 12:43:37 2015 @@ -27,6 +27,9 @@ under the License. <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> + <!-- + ! This entry is allowed + --> <distributionManagement> <repository> <id>first</id>