-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MENFORCER-422] Added externalRules rule
Co-authored-by: Peter Palaga <[email protected]>
- Loading branch information
Showing
14 changed files
with
552 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ target | |
.svn | ||
*.iml | ||
.checkstyle | ||
|
||
.DS_Store |
42 changes: 42 additions & 0 deletions
42
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/EnforcerDescriptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.apache.maven.plugins.enforcer; | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import org.apache.maven.enforcer.rule.api.EnforcerRule; | ||
|
||
/** | ||
* An enforcer rules descriptor used by {@link ExternalRules} | ||
* | ||
* @author <a href="mailto:[email protected]">George Gastaldi</a> | ||
*/ | ||
public class EnforcerDescriptor | ||
{ | ||
EnforcerRule[] rules; | ||
|
||
public EnforcerRule[] getRules() | ||
{ | ||
return rules; | ||
} | ||
|
||
public void setRules( EnforcerRule[] rules ) | ||
{ | ||
this.rules = rules; | ||
} | ||
} |
152 changes: 152 additions & 0 deletions
152
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/ExternalRules.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package org.apache.maven.plugins.enforcer; | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import org.apache.maven.enforcer.rule.api.EnforcerRule; | ||
import org.apache.maven.enforcer.rule.api.EnforcerRuleException; | ||
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; | ||
import org.apache.maven.plugin.MojoExecution; | ||
import org.codehaus.plexus.classworlds.realm.ClassRealm; | ||
import org.codehaus.plexus.component.configurator.ComponentConfigurator; | ||
import org.codehaus.plexus.component.repository.exception.ComponentLookupException; | ||
import org.codehaus.plexus.configuration.PlexusConfiguration; | ||
import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration; | ||
import org.codehaus.plexus.util.xml.Xpp3DomBuilder; | ||
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
|
||
/** | ||
* An enforcer rule that will invoke rules from an external resource | ||
* | ||
* @author <a href="mailto:[email protected]">George Gastaldi</a> | ||
*/ | ||
public class ExternalRules extends AbstractNonCacheableEnforcerRule | ||
{ | ||
private static final String LOCATION_PREFIX_CLASSPATH = "classpath:"; | ||
|
||
/** | ||
* The external rules location. If it starts with "classpath:", the resource is read from the classpath. | ||
* Otherwise, it is handled as a filesystem path, either absolute, or relative to <code>${project.basedir}</code> | ||
*/ | ||
String location; | ||
|
||
public ExternalRules() | ||
{ | ||
} | ||
|
||
public ExternalRules( String location ) | ||
{ | ||
this.location = location; | ||
} | ||
|
||
@Override | ||
public void execute( EnforcerRuleHelper helper ) throws EnforcerRuleException | ||
{ | ||
// Find descriptor | ||
EnforcerDescriptor enforcerDescriptor = getEnforcerDescriptor( helper ); | ||
for ( EnforcerRule rule : enforcerDescriptor.getRules() ) | ||
{ | ||
rule.execute( helper ); | ||
} | ||
} | ||
|
||
/** | ||
* Resolve the {@link EnforcerDescriptor} based on the provided {@link #descriptor} or {@link #descriptorRef} | ||
* | ||
* @param helper used to build the {@link EnforcerDescriptor} | ||
* @return an {@link EnforcerDescriptor} for this rule | ||
* @throws EnforcerRuleException if any failure happens while reading the descriptor | ||
*/ | ||
EnforcerDescriptor getEnforcerDescriptor( EnforcerRuleHelper helper ) | ||
throws EnforcerRuleException | ||
{ | ||
try ( InputStream descriptorStream = resolveDescriptor( helper ) ) | ||
{ | ||
EnforcerDescriptor descriptor = new EnforcerDescriptor(); | ||
// To get configuration from the enforcer-plugin mojo do: | ||
//helper.evaluate(helper.getComponent(MojoExecution.class).getConfiguration().getChild("fail").getValue()) | ||
// Configure EnforcerDescriptor from the XML | ||
ComponentConfigurator configurator = helper.getComponent( ComponentConfigurator.class, "basic" ); | ||
configurator.configureComponent( descriptor, toPlexusConfiguration( descriptorStream ), helper, | ||
getClassRealm( helper ) ); | ||
return descriptor; | ||
} | ||
catch ( EnforcerRuleException e ) | ||
{ | ||
throw e; | ||
} | ||
catch ( Exception e ) | ||
{ | ||
throw new EnforcerRuleException( "Error while enforcing rules", e ); | ||
} | ||
} | ||
|
||
private InputStream resolveDescriptor( EnforcerRuleHelper helper ) | ||
throws ComponentLookupException, EnforcerRuleException | ||
{ | ||
InputStream descriptorStream; | ||
if ( location != null ) | ||
{ | ||
if ( location.startsWith( LOCATION_PREFIX_CLASSPATH ) ) | ||
{ | ||
String classpathLocation = location.substring( LOCATION_PREFIX_CLASSPATH.length() ); | ||
ClassLoader classRealm = getClassRealm( helper ); | ||
descriptorStream = classRealm.getResourceAsStream( classpathLocation ); | ||
if ( descriptorStream == null ) | ||
{ | ||
throw new EnforcerRuleException( "Location '" + classpathLocation + "' not found in classpath" ); | ||
} | ||
} | ||
else | ||
{ | ||
File descriptorFile = helper.alignToBaseDirectory( new File( location ) ); | ||
try | ||
{ | ||
descriptorStream = Files.newInputStream( descriptorFile.toPath() ); | ||
} | ||
catch ( IOException e ) | ||
{ | ||
throw new EnforcerRuleException( "Could not read descriptor in " + descriptorFile, e ); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
throw new EnforcerRuleException( "No location provided" ); | ||
} | ||
return descriptorStream; | ||
} | ||
|
||
private static PlexusConfiguration toPlexusConfiguration( InputStream descriptorStream ) | ||
throws XmlPullParserException, IOException | ||
{ | ||
return new XmlPlexusConfiguration( Xpp3DomBuilder.build( descriptorStream, "UTF-8" ) ); | ||
} | ||
|
||
private ClassRealm getClassRealm( EnforcerRuleHelper helper ) throws ComponentLookupException | ||
{ | ||
return helper.getComponent( MojoExecution.class ).getMojoDescriptor().getRealm(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
~~ Licensed to the Apache Software Foundation (ASF) under one | ||
~~ or more contributor license agreements. See the NOTICE file | ||
~~ distributed with this work for additional information | ||
~~ regarding copyright ownership. The ASF licenses this file | ||
~~ to you under the Apache License, Version 2.0 (the | ||
~~ "License"); you may not use this file except in compliance | ||
~~ with the License. You may obtain a copy of the License at | ||
~~ | ||
~~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~~ | ||
~~ Unless required by applicable law or agreed to in writing, | ||
~~ software distributed under the License is distributed on an | ||
~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
~~ KIND, either express or implied. See the License for the | ||
~~ specific language governing permissions and limitations | ||
~~ under the License. | ||
|
||
------ | ||
External Rules | ||
------ | ||
George Gastaldi | ||
------ | ||
August 2022 | ||
------ | ||
|
||
External Rules | ||
|
||
This rule will evaluate rules from an external resource. It is useful for reusing enforcer rules from other dependencies. | ||
|
||
|
||
Sample Plugin Configuration: | ||
|
||
+---+ | ||
<project> | ||
[...] | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-enforcer-plugin</artifactId> | ||
<version>${project.version}</version> | ||
<dependencies> | ||
<!-- Dependency containing the enforcer/rules.xml file --> | ||
<dependency> | ||
<groupId>org.foo</groupId> | ||
<artifactId>foobar-rules</artifactId> | ||
<version>1.0.0</version> | ||
</dependency> | ||
</dependencies> | ||
<executions> | ||
<execution> | ||
<id>enforce</id> | ||
<goals> | ||
<goal>enforce</goal> | ||
</goals> | ||
<configuration> | ||
<rules> | ||
<ExternalRules> | ||
<!-- if referencing a file, remove the 'classpath:' prefix --> | ||
<location>classpath:enforcer/rules.xml</location> | ||
</ExternalRules> | ||
</rules> | ||
<fail>true</fail> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
[...] | ||
</project> | ||
+---+ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestExternalRules.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.apache.maven.plugins.enforcer; | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import org.apache.maven.enforcer.rule.api.EnforcerRuleException; | ||
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
||
public class TestExternalRules | ||
{ | ||
@Test | ||
void shouldFailIfNoLocationIsSet() | ||
{ | ||
ExternalRules rule = new ExternalRules(); | ||
EnforcerRuleHelper helper = EnforcerTestUtils.getHelper(); | ||
assertThatExceptionOfType( EnforcerRuleException.class ).isThrownBy( () -> rule.execute( helper ) ) | ||
.withMessage( "No location provided" ); | ||
} | ||
|
||
@Test | ||
void shouldFailIfClasspathLocationIsNotFound() | ||
{ | ||
ExternalRules rule = new ExternalRules("classpath:foo"); | ||
EnforcerRuleHelper helper = EnforcerTestUtils.getHelper(); | ||
assertThatExceptionOfType( EnforcerRuleException.class ).isThrownBy( () -> rule.execute( helper ) ) | ||
.withMessage( "Location 'foo' not found in classpath" ); | ||
} | ||
|
||
@Test | ||
void shouldFailIfFileLocationIsNotFound() | ||
{ | ||
ExternalRules rule = new ExternalRules("blah.xml"); | ||
EnforcerRuleHelper helper = EnforcerTestUtils.getHelper(); | ||
assertThatExceptionOfType( EnforcerRuleException.class ).isThrownBy( () -> rule.execute( helper ) ) | ||
.withMessageMatching( "Could not read descriptor in .*blah.xml" ); | ||
} | ||
} |
Oops, something went wrong.