3

我可以告诉 Quarkus Gradle 插件(gradle quarkusDevgradlew quarkusBuild -Dquarkus.package.uber-jar=true)使用我自己提供的资源,而不是在依赖 jar 重复时从依赖 jar 中选择资源吗?

我在构建 uber-jar 时收到这些消息:

Duplicate entry META-INF/org.apache.uima.fit/types.txt entry from de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.api.segmentation-asl::jar:1.10.0(runtime) will be ignored. Existing file was provided by de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.api.syntax-asl::jar:1.10.0(runtime)
Duplicate entry META-INF/org.apache.uima.fit/types.txt entry from de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.api.lexmorph-asl::jar:1.10.0(runtime) will be ignored. Existing file was provided by de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.api.syntax-asl::jar:1.10.0(runtime)
Duplicate entry META-INF/org.apache.uima.fit/types.txt entry from de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.api.metadata-asl::jar:1.10.0(runtime) will be ignored. Existing file was provided by de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.api.syntax-asl::jar:1.10.0(runtime)
Duplicate entry META-INF/org.apache.uima.fit/types.txt entry from de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.api.ner-asl::jar:1.10.0(runtime) will be ignored. Existing file was provided by de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.api.syntax-asl::jar:1.10.0(runtime)

这些 DKPro / uimaFIT 库是 NLP 库,自带META-INF/org.apache.uima.fit/types.txt文件。您应该自己合并这些文件并添加自己的类型,然后只将这个新合并的文件包含在您的 uber-jar 中,或者作为类路径中的第一个。

