Search code examples
optaplanner

How to upgrade from Optaplanner 8.17 to 8.18 or 8.19


I have an optaplanner project which was working fine on 8.17.0.Final, but trying to upgrade to 8.18.0.Final or 8.19.9.Final results in the following error:

Exception in thread "main" java.lang.IllegalStateException: Constraint Streams requested via constraintProviderClass (class v2.constraints.AuctionConstraintProvider) but the supporting classes were not found on the classpath.
Maybe include org.optaplanner:optaplanner-constraint-streams dependency in your project?
        at org.optaplanner.core.impl.score.director.ScoreDirectorFactoryFactory.decideMultipleScoreDirectorFactories(ScoreDirectorFactoryFactory.java:130)
        at org.optaplanner.core.impl.score.director.ScoreDirectorFactoryFactory.buildScoreDirectorFactory(ScoreDirectorFactoryFactory.java:55)
        at org.optaplanner.core.impl.solver.DefaultSolverFactory.buildScoreDirectorFactory(DefaultSolverFactory.java:177)
        at org.optaplanner.core.impl.solver.DefaultSolverFactory.<init>(DefaultSolverFactory.java:87)
        at org.optaplanner.core.api.solver.SolverFactory.create(SolverFactory.java:122)
        at org.optaplanner.core.api.solver.SolverManager.create(SolverManager.java:85)
        at v2.WhitespaceServiceKt.main(WhitespaceService.kt:25)
        at v2.WhitespaceServiceKt.main(WhitespaceService.kt)

I'm not sure what I'm doing wrong. I don't see any notes in the changelog on migration, and it appears like the optaplanner-examples don't have anything special in their imports. I tried adding the optaplanner-constraint-streams project to my pom.xml, but that didn't fix anything either. Is there a new method for choosing an implementation of constraint streams? Where would I find information on this?

For reference, here are the relevant sections of my 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>ws_optimizer</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <optaplanner.version>8.17.0.Final</optaplanner.version>
        <kotlin.version>1.6.10</kotlin.version>
        <ktor.version>1.6.8</ktor.version>
        <serialization.version>1.3.2</serialization.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.optaplanner</groupId>
                <artifactId>optaplanner-bom</artifactId>
                <version>${optaplanner.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.optaplanner</groupId>
            <artifactId>optaplanner-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.optaplanner</groupId>
            <artifactId>optaplanner-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.3.3</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.2.11</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.11</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.36</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-stdlib-jdk8</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-reflect</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlinx</groupId>
            <artifactId>kotlinx-serialization-json</artifactId>
            <version>${serialization.version}</version>
        </dependency>
        <dependency>
            <groupId>org.ktorm</groupId>
            <artifactId>ktorm-core</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.ktorm</groupId>
            <artifactId>ktorm-support-postgresql</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-test</artifactId>
            <version>${kotlin.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.ktor</groupId>
            <artifactId>ktor-server-core</artifactId>
            <version>${ktor.version}</version>
        </dependency>
        <dependency>
            <groupId>io.ktor</groupId>
            <artifactId>ktor-server-netty</artifactId>
            <version>${ktor.version}</version>
        </dependency>
        <dependency>
            <groupId>io.ktor</groupId>
            <artifactId>ktor-serialization</artifactId>
            <version>${ktor.version}</version>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-maven-plugin</artifactId>
                <version>${kotlin.version}</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>test-compile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <sourceDirs>
                        <sourceDir>src/main/java</sourceDir>
                        <sourceDir>${project.basedir}/src/main/java</sourceDir>
                    </sourceDirs>
                    <compilerPlugins>
                        <plugin>kotlinx-serialization</plugin>
                    </compilerPlugins>
                    <jvmTarget>1.8</jvmTarget>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.jetbrains.kotlin</groupId>
                        <artifactId>kotlin-maven-serialization</artifactId>
                        <version>${kotlin.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>v2.WhitespaceServiceKt</mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

output of mvn dependency:tree --batch-mode | grep optaplanner:

[INFO] +- org.optaplanner:optaplanner-core:jar:8.19.0.Final:compile
[INFO] |  +- org.optaplanner:optaplanner-core-impl:jar:8.19.0.Final:compile
[INFO] |  +- org.optaplanner:optaplanner-constraint-streams:jar:8.19.0.Final:compile
[INFO] |  \- org.optaplanner:optaplanner-constraint-drl:jar:8.19.0.Final:compile
[INFO] +- org.optaplanner:optaplanner-test:jar:8.19.0.Final:test

Solution

  • If I had to guess, you are building an uberjar, and that uberjar does not include the META-INF/services files that are bundled in the optaplanner-constraint-streams JAR. This leads to OptaPlanner no longer being able to discover the CS implementation, which is now dynamically loaded.

    I am not familiar with the assembly plugin, but the maven-shade-plugin configuration would have needed the following extra config to make this work:

    <transformers>
       <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
    </transformers>
    

    This answer suggests a solution for the assembly plugin:

    <containerDescriptorHandlers>
        <containerDescriptorHandler>
            <handlerName>metaInf-services</handlerName>
        </containerDescriptorHandler>    
    </containerDescriptorHandlers>