ExceptionInInitializerError при экспорте OptaPlanner в качестве jar - PullRequest
0 голосов
/ 28 февраля 2020

Я экспортировал свой проект OptaPlanner в банку. Когда я запускаю флягу, я получаю следующую ошибку:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.kie.api.internal.utils.ServiceRegistry.getInstance(ServiceRegistry.java:27)
    at org.kie.api.KieServices$Factory$LazyHolder.<clinit>(KieServices.java:341)
    at org.kie.api.KieServices$Factory.get(KieServices.java:348)
    at org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildDroolsScoreDirectorFactory(ScoreDirectorFactoryConfig.java:639)
    at org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildScoreDirectorFactory(ScoreDirectorFactoryConfig.java:463)
    at org.optaplanner.core.config.solver.SolverConfig.buildScoreDirectorFactory(SolverConfig.java:606)
    at org.optaplanner.core.config.solver.SolverConfig.buildSolver(SolverConfig.java:514)
    at org.optaplanner.core.impl.solver.DefaultSolverFactory.buildSolver(DefaultSolverFactory.java:49)
    at org.optaplanner.examples.common.app.CommonApp.createSolutionBusiness(CommonApp.java:124)
    at org.optaplanner.examples.common.app.CommonApp.init(CommonApp.java:114)
    at org.optaplanner.examples.common.app.CommonApp.init(CommonApp.java:110)
    at org.optaplanner.examples.projectjobscheduling.app.ProjectJobSchedulingApp.main(ProjectJobSchedulingApp.java:36)
Caused by: java.lang.RuntimeException: Child services [org.kie.api.internal.assembler.KieAssemblers] have no parent
    at org.kie.api.internal.utils.ServiceDiscoveryImpl.buildMap(ServiceDiscoveryImpl.java:197)
    at org.kie.api.internal.utils.ServiceDiscoveryImpl.getServices(ServiceDiscoveryImpl.java:95)
    at org.kie.api.internal.utils.ServiceRegistryImpl.<init>(ServiceRegistryImpl.java:36)
    at org.kie.api.internal.utils.ServiceRegistryImpl$LazyHolder.<clinit>(ServiceRegistryImpl.java:32)
    ... 12 more

Я видел, что у других была такая же проблема, и посоветовал им добавить следующий код в файл pom. xml file:

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

Для меня это не решило мою проблему, и я, попробовав все различные изменения в моем файле pom. xml, обнаружил, что немного отчаялся, как решить эту проблему.

