具有外部依赖项的 Moqui 脚本

Moqui script with external dependencies

我正在编写一个小组件,它需要从 Google BigQuery table 中提取数据,以便稍后将其保存为 Party。

所以我创建了一个新组件,我为其提供了一个服务,只需一个操作即可调用一个脚本和一个脚本。在组件上我还添加了一个 build.gradle 以将依赖项添加到 google bigquery.

问题是,当我尝试从脚本导入 bigquery 库时,它说找不到它们。

component/mycomponent/{数据、实体、屏幕、脚本、服务}

mycomponent/component.xml:

<?xml version="1.0" encoding="UTF-8"?>
<component xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://moqui.org/xsd/moqui-conf-2.1.xsd"
    name="mycomponent" version="${moqui_version}">
</component>

mycomponent/build.gradle:

apply plugin: 'groovy'

sourceCompatibility = '1.8'
def moquiDir = file(projectDir.absolutePath + '/../../..')
def frameworkDir = file(moquiDir.absolutePath + '/framework')

// maybe in the future: repositories { mavenCentral() }
repositories {
    flatDir name: 'localLib', dirs: frameworkDir.absolutePath + '/lib'
    jcenter()
}

dependencies {
    compile project(':framework')
    testCompile project(':framework').configurations.testCompile.allDependencies
    compile 'com.google.cloud:google-cloud-bigquery:1.40.0'
}

// by default the Java plugin runs test on build, change to not do that (only run test if explicit task)
// no longer workds as of gradle 4.8 or possibly earlier, use clear() instead: check.dependsOn.remove(test)
check.dependsOn.clear()

test {
    dependsOn cleanTest
    dependsOn ':runtime:component:mantle-usl:test'

    systemProperty 'moqui.runtime', moquiDir.absolutePath + '/runtime'
    systemProperty 'moqui.conf', 'conf/MoquiDevConf.xml'
    systemProperty 'moqui.init.static', 'true'

    // show standard out and standard error of the test JVM(s) on the console
    testLogging.showStandardStreams = true; testLogging.showExceptions = true

    classpath += files(sourceSets.main.output.classesDirs)
    // filter out classpath entries that don't exist (gradle adds a bunch of these), or ElasticSearch JarHell will blow up
    classpath = classpath.filter { it.exists() }

    beforeTest { descriptor -> logger.lifecycle("Running test: ${descriptor}") }
}

mycomponent/services/myservice.xml:

<services xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://moqui.org/xsd/service-definition-2.1.xsd">
    <service verb="sync" noun="myservice">
        <in-parameters/>
        <out-parameters/>
        <actions>
            <script location="component://mycomponent/script/pullClientesBQ.groovy" />
        </actions>
    </service>
</services>

mycomponent/script/pullClientesBQ.groovy:

import com.google.cloud.bigquery.BigQuery
import com.google.cloud.bigquery.BigQueryOptions
import com.google.cloud.bigquery.FieldValueList
import com.google.cloud.bigquery.QueryJobConfiguration
// Script code follows.

然后我转到 运行 服务的工具 Web 界面,然后:

17:47:13.788 ERROR 110121908-17 o.m.i.a.XmlAction Error running groovy script (org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: component___intermegaBaseClientes_script_pullClientesBQ_groovy: 1: unable to resolve class com.google.cloud.bigquery.BigQuery @ line 1, column 1. import com.google.cloud.bigquery.BigQuery

那么,我怎样才能(正确地)在我的组件脚本上使用外部库?

谢谢

请参阅此处的 Moqui 组件结构文档,了解组件中约定使用的目录和文件的说明:

https://www.moqui.org/docs/framework/Tool+and+Config+Overview#extensions-and-add-ons

那里有对 'lib' 目录的描述,这是在 Moqui 组件的类路径中获取 JAR 文件的方式。我刚刚为支持的文件添加了一些额外的详细信息,包括 build.gradle 以及与此问题相关的注释。

简而言之,真正的问题是如何在 Java 类路径中获取内容,这是使用 Moqui 组件目录中的 'classes' 和 'lib' 目录完成的。您缺少的是 build.gradle 文件中的内容,用于将构建的和依赖的 JAR 文件复制到 'lib' 目录。以下是您可以添加到 build.gradle 文件中的示例:

task cleanLib(type: Delete) { delete fileTree(dir: projectDir.absolutePath+'/lib', include: '*') }
clean.dependsOn cleanLib

jar {
    destinationDir = file(projectDir.absolutePath + '/lib')
    baseName = jarBaseName
}
task copyDependencies { doLast {
    copy { from (configurations.runtime - project(':framework').configurations.runtime - project(':framework').jar.archivePath)
        into file(projectDir.absolutePath + '/lib') }
} }
copyDependencies.dependsOn cleanLib
jar.dependsOn copyDependencies

将 'lib' 目录添加到组件目录中的 .gitignore 文件也是一个好主意,这样 JAR 文件就不会意外添加到 git .