You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:15:17 UTC

[sling-maven-launchpad-plugin] 34/49: SLING-1795 - adding Drools-based rules for rewriting bundle list. Includes sample rule which can be activated by running "mvn -P test-reactor-sling-bundles clean install" from the root project.

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag maven-launchpad-plugin-2.0.10
in repository https://gitbox.apache.org/repos/asf/sling-maven-launchpad-plugin.git

commit f300177d6413c9f242e4b33583db9d10a193971c
Author: Justin Edelson <ju...@apache.org>
AuthorDate: Wed Oct 13 19:52:37 2010 +0000

    SLING-1795 - adding Drools-based rules for rewriting bundle list. Includes sample rule which can be activated by running "mvn -P test-reactor-sling-bundles clean install" from the root project.
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/maven/maven-launchpad-plugin@1022267 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  5 ++
 .../projectsupport/AbstractBundleListMojo.java     | 63 +++++++++++++++++++++-
 .../AbstractLaunchpadStartingMojo.java             |  7 ---
 .../sling/maven/projectsupport/drools-globals.drl  | 24 +++++++++
 4 files changed, 90 insertions(+), 9 deletions(-)

diff --git a/pom.xml b/pom.xml
index aedcce8..8f226ae 100644
--- a/pom.xml
+++ b/pom.xml
@@ -147,5 +147,10 @@
             <artifactId>maven-filtering</artifactId>
             <version>1.0-beta-4</version>
         </dependency>
+        <dependency>
+            <groupId>org.drools</groupId>
+            <artifactId>drools-compiler</artifactId>
+            <version>5.1.1</version>
+        </dependency>
     </dependencies>
 </project>
diff --git a/src/main/java/org/apache/sling/maven/projectsupport/AbstractBundleListMojo.java b/src/main/java/org/apache/sling/maven/projectsupport/AbstractBundleListMojo.java
index a0c3897..cd66edc 100644
--- a/src/main/java/org/apache/sling/maven/projectsupport/AbstractBundleListMojo.java
+++ b/src/main/java/org/apache/sling/maven/projectsupport/AbstractBundleListMojo.java
@@ -30,6 +30,7 @@ import org.apache.maven.artifact.resolver.ArtifactResolutionException;
 import org.apache.maven.artifact.resolver.ArtifactResolver;
 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
 import org.apache.maven.artifact.versioning.VersionRange;
+import org.apache.maven.execution.MavenSession;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
@@ -39,6 +40,14 @@ import org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.BundleList;
 import org.apache.sling.maven.projectsupport.bundlelist.v1_0_0.io.xpp3.BundleListXpp3Reader;
 import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+import org.drools.KnowledgeBase;
+import org.drools.KnowledgeBaseFactory;
+import org.drools.builder.KnowledgeBuilder;
+import org.drools.builder.KnowledgeBuilderError;
+import org.drools.builder.KnowledgeBuilderFactory;
+import org.drools.builder.ResourceType;
+import org.drools.io.ResourceFactory;
+import org.drools.runtime.StatefulKnowledgeSession;
 
 public abstract class AbstractBundleListMojo extends AbstractMojo {
 
@@ -141,6 +150,18 @@ public abstract class AbstractBundleListMojo extends AbstractMojo {
      */
     private ArtifactResolver resolver;
 
+    /**
+     * @parameter
+     */
+    private File[] rewriteRuleFiles;
+
+    /**
+     * @parameter expression="${session}
+     * @required
+     * @readonly
+     */
+    protected MavenSession mavenSession;
+
     public final void execute() throws MojoFailureException, MojoExecutionException {
         try {
             initBundleList();
@@ -252,8 +273,9 @@ public abstract class AbstractBundleListMojo extends AbstractMojo {
         } else {
             bundleList = new BundleList();
             if (includeDefaultBundles) {
-                Artifact defBndListArtifact = getArtifact(defaultBundleList.getGroupId(), defaultBundleList.getArtifactId(),
-                        defaultBundleList.getVersion(), defaultBundleList.getType(), defaultBundleList.getClassifier());
+                Artifact defBndListArtifact = getArtifact(defaultBundleList.getGroupId(),
+                        defaultBundleList.getArtifactId(), defaultBundleList.getVersion(), defaultBundleList.getType(),
+                        defaultBundleList.getClassifier());
                 getLog().info("Using bundle list file from " + defBndListArtifact.getFile().getAbsolutePath());
                 bundleList = readBundleList(defBndListArtifact.getFile());
             }
@@ -273,6 +295,43 @@ public abstract class AbstractBundleListMojo extends AbstractMojo {
             }
         }
         initBundleList(bundleList);
+
+        rewriteBundleList(bundleList);
+    }
+
+    private void rewriteBundleList(BundleList bundleList) throws MojoExecutionException {
+        if (rewriteRuleFiles != null) {
+            KnowledgeBase knowledgeBase = createKnowledgeBase(rewriteRuleFiles);
+            StatefulKnowledgeSession session = knowledgeBase.newStatefulKnowledgeSession();
+            try {
+                session.setGlobal("mavenSession", mavenSession);
+                session.setGlobal("mavenProject", project);
+                session.insert(bundleList);
+                session.fireAllRules();
+            } finally {
+                session.dispose();
+            }
+        }
+    }
+
+    private KnowledgeBase createKnowledgeBase(File[] files) throws MojoExecutionException {
+        KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+        builder.add(ResourceFactory.newClassPathResource("drools-globals.drl", getClass()), ResourceType.DRL);
+        for (File file : files) {
+            getLog().info("Parsing rule file " + file.getAbsolutePath());
+            builder.add(ResourceFactory.newFileResource(file), ResourceType.DRL);
+        }
+        if (builder.hasErrors()) {
+            getLog().error("Rule errors:");
+            for (KnowledgeBuilderError error : builder.getErrors()) {
+                getLog().error(error.toString());
+            }
+            throw new MojoExecutionException("Unable to create rules. See log for details.");
+        }
+
+        KnowledgeBase base = KnowledgeBaseFactory.newKnowledgeBase();
+        base.addKnowledgePackages(builder.getKnowledgePackages());
+        return base;
     }
 
     private BundleList readBundleList(File file) throws IOException, XmlPullParserException {
diff --git a/src/main/java/org/apache/sling/maven/projectsupport/AbstractLaunchpadStartingMojo.java b/src/main/java/org/apache/sling/maven/projectsupport/AbstractLaunchpadStartingMojo.java
index bf44e40..29004a6 100644
--- a/src/main/java/org/apache/sling/maven/projectsupport/AbstractLaunchpadStartingMojo.java
+++ b/src/main/java/org/apache/sling/maven/projectsupport/AbstractLaunchpadStartingMojo.java
@@ -110,13 +110,6 @@ public abstract class AbstractLaunchpadStartingMojo extends AbstractBundleListMo
      */
     private MavenFileFilter mavenFileFilter;
 
-    /**
-     * @parameter expression="${session}"
-     * @required
-     * @readonly
-     */
-    private MavenSession mavenSession;
-
     private ResourceProvider resourceProvider = new ResourceProvider() {
 
         @Override
diff --git a/src/main/resources/org/apache/sling/maven/projectsupport/drools-globals.drl b/src/main/resources/org/apache/sling/maven/projectsupport/drools-globals.drl
new file mode 100644
index 0000000..58a5d6c
--- /dev/null
+++ b/src/main/resources/org/apache/sling/maven/projectsupport/drools-globals.drl
@@ -0,0 +1,24 @@
+# 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.
+
+# this file ensures that the globals are always defined, even if they're not needed
+
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.project.MavenProject;
+
+global MavenSession mavenSession
+global MavenProject mavenProject
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.