Search code examples
javaspringgradleclojure

Clojure (clara rule) file not generated when gradle build


I have a Spring application that integrates a clara rule engine (clojure) file that is triggered from a java class. When building the app with gradle, the clojure file is not generated as part of the JAR.

So, when running ther jar, it throws the following exception:

Caused by: java.io.FileNotFoundException: Could not locate au/com/acme/mti/mec/runtime/rules/mec__init.class or au/com/acme/mti/mec/runtime/rules/mec.clj on classpath.

What is the best way (or at least a way) to make gradle generates the clj file when building/generating the jar?

I have already include a task in the build.gradle file to copy the clj file from src path to the build path. It copies the file under the build path, but it does not in the jar.

build.gradle:

plugins {
    id 'org.springframework.boot' version '2.3.1.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
    id 'jacoco'
}

group = 'au.com.acme.mti.mec'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
    mavenLocal()
    maven {
        url "http://clojars.org/repo"
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.clojure:clojure:1.9.0'
    implementation 'com.cerner:clara-rules:0.20.0'
    implementation 'org.projectlombok:lombok'
    
    }
    
}

task copyRules(type: Copy){
    from 'src/main/resources/au.com.acme.mti.mec.runtime.rules/'
    into 'build/classes/java/main/au/com/acme/mti/mec/runtime/rules/'
}

test {
    test.dependsOn copyRules
    useJUnitPlatform()
    finalizedBy jacocoTestReport
}

jacocoTestReport {
    reports {
        xml.enabled false
        csv.enabled false
        html.destination file("${buildDir}/jacocoHtml")
    }
}

Solution

  • Putting the Clojure files (the Clara rules) in a place, where they end up in the correct place inside the JAR, would make the copy task obsolute (which is not used for building the JAR -- at least from the part of the build.gradle we can see in the question.

    1. Move the files from src/main/resources/au.com.acme.mti.mec.runtime.rules to src/main/resources/au/com/acme/mti/mec/runtime/rules
    2. Get rid of the copyRules task (and it's dependsOn)

    edit:

    Added a repository that shows a minimal working example, that puts the clj files just into resources https://github.com/christoph-frick/try-java-clara