简体   繁体   English

将 Drools 作为胖 jar 运行(将 drools 编译为 jar)

[英]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.当我运行我的测试或从主方法运行我的应用程序时,drools 初始化很好并且一切正常。 BUT as soon as I compile my application to a jar file (as a fat jar), this code throws a NullPointerException .但是一旦我将我的应用程序编译为一个 jar 文件(作为一个胖 jar),这段代码就会抛出一个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.我还注意到,当从我的 ide 运行它时,ServiceDiscovery 会发现更多 kie.conf 文件。

Running it from IDE从 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运行编译的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我的pom文件

<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.提前致谢。

The problem is that when you compile your jar you are overriding the META-INF/kie.conf file.问题在于,当您编译 jar 时,您正在覆盖META-INF/kie.conf文件。

Rob pointed me to the correct solution. Rob 向我指出了正确的解决方案。 As your workaround did not work out adding this to the pom.xml solved my issue:由于您的解决方法没有解决,将它添加到pom.xml解决了我的问题:

<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>

For Gradle+Kotlin based build,对于基于 Gradle+Kotlin 的构建,

named<ShadowJar>("shadowJar") {
    transform(AppendingTransformer::class.java) {
        resource = "META-INF/kie.conf"
    }
}

The above code should work.上面的代码应该可以工作。

Add to kaanchabhau's answer, if this was gradle, you would have:添加到 kaanchabhau 的答案中,如果这是 gradle,您将拥有:

shadowJar {
    append 'META-INF/kie.conf'
    manifest {
        attributes 'Main-Class': mainClassName
    }
}

Ok so this is an ugly work around.好的,这是一个丑陋的解决方法。 The problem is that when you compile your jar you are overriding the META-INF/kie.conf file.问题在于,当您编译 jar 时,您正在覆盖 META-INF/kie.conf 文件。 So what I need to do is merge all the dependencies kie.conf file into one conf.所以我需要做的是将所有依赖项 kie.conf 文件合并到一个 conf 中。

This was the end result这是最终结果

org.drools.compiler.compiler.DecisionTableProvider = org.drools.decisiontable.DecisionTableProviderImpl
org.kie.api.KieServices = org.drools.compiler.kie.builder.impl.KieServicesImpl
org.kie.internal.builder.KnowledgeBuilderFactoryService = org.drools.compiler.builder.impl.KnowledgeBuilderFactoryServiceImpl
org.kie.api.internal.assembler.KieAssemblers = org.kie.internal.services.KieAssemblersImpl
org.kie.api.internal.runtime.KieRuntimes = org.kie.internal.services.KieRuntimesImpl
org.kie.api.internal.weaver.KieWeavers = org.kie.internal.services.KieWeaversImpl
org.kie.api.internal.runtime.beliefs.KieBeliefs = org.kie.internal.services.KieBeliefsImpl
org.kie.api.io.KieResources = org.drools.core.io.impl.ResourceFactoryServiceImpl
org.kie.api.marshalling.KieMarshallers = org.drools.core.marshalling.impl.MarshallerProviderImpl
org.kie.api.concurrent.KieExecutors = org.drools.core.concurrent.ExecutorProviderImpl

Please if anyone knows of a better way to do this post it here.如果有人知道更好的方法来做这个,请在​​此处发布。

Tried everything and the only maven plugin that works for me is the spring-boot-maven-plugin even if you don't use spring boot:尝试了所有方法,唯一对我有用的 maven 插件是 spring-boot-maven-plugin,即使您不使用 spring boot:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>2.3.3.RELEASE</version>
    <configuration>
        <mainClass>${main.class}</mainClass>
        <layout>ZIP</layout>
    </configuration>
</plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM