-
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 descriptors rule
- Loading branch information
Showing
11 changed files
with
461 additions
and
1 deletion.
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 |
148 changes: 148 additions & 0 deletions
148
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/Descriptors.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,148 @@ | ||
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.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 Descriptors extends AbstractNonCacheableEnforcerRule | ||
{ | ||
String descriptorRef; | ||
|
||
String descriptor; | ||
|
||
@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()) | ||
// Get the enforcer plugin's class resolver | ||
ClassRealm realm = helper.getComponent( MojoExecution.class ).getMojoDescriptor().getRealm(); | ||
ComponentConfigurator configurator = helper.getComponent( ComponentConfigurator.class, "basic" ); | ||
// Configure EnforcerDescriptor from the XML | ||
configurator.configureComponent( descriptor, toPlexusConfiguration( descriptorStream ), helper, realm ); | ||
return descriptor; | ||
} | ||
catch ( EnforcerRuleException e ) | ||
{ | ||
throw e; | ||
} | ||
catch ( Exception e ) | ||
{ | ||
throw new EnforcerRuleException( "Error while enforcing rules", e ); | ||
} | ||
} | ||
|
||
private InputStream resolveDescriptor( EnforcerRuleHelper helper ) throws EnforcerRuleException | ||
{ | ||
InputStream descriptorStream; | ||
if ( descriptorRef != null ) | ||
{ | ||
descriptorStream = Thread.currentThread().getContextClassLoader() | ||
.getResourceAsStream( "enforcer-rules/" + descriptorRef + ".xml" ); | ||
if ( descriptorStream == null ) | ||
{ | ||
throw new EnforcerRuleException( "Descriptor Ref '" + descriptorRef + "' not found" ); | ||
} | ||
} | ||
else if ( descriptor != null ) | ||
{ | ||
File descriptorFile = helper.alignToBaseDirectory( new File( descriptor ) ); | ||
try | ||
{ | ||
descriptorStream = Files.newInputStream( descriptorFile.toPath() ); | ||
} | ||
catch ( IOException e ) | ||
{ | ||
throw new EnforcerRuleException( "Could not read descriptor in " + descriptorFile, e ); | ||
} | ||
} | ||
else | ||
{ | ||
throw new EnforcerRuleException( "No descriptorRef or descriptor provided" ); | ||
} | ||
return descriptorStream; | ||
} | ||
|
||
private static PlexusConfiguration toPlexusConfiguration( InputStream descriptorStream ) | ||
throws XmlPullParserException, IOException | ||
{ | ||
return new XmlPlexusConfiguration( Xpp3DomBuilder.build( descriptorStream, "UTF-8" ) ); | ||
} | ||
|
||
public void setDescriptorRef( String descriptorRef ) | ||
{ | ||
this.descriptorRef = descriptorRef; | ||
} | ||
|
||
public String getDescriptorRef() | ||
{ | ||
return descriptorRef; | ||
} | ||
|
||
public void setDescriptor( String descriptor ) | ||
{ | ||
this.descriptor = descriptor; | ||
} | ||
|
||
public String getDescriptor() | ||
{ | ||
return descriptor; | ||
} | ||
} |
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 Descriptors} | ||
* | ||
* @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; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestDescriptors.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,58 @@ | ||
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 TestDescriptors | ||
{ | ||
@Test | ||
void shouldFailIfNoDescriptorIsSet() | ||
{ | ||
Descriptors rule = new Descriptors(); | ||
EnforcerRuleHelper helper = EnforcerTestUtils.getHelper(); | ||
assertThatExceptionOfType( EnforcerRuleException.class ).isThrownBy( () -> rule.execute( helper ) ) | ||
.withMessage( "No descriptorRef or descriptor provided" ); | ||
} | ||
|
||
@Test | ||
void shouldFailIfRefIsNotFound() | ||
{ | ||
Descriptors rule = new Descriptors(); | ||
rule.setDescriptorRef( "foo" ); | ||
EnforcerRuleHelper helper = EnforcerTestUtils.getHelper(); | ||
assertThatExceptionOfType( EnforcerRuleException.class ).isThrownBy( () -> rule.execute( helper ) ) | ||
.withMessage( "Descriptor Ref 'foo' not found" ); | ||
} | ||
|
||
@Test | ||
void shouldFailIfDescriptorIsNotFound() | ||
{ | ||
Descriptors rule = new Descriptors(); | ||
rule.setDescriptor( "blah.xml" ); | ||
EnforcerRuleHelper helper = EnforcerTestUtils.getHelper(); | ||
assertThatExceptionOfType( EnforcerRuleException.class ).isThrownBy( () -> rule.execute( helper ) ) | ||
.withMessageMatching( "Could not read descriptor in .*blah.xml" ); | ||
} | ||
} |
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,26 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!-- | ||
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. | ||
--> | ||
|
||
<enforcer> | ||
<rules> | ||
<AlwaysPass/> | ||
</rules> | ||
</enforcer> |
26 changes: 26 additions & 0 deletions
26
maven-enforcer-plugin/src/it/projects/descriptors-local-always-fail/enforcer-rules.xml
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,26 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!-- | ||
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. | ||
--> | ||
|
||
<enforcer> | ||
<rules> | ||
<AlwaysFail/> | ||
</rules> | ||
</enforcer> |
18 changes: 18 additions & 0 deletions
18
maven-enforcer-plugin/src/it/projects/descriptors-local-always-fail/invoker.properties
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,18 @@ | ||
# 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. | ||
|
||
invoker.buildResult = failure |
56 changes: 56 additions & 0 deletions
56
maven-enforcer-plugin/src/it/projects/descriptors-local-always-fail/pom.xml
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 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!-- | ||
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. | ||
--> | ||
|
||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>org.apache.maven.its.enforcer</groupId> | ||
<artifactId>test</artifactId> | ||
<version>1.0</version> | ||
|
||
<description> | ||
</description> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-enforcer-plugin</artifactId> | ||
<version>@project.version@</version> | ||
<executions> | ||
<execution> | ||
<id>test</id> | ||
<goals> | ||
<goal>enforce</goal> | ||
</goals> | ||
<configuration> | ||
<rules> | ||
<Descriptors> | ||
<descriptor>enforcer-rules.xml</descriptor> | ||
</Descriptors> | ||
</rules> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
Oops, something went wrong.