Skip to content

Commit

Permalink
[MENFORCER-422] Added descriptors rule
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Aug 11, 2022
1 parent ae93fa8 commit 606b254
Show file tree
Hide file tree
Showing 11 changed files with 461 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ target
.svn
*.iml
.checkstyle

.DS_Store
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:gastaldi@apache.org">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;
}
}
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:gastaldi@apache.org">George Gastaldi</a>
*/
public class EnforcerDescriptor
{
EnforcerRule[] rules;

public EnforcerRule[] getRules()
{
return rules;
}

public void setRules( EnforcerRule[] rules )
{
this.rules = rules;
}
}
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" );
}
}
26 changes: 26 additions & 0 deletions enforcer-rules/src/test/resources/enforcer-rules/pass.xml
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>
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>
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
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>

0 comments on commit 606b254

Please sign in to comment.