中有一个选项quarkus.package.user-configured-ignored-entriesapplication.properties但它也会删除我自己提供的文件。所以这不是我想要的(另见https://github.com/quarkusio/quarkus/blob/master/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/JarResultBuildStep.java#L186)。我没有检查 的来源gradle quarkusDev,但它会导致相同的运行时异常。

供其他使用 uimaFIT 的人参考,这个不正确META-INF/org.apache.uima.fit/types.txt的文件会导致类似 org.apache.uima.analysis_engine.AnalysisEngineProcessException: JCas type "org.apache.uima.conceptMapper.support.tokenizer.TokenAnnotation" used in Java code, but was not declared in the XML type descriptor..

所以我的问题是,我如何告诉 Gradle 或 Quarkus 使用我自己提供的这个文件,而不是从依赖 jar 中随机选择一个文件?

用 Kotlin DSL 编写的示例 Gradle 脚本。该任务generateNlpFiles和函数根据 uimaFIT 的要求,joinResources自动将 XML 文件中的 Java 源文件生成src/main/typesystem到 into中,并将重复的资源加入到中。你不需要太用力地看它们。build/generated/sources/jcasgen/main/META-INF/org.apache.uima.fit/types.txt/generated/resources/uimafit/

import java.io.FileOutputStream
import java.net.URLClassLoader
import org.apache.commons.io.IOUtils

plugins {
    id("java")
    id("io.quarkus")
    id("eclipse")
}

repositories {
    jcenter()
    // required for downloading OpenNLP models
    maven("https://zoidberg.ukp.informatik.tu-darmstadt.de/artifactory/public-releases/")
}

group = "com.example"
version = "0.0.0-SNAPSHOT"

java.sourceCompatibility = JavaVersion.VERSION_11
java.targetCompatibility = JavaVersion.VERSION_11

dependencies {
    val quarkusPlatformGroupId: String by project
    val quarkusPlatformArtifactId: String by project
    val quarkusPlatformVersion: String by project
    // Quarkus dependencies
    implementation(enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}"))
    implementation("io.quarkus:quarkus-jaxb")
    implementation("io.quarkus:quarkus-jackson")
    implementation("io.quarkus:quarkus-resteasy")
    implementation("io.quarkus:quarkus-jdbc-mariadb")
    implementation("io.quarkus:quarkus-resteasy-jsonb")
    implementation("io.quarkus:quarkus-smallrye-openapi")
    implementation("io.quarkus:quarkus-container-image-docker")
    // UIMA
    implementation("org.apache.uima:uimaj-core:2.10.3")
    implementation("org.apache.uima:ConceptMapper:2.10.2")
    implementation("org.apache.uima:uimafit-core:2.4.0")
    // DKPro
    implementation("de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.io.xmi-asl:1.10.0")
    implementation("de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.api.metadata-asl:1.10.0")
    implementation("de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.langdetect-asl:1.10.0")
    implementation("de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.icu-asl:1.10.0")
    implementation("de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.opennlp-asl:1.10.0")
    implementation("de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.opennlp-model-tagger-de-maxent:20120616.1")
    implementation("de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.opennlp-model-tagger-en-maxent:20120616.1")
    implementation("de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.opennlp-asl:1.10.0")
    implementation("de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.opennlp-model-ner-de-nemgp:20141024.1")
    implementation("de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.opennlp-model-ner-en-location:20100907.0")
    implementation("de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.opennlp-model-ner-en-organization:20100907.0")
    implementation("de.tudarmstadt.ukp.dkpro.core:de.tudarmstadt.ukp.dkpro.core.opennlp-model-ner-en-person:20130624.1")
    // tests
    testImplementation("io.quarkus:quarkus-junit5")
    testImplementation("io.rest-assured:rest-assured")
    // for generating NLP type system during compile time
    compileOnly("org.apache.uima:uimaj-tools:2.10.4")
}

// joins resource files from classpath into single file
fun joinResources(classLoader: URLClassLoader, inputResourceName: String, outputFile: File) {
    val outputStream = FileOutputStream(outputFile)
    val resources = classLoader.findResources(inputResourceName).toList()
    resources.forEach {
        val inputStream = it.openStream()
        IOUtils.copy(inputStream, outputStream)
        outputStream.write('\n'.toInt());
        inputStream.close()
    }
    outputStream.close()
}

// generate NLP type system from XML files and join uimaFIT files
val generateNlpFiles = task("generateNlpFiles") {
    inputs.files(fileTree("src/main/typesystem"))
    inputs.files(fileTree("src/main/resources"))
    outputs.dir("${buildDir}/generated/sources/jcasgen/main/")
    outputs.dir("${buildDir}/generated/resources/uimafit/")

    val compileClasspath = project.sourceSets.main.get().compileClasspath
    val runtimeClasspath = project.sourceSets.main.get().runtimeClasspath
    val compileClassLoader = URLClassLoader(compileClasspath.map{ it.toURI().toURL() }.toTypedArray())
    val runtimeClassLoader = URLClassLoader(runtimeClasspath.map{ it.toURI().toURL() }.toTypedArray())

    // from XML files in src/main/typesystem/ generate Java sources into build/generated/sources/jcasgen/main/
    val jCasGen = compileClassLoader.loadClass("org.apache.uima.tools.jcasgen.Jg").newInstance()
    fileTree("src/main/typesystem").forEach() { typeSystemFile ->
        doFirst {
            // see https://github.com/Dictanova/gradle-jcasgen-plugin/blob/master/src/main/groovy/com/dictanova/jcasgen/gradle/JCasGenTask.groovy#L45
            val jcasgeninput = "${typeSystemFile}"
            val jcasgenoutput = "${buildDir}/generated/sources/jcasgen/main/"
            val jcasgenclasspath = "${runtimeClasspath.asPath}"
            val arguments: Array<String> = arrayOf("-jcasgeninput", jcasgeninput, "-jcasgenoutput", jcasgenoutput, "-jcasgenclasspath", jcasgenclasspath)
            val main1 = jCasGen.javaClass.getMethod("main1", arguments.javaClass)
            main1.invoke(jCasGen, arguments)
        }
    }

    // collect types.txt and components.txt from classpath and join them in build/generated/resources/uimafit/META-INF/org.apache.uima.fit/
    val uimafitDir = "${buildDir}/generated/resources/uimafit/META-INF/org.apache.uima.fit"
    mkdir(uimafitDir)
    joinResources(runtimeClassLoader, "META-INF/org.apache.uima.fit/types.txt", File("${uimafitDir}/types.txt"))
    joinResources(runtimeClassLoader, "META-INF/org.apache.uima.fit/components.txt", File("${uimafitDir}/components.txt"))
}

eclipse {
    project {
        natures(
                "org.eclipse.wst.common.project.facet.core.nature",
                "org.eclipse.buildship.core.gradleprojectnature"
        )
    }
    classpath {
        file.withXml {
            val attributes = mapOf("kind" to "src", "path" to "build/generated/sources/jcasgen/main")
            this.asNode().appendNode("classpathentry", attributes)
        }
    }
}

tasks {
    compileJava {
        options.encoding = "UTF-8"
        options.compilerArgs.add("-parameters") // was in original Quarkus Gradle file, not sure what this does
        dependsOn(generateNlpFiles)
        // add generated sources to source sets
        sourceSets["main"].java.srcDir(file("${buildDir}/generated/sources/jcasgen/main/"))
        sourceSets["main"].resources.srcDir(file("${buildDir}/generated/resources/uimafit/"))
    }
    compileTestJava {
        options.encoding = "UTF-8"
    }
    "eclipse" {
        dependsOn(generateNlpFiles)
    }
}

一种解决方法是使用gradlew quarkusBuild -Dquarkus.package.uber-jar=truewith 条目quarkus.package.user-configured-ignored-entries并将我自己的文件手动添加到生成的 jar 中,但这不适用于gradle quarkusDev.

我正在使用 Quarkus 1.3.2,因为 Quarkus 1.4.1 无法处理多个资源目录(另请参阅https://github.com/quarkusio/quarkus/blob/master/devtools/gradle/src/main/java/io/quarkus /gradle/tasks/QuarkusDev.java#L391),根据我的项目的需要。

我还尝试使用一些 Gradle JarJar 插件排除文件,例如https://github.com/shevek/jarjar,但无法让它们运行。

4

1 回答 1

2

现在,你不能,它只会从提供它的罐子中取出一个。

您能否在我们的跟踪器中创建功能请求:https ://github.com/quarkusio/quarkus/issues/new?assignees=&labels=kind%2Fenhancement&template=feature_request.md&title= 。

听起来很有用。

谢谢!

于 2020-04-30T17:46:58.533 回答