Search code examples
javadroolsrules

Running Drools as a fat jar(Compile drools into jar)


Problem:

When I runs my test or run my application from the main method drools initialize fine and every thing works. BUT as soon as I compile my application to a jar file (as a fat jar), this code throws a NullPointerException.

KieServices ks = KieServices.Factory.get();
kieContainer = ks.getKieClasspathContainer(); // ks is null

I have also notice that when running it from my ide the ServiceDiscovery finds a lot more kie.conf files.

Running it from IDE

2018-05-09 18:18:12,219 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Loading kie.conf from  
2018-05-09 18:18:12,221 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Discovered kie.conf url=jar:file:/C:/Users/alece/.m2/repository/org/drools/drools-decisiontables/7.6.0.Final/drools-decisiontables-7.6.0.Final.jar!/META-INF/kie.conf 
2018-05-09 18:18:12,307 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Adding Service org.drools.decisiontable.DecisionTableProviderImpl

2018-05-09 18:18:12,307 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Loading kie.conf from  
2018-05-09 18:18:12,308 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Discovered kie.conf url=jar:file:/C:/Users/alece/.m2/repository/org/kie/kie-internal/7.6.0.Final/kie-internal-7.6.0.Final.jar!/META-INF/kie.conf 
2018-05-09 18:18:12,311 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Adding Service org.kie.internal.services.KieWeaversImpl

2018-05-09 18:18:12,313 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Adding Service org.kie.internal.services.KieBeliefsImpl

2018-05-09 18:18:12,314 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Adding Service org.kie.internal.services.KieAssemblersImpl

2018-05-09 18:18:12,315 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Adding Service org.kie.internal.services.KieRuntimesImpl

2018-05-09 18:18:12,316 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Loading kie.conf from  
2018-05-09 18:18:12,316 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Discovered kie.conf url=jar:file:/C:/Users/alece/.m2/repository/org/drools/drools-core/7.6.0.Final/drools-core-7.6.0.Final.jar!/META-INF/kie.conf 
2018-05-09 18:18:12,319 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Adding Service org.drools.core.io.impl.ResourceFactoryServiceImpl

2018-05-09 18:18:12,323 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Adding Service org.drools.core.marshalling.impl.MarshallerProviderImpl

2018-05-09 18:18:12,335 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Adding Service org.drools.core.concurrent.ExecutorProviderImpl

2018-05-09 18:18:12,335 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Loading kie.conf from  
2018-05-09 18:18:12,336 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Discovered kie.conf url=jar:file:/C:/Users/alece/.m2/repository/org/drools/drools-compiler/7.6.0.Final/drools-compiler-7.6.0.Final.jar!/META-INF/kie.conf 
2018-05-09 18:18:12,348 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Adding Service org.drools.compiler.kie.builder.impl.KieServicesImpl

2018-05-09 18:18:12,357 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Adding Service org.drools.compiler.builder.impl.KnowledgeBuilderFactoryServiceImpl

Running compiled jar

2018-05-09 18:14:02,771 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Loading kie.conf from
2018-05-09 18:14:02,772 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Discovered kie.conf url=jar:file:/C:/Users/alece/git-idea/directpay-monthend/target/monthend-1.0.0.jar!/META-INF/kie.conf
2018-05-09 18:14:02,773 [monthEnd] INFO  org.kie.api.internal.utils.ServiceDiscoveryImpl - Adding Service org.drools.decisiontable.DecisionTableProviderImpl

My pom file

<properties>
      <drools.version>7.6.0.Final</drools.version>
</properties>
        <!-- Drools -->
        <dependency>
            <groupId>org.kie</groupId>
            <artifactId>kie-api</artifactId>
            <version>${drools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-decisiontables</artifactId>
            <version>${drools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-core</artifactId>
            <version>${drools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-compiler</artifactId>
            <version>${drools.version}</version>
        </dependency>

Thanks in advance.


Solution

  • The problem is that when you compile your jar you are overriding the META-INF/kie.conf file.

    Rob pointed me to the correct solution. As your workaround did not work out adding this to the pom.xml solved my issue:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                <resource>META-INF/kie.conf</resource>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>