gastaldi commented on code in PR #180:
URL: https://github.com/apache/maven-enforcer/pull/180#discussion_r944013362


##########
enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/Descriptors.java:
##########
@@ -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:gasta...@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" );

Review Comment:
   @ppalaga now it's called `location` and it expects a file path or a resource 
from the classpath if it starts with `classpath:`
   ```xml
   <externalRules>
       <location>classpath:enforcer-rules/quarkus.xml</location>
   </externalRules>
   ```
   
   You don't specify the `classpath:` prefix, it will respect the 
implementation in 
`org.apache.maven.plugin.PluginParameterExpressionEvaluator#alignToBaseDirectory`:
   
   ```xml
   <externalRules>
       <location>classpath:enforcer-rules/quarkus.xml</location>
   </externalRules>
   ```
   
   
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@maven.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to