Мой оригинальный файл pom. xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.optaplanner</groupId>
    <artifactId>optaplanner</artifactId>
    <version>7.29.0.Final</version>
  </parent>

  <artifactId>optaplanner-examples</artifactId>

  <name>OptaPlanner examples</name>
  <description>
    OptaPlanner solves planning problems.
    This lightweight, embeddable planning engine implements powerful and scalable algorithms
    to optimize business resource scheduling and planning.

    This module contains the examples which demonstrate how to use it in a normal Java application.
  </description>
  <url>https://www.optaplanner.org</url>

  <properties>
    <java.module.name>org.optaplanner.examples</java.module.name>
  </properties>

  <repositories>
    <!-- Included so the examples sources in the distribution zip build out-of-the-box with maven -->
    <repository>
      <id>jboss-public-repository-group</id>
      <name>JBoss Public Repository Group</name>
      <url>https://repository.jboss.org/nexus/content/groups/public/</url>
      <layout>default</layout>
      <releases>
        <enabled>true</enabled>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>true</enabled>
        <updatePolicy>daily</updatePolicy>
      </snapshots>
    </repository>
  </repositories>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <configuration>
            <forkCount>0.5C</forkCount>
            <systemPropertyVariables>
              <java.awt.headless>true</java.awt.headless>
            </systemPropertyVariables>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <!-- WARNING: This configuration must be run with "mvn exec:java" not "mvn exec:exec". -->
          <!-- It is impossible to write a configuration that is compatible with both exec:java and exec:exec -->
          <configuration>
            <mainClass>org.optaplanner.examples.app.OptaPlannerExamplesApp</mainClass>
            <arguments>
              <argument>-Xms256m</argument>
              <!-- Most examples run (potentially slower) with max heap of 128 MB (so -Xmx128m), but 1 example's dataset requires 1.5 GB -->
              <argument>-Xmx1536m</argument>
              <argument>-server</argument>
            </arguments>
          </configuration>
        </plugin>
        <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <dependencies>
    <!-- Internal dependencies -->
    <dependency>
      <groupId>org.optaplanner</groupId>
      <artifactId>optaplanner-core</artifactId>
    </dependency>
    <dependency>
      <groupId>org.optaplanner</groupId>
      <artifactId>optaplanner-persistence-common</artifactId>
    </dependency>
    <dependency><!-- Most examples use the XStream integration -->
      <groupId>org.optaplanner</groupId>
      <artifactId>optaplanner-persistence-xstream</artifactId>
    </dependency>
    <dependency><!-- TODO add examples that use the JAXB integration -->
      <groupId>org.optaplanner</groupId>
      <artifactId>optaplanner-persistence-jaxb</artifactId>
    </dependency>
    <dependency><!-- TODO add examples that use the jackson integration -->
      <groupId>org.optaplanner</groupId>
      <artifactId>optaplanner-persistence-jackson</artifactId>
    </dependency>
    <dependency>
      <groupId>org.optaplanner</groupId>
      <artifactId>optaplanner-benchmark</artifactId>
    </dependency>
    <dependency>
      <groupId>org.optaplanner</groupId>
      <artifactId>optaplanner-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.kie</groupId>
      <artifactId>kie-api</artifactId>
    </dependency>
    <dependency>
      <groupId>org.drools</groupId>
      <artifactId>drools-decisiontables</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.drools</groupId>
      <artifactId>drools-canonical-model</artifactId>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.drools</groupId>
      <artifactId>drools-model-compiler</artifactId>
      <scope>runtime</scope>
    </dependency>
    <!-- External dependencies -->
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
    </dependency>
    <dependency>
      <groupId>org.jfree</groupId>
      <artifactId>jfreechart</artifactId>
    </dependency>
    <!-- Common utils -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
    </dependency>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
    </dependency>
    <!-- Logging -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <scope>runtime</scope>
    </dependency>
    <!-- XML -->
    <dependency>
      <groupId>com.thoughtworks.xstream</groupId>
      <artifactId>xstream</artifactId>
    </dependency>

    <!-- Examples only dependencies. -->
    <!--
      WARNING: every examples only dependency must be properly dealt with in
      optaplanner-distribution/src/main/assembly/assembly-optaplanner.xml
    -->
    <!-- Converters -->
    <dependency>
      <groupId>org.jdom</groupId>
      <artifactId>jdom</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <exclusions>
        <exclusion>
          <!-- Collides with 'javax.xml.stream:stax-api' brought in by 'org.drools:drools-decisiontables'. -->
          <groupId>stax</groupId>
          <artifactId>stax-api</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.glassfish</groupId>
      <artifactId>javax.json</artifactId>
    </dependency>
  </dependencies>

</project>

Содержимое моего файла k ie .conf:

org.kie.api.internal.assembler.KieAssemblers = +org.optaplanner.core.impl.solver.kie.KieSolverAssemblerService

Кто-нибудь есть? кто знает как решить эту проблему?

1 Ответ

1 голос
/ 08 марта 2020

После добавления следующего в раздел <build><plugins>...</plugins></build> вашего POM я смог создать и запустить JAR без проблем:

<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.ManifestResourceTransformer">          
        <mainClass>org.optaplanner.examples.app.OptaPlannerExamplesApp</mainClass>
          </transformer>
          <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
            <resource>META-INF/kie.conf</resource>
          </transformer>
        </transformers>
      </configuration>
    </execution>
  </executions>
</plugin>

Это изменение фактически создает затененный JAR (который ваш оригинальный версия не работает).

...