具有外部依赖项的Moqui脚本

时间:2018-10-05 15:48:42

标签: google-bigquery moqui

我正在编写一个小的组件,该组件需要从Google BigQuery表中提取数据,以后再将其另存为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错误110121908-17 o.m.i.a.XmlAction运行groovy脚本时发生错误(org.codehaus.groovy.control.MultipleCompilationErrorsException:启动失败:   component___intermegaBaseClientes_script_pullClientesBQ_groovy:1:无法解析com.google.cloud.bigquery.BigQuery类    @第1行,第1列。     导入com.google.cloud.bigquery.BigQuery

那么,如何(正确)在组件脚本上使用外部库?

谢谢

1 个答案:

答案 0 :(得分:0)

请参阅此处的Moqui组件结构文档,以获取有关组件中约定使用的目录和文件的描述:

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

那里有一个'lib'目录的描述,这是您如何在Moqui组件的类路径上获取JAR文件的方法。我只是添加了一些受支持文件的其他详细信息,包括带有与此问题相关注释的build.gradle。

简而言之,真正的问题是如何在Java类路径上获取内容,这是使用Moqui组件目录中的“类”和“ 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中。