首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有外部依赖关系的Moqui脚本

具有外部依赖关系的Moqui脚本
EN

Stack Overflow用户
提问于 2018-10-05 15:48:42
回答 1查看 128关注 0票数 0

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

因此,我创建了一个新组件,其中我有一个服务,其中有一个操作来调用一个脚本和一个脚本。在组件上,我还添加了一个build.gradle来向google添加依赖项。

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

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

mycomponent/component.xml:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
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.

然后,我转到Tools web接口来运行该服务,并:

17:47:13.788错误110121908-17运行groovy脚本(org.codehaus.groovy.control.MultipleCompilationErrorsException:启动失败: component___intermegaBaseClientes_script_pullClientesBQ_groovy: 1:无法解析类com.google.cloud.bigquery.BigQuery @第1行,第1列,导入com.google.cloud.bigquery.BigQuery

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

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-06 06:38:21

有关组件中约定使用的目录和文件的说明,请参阅此处的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文件中以执行以下操作的示例:

代码语言:javascript
复制
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中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52669284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档