英文:
Clojure (clara rule) file not generated when gradle build
问题
我有一个Spring应用程序,集成了一个Clara规则引擎(Clojure)文件,该文件从一个Java类中触发。使用Gradle构建应用程序时,Clojure文件未作为JAR的一部分生成。
因此,在运行该JAR时,会抛出以下异常:
Caused by: java.io.FileNotFoundException: 无法在类路径中找到au/com/acme/mti/mec/runtime/rules/mec__init.class或au/com/acme/mti/mec/runtime/rules/mec.clj。
在构建/生成JAR时,最佳方法(或至少一种方法)是使Gradle生成clj文件。我已经在build.gradle文件中包含了一个任务,用于将clj文件从src路径复制到build路径。它会将文件复制到build路径下,但不会包含在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")
}
}
英文:
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")
}
}
答案1
得分: 1
将Clojure文件(Clara规则)放在一个地方,使它们最终位于JAR内的正确位置,将使得复制任务变得不再需要(至少从问题中我们可以看出在build.gradle
的哪部分中没有用于构建JAR)。
- 将文件从
src/main/resources/au.com.acme.mti.mec.runtime.rules
移动到src/main/resources/au/com/acme/mti/mec/runtime/rules
- 摆脱
copyRules
任务(以及它的dependsOn
)
编辑:
添加了一个存储库,展示了一个将clj文件放入资源中的最小工作示例 https://github.com/christoph-frick/try-java-clara
英文:
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.
- Move the files from
src/main/resources/au.com.acme.mti.mec.runtime.rules
tosrc/main/resources/au/com/acme/mti/mec/runtime/rules
- Get rid of the
copyRules
task (and it'sdependsOn
)
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论