尝试使用 gradle 内的 ant 任务编译 jrxml 时,未生成 jasper 文件。在 gradle 控制台中可以看到以下错误。请就此提供指点。以下是 ant 任务的 gradle 构建文件的错误。
注意:所有需要的jasper和其他依赖jar都放在libs目录下
错误:
2020-10-28T15:09:51.520+0000 [INFO] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:jrc] ERROR: Could not compile report [/builds/DFSDocker.Admin/rhel_dec_release/compileux/customjrxml/src/main/jasperreports/UBBDFail.jrxml]. Exception: net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
2020-10-28T15:09:51.520+0000 [INFO] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:jrc] org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
2020-10-28T15:09:51.520+0000 [INFO] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:jrc] calculator_UBBDFail_1603897790979_43679: 24: unable to resolve class net.sf.jasperreports.compilers.GroovyEvaluator
2020-10-28T15:09:51.520+0000 [INFO] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:jrc] @ line 24, column 1.
2020-10-28T15:09:51.520+0000 [INFO] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:jrc] calculator_UBBDFail_1603897790979_43679: 34: unable to resolve class JRFillParameter
2020-10-28T15:09:51.520+0000 [INFO] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:jrc] @ line 34, column 5.
2020-10-28T15:09:51.520+0000 [INFO] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:jrc] calculator_UBBDFail_1603897790979_43679: 35: unable to resolve class JRFillParameter
2020-10-28T15:09:51.520+0000 [INFO] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:jrc] @ line 35, column 5.
2020-10-28T15:09:51.520+0000 [INFO] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:jrc] calculator_UBBDFail_1603897790979_43679: 36: unable to resolve class JRFillParameter
2020-10-28T15:09:51.520+0000 [INFO] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:jrc] @ line 36, column 5.
2020-10-28T15:09:51.521+0000 [INFO] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:jrc] calculator_UBBDFail_1603897790979_43679: 37: unable to resolve class JRFillParameter
2020-10-28T15:09:51.521+0000 [INFO] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:jrc] @ line 37, column 5.
2020-10-28T15:09:51.521+0000 [INFO] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:jrc] calculator_UBBDFail_1603897790979_43679: 38: unable to resolve class JRFillParameter
2020-10-28T15:09:51.521+0000 [INFO] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:jrc] @ line 38, column 5.
2020-10-28T15:09:51.521+0000 [INFO] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:jrc] calculator_UBBDFail_1603897790979_43679: 39: unable to resolve class JRFillParameter
2020-10-28T15:09:51.521+0000 [INFO] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:jrc] @ line 39, column 5.
build.gradle:
apply plugin: 'java'
repositories {
mavenCentral()
flatDir {
println 'in repositories'
dirs 'libs'
}
}
configurations.create('jasperreports')
configurations.jasperreports {
transitive = true
}
dependencies {
jasperreports fileTree(dir: 'libs',include: '*.jar')
}
task compileJasperJava {
def jasperSourceDir = file('customjrxml/src/main/jasperreports')
def jasperTargetDir = file('customjrxml/build/classes/main/jasperreports')
def compiler = file('customjrxml/build/classes/main/jasperreports')
ant {
taskdef(name: 'jrc', classname: 'net.sf.jasperreports.ant.JRAntCompileTask', classpath: configurations.jasperreports.asPath)
jasperTargetDir.mkdirs()
jrc(srcdir: jasperSourceDir, destdir:jasperTargetDir,compiler:'net.sf.jasperreports.compilers.JRGroovyCompiler') {
include(name:'**/*.jrxml')
}
}
}
classes.dependsOn compileJasperJava
在 build.gradle 下面添加突出显示的(类路径块 - 使用前删除 **)解决了 jasper 生成问题。
apply plugin: 'java'
repositories {
mavenCentral()
flatDir {
println 'in repositories'
dirs 'libs'
}
}
configurations.create('jasperreports')
configurations.jasperreports {
transitive = true
}
dependencies {
jasperreports fileTree(dir: 'libs',include: '*.jar')
}
task compileJasperJava {
def jasperSourceDir = file('customjrxml/src/main/jasperreports')
def jasperTargetDir = file('customjrxml/build/classes/main/jasperreports')
ant {
taskdef(name: 'jrc', classname: 'net.sf.jasperreports.ant.JRAntCompileTask', classpath: configurations.jasperreports.asPath)
jasperTargetDir.mkdirs()
jrc(srcdir: jasperSourceDir, destdir:jasperTargetDir) {
include(name:'**/*.jrxml')
**classpath {
pathElement(path: configurations.jasperreports.asPath)
}**
}
}
}
classes.dependsOn compileJasperJava
参考文章:http://gradle.1045684.n5.nabble.com/Classpath-issue-with-jasperreports-taskdef-td3275141.html
上面的 anwser 几乎对我有用,但由于我必须做出一些更改,所以我决定在这里分享我的解决方案。
我下面的代码需要在您的
build.gradle.kts
中。我删除了我的特定代码以避免任何混淆,因为我正在使用 Spring Boot 插件和一些其他构建约定。
这是我必须使用 gradle kotlin 语法添加到我的
build.gradle.kts
中的内容:
val jasperreports: Configuration by configurations.creating
dependencies {
jasperreports("net.sf.jasperreports:jasperreports:$jasperVersion")
// I believe JR version 7 started to generate multiple packages
// Because of this, you need to add this dependency as well to find ant task
jasperreports("net.sf.jasperreports:jasperreports-ant:$jasperVersion")
// Don't forget to add other needed dependencies for your report
jasperreports("net.sf.jasperreports:jasperreports-barbecue:$jasperVersion")
jasperreports("net.sf.jasperreports:jasperreports-barcode4j:$jasperVersion")
// This dependency is needed for compilation.
jasperreports("net.sf.jasperreports:jasperreports-jdt:$jasperVersion")
}
val compileJasperReports =
tasks.register("compileJasperReports") {
description = "Compiles source jasper reports files."
doLast {
println("running compileJasperReports task")
val jasperSourcesDirs = sourceSets.main.get().resources.sourceDirectories.files
val jasperTargetDir = sourceSets.main.get().output.resourcesDir
println("jasperTargetDir = $jasperTargetDir")
jasperTargetDir!!.mkdirs()
jasperSourcesDirs.forEach { jasperSourceDir ->
println("Processing jasper reports in source dir = $jasperSourceDir")
ant.withGroovyBuilder {
"taskdef"(
"name" to "jrc",
"classname" to "net.sf.jasperreports.ant.JRAntCompileTask",
"classpath" to jasperreports.asPath,
)
"jrc"(
"srcdir" to jasperSourceDir,
"destdir" to jasperTargetDir,
"includes" to "*.jrxml",
)
}
}
}
}
tasks.processResources { finalizedBy(compileJasperReports) }
重要的两点:
compileJasperReports
之后添加了 processResources
任务。由于某种原因,在某些环境中,processResources
在进行任何复制之前正在清理输出文件夹。这就是为什么我必须使用 finalizedBy
而不是 dependsOn
。如果您要将 jrxml 复制到输出资源,您可以删除 forEach 并使用源文件夹作为资源输出。希望这可以帮助任何为此苦苦挣扎的人。