【问题标题】:Clojure (clara rule) file not generated when gradle buildgradle build 时未生成 Clojure (clara rule) 文件
【发布时间】:2020-08-20 20:07:38
【问题描述】:

我有一个 Spring 应用程序,它集成了从 java 类触发的 clara 规则引擎 (clojure) 文件。使用 gradle 构建应用时,不会生成 clojure 文件作为 JAR 的一部分。

所以,当运行 jar 时,它会抛出以下异常:

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.

在构建/生成 jar 时,让 gradle 生成 clj 文件的最佳方式(或至少一种方式)是什么?

我已经在 build.gradle 文件中包含一个任务,将 clj 文件从 src 路径复制到构建路径。它会复制构建路径下的文件,但不会在 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")
    }
}

【问题讨论】:

  • 您是否尝试过将 .clj 文件直接放入 src/main/resources/au/com/acme/mti/mec/runtime/rules 中?否则看起来你也只依赖copyRules 任务进行测试?而且您正在谈论构建一个 jar - 它在您的测试中有效吗?
  • 是的,这就是 gradle 任务的作用:将文件从 src/main/resources/au.com.acme.mti.mec.runtime.rules 复制到 build/classes/java/main/au/com/acme/mti/mec/runtime/rules
  • 这并没有回答任何一个问题:你为什么需要这个复制任务?为什么不首先将文件放在正确的位置呢?为什么没有执行构建jar的复制任务?
  • 我已尝试将规则文件放入资源中。完成后,.clj 文件被打包在资源中,但随后出现的问题是运行 clara 的 java 程序无法找到位于资源中的规则文件。​ 复制任务的添加是为了尝试解决这个问题。这显然不能解决问题,这就是为什么我要联系 SO
  • 我在答案中添加了一个项目,表明不需要专门的复制任务

标签: java spring gradle clojure


【解决方案1】:

将 Clojure 文件(Clara 规则)放在一个地方,它们最终在 JAR 中的正确位置,会使复制任务过时(它不用于构建 JAR - 至少从部分我们可以在问题中看到build.gradle

  1. 将文件从src/main/resources/au.com.acme.mti.mec.runtime.rules 移动到src/main/resources/au/com/acme/mti/mec/runtime/rules
  2. 摆脱copyRules 任务(它是dependsOn

编辑:

添加了一个显示最小工作示例的存储库,它将 clj 文件放入资源https://github.com/christoph-frick/try-java-clara

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 2023-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    相关资源
    最近更新 更多