RequireNoRepositories extends AbstractNonCacheableEnforcerRule { @Override public void execute( EnforcerRuleHelper helper ) throws EnforcerRuleException { logger = helper.getLog(); MavenSession session; try { session = (MavenSession) helper.evaluate( "${session}" ); List<MavenProject> sortedProjects = session.getProjectDependencyGraph().getSortedProjects(); List<Model> models = new ArrayList<>(); for ( MavenProject mavenProject : sortedProjects ) { logger.debug( "Scanning project: " + mavenProject.getGroupId() + ":" + mavenProject.getArtifactId() + VERSION + mavenProject.getVersion() ); models.add( mavenProject.getOriginalModel() ); } List<Model> badModels = new ArrayList<>(); StringBuilder newMsg = new StringBuilder(); newMsg.append( "Some poms have repositories defined:" + System.lineSeparator() ); for ( Model model : models ) { if ( banRepositories ) { List<Repository> repos = model.getRepositories(); if ( repos != null && !repos.isEmpty() ) { List<String> bannedRepos = findBannedRepositories( repos, allowedRepositories, allowSnapshotRepositories ); if ( !bannedRepos.isEmpty() ) { badModels.add( model ); newMsg.append( model.getGroupId() + ":" + model.getArtifactId() + VERSION + model.getVersion() + " has repositories " + bannedRepos ); } } } if ( banPluginRepositories ) { List<Repository> repos = model.getPluginRepositories(); if ( repos != null && !repos.isEmpty() ) { List<String> bannedRepos = findBannedRepositories( repos, allowedPluginRepositories, allowSnapshotPluginRepositories ); if ( !bannedRepos.isEmpty() ) { badModels.add( model ); newMsg.append( model.getGroupId() + ":" + model.getArtifactId() + VERSION + model.getVersion() + " has plugin repositories " + bannedRepos ); } } } } if ( !badModels.isEmpty() ) { String message = getMessage(); if ( StringUtils.isNotEmpty( message ) ) { newMsg.append( message ); } throw new EnforcerRuleException( newMsg.toString() ); } } catch ( ExpressionEvaluationException e ) { throw new EnforcerRuleException( e.getLocalizedMessage() ); } }  final void setBanRepositories( boolean banRepositories ); final void setBanPluginRepositories( boolean banPluginRepositories ); final void setAllowedRepositories( List<String> allowedRepositories ); final void setAllowedPluginRepositories( List<String> allowedPluginRepositories ); final void setAllowSnapshotRepositories( boolean allowSnapshotRepositories ); final void setAllowSnapshotPluginRepositories( boolean allowSnapshotPluginRepositories ); @Override void execute( EnforcerRuleHelper helper );  }
@Test public void testAllBannedNoRepositories() throws EnforcerRuleException { MavenProject baseProject = createStandAloneProject(); setupSortedProjects( Collections.singletonList( baseProject ) ); rule.execute( helper ); }
@Test( expected = EnforcerRuleException.class ) public void testAllBannedWithRepository() throws EnforcerRuleException { MavenProject baseProject = createStandAloneProject(); addRepository( baseProject, createRepository( "repo", "http: setupSortedProjects( Collections.singletonList( baseProject ) ); rule.execute( helper ); }
@Test( expected = EnforcerRuleException.class ) public void testAllBannedWithPluginRepository() throws EnforcerRuleException { MavenProject baseProject = createStandAloneProject(); addPluginRepository( baseProject, createRepository( "repo", "http: setupSortedProjects( Collections.singletonList( baseProject ) ); rule.execute( helper ); }
@Test( expected = EnforcerRuleException.class ) public void testAllBannedWithSnapshotRepository() throws EnforcerRuleException { MavenProject baseProject = createStandAloneProject(); addRepository( baseProject, createSnapshotRepository( "repo", "http: setupSortedProjects( Collections.singletonList( baseProject ) ); rule.execute( helper ); }
@Test public void testAllBannedWithEmptyRepository() throws EnforcerRuleException { MavenProject baseProject = createStandAloneProject(); addEmptyRepository( baseProject ); setupSortedProjects( Collections.singletonList( baseProject ) ); rule.execute( helper ); }
@Test public void testAllBannedWithEmptyPluginRepository() throws EnforcerRuleException { MavenProject baseProject = createStandAloneProject(); addEmptyPluginRepository( baseProject ); setupSortedProjects( Collections.singletonList( baseProject ) ); rule.execute( helper ); }