我编写了一个小组件,它需要从Google BigQuery表中提取数据,以便稍后将其保存为Party。
因此,我创建了一个新组件,其中我有一个服务,其中有一个操作来调用一个脚本和一个脚本。在组件上,我还添加了一个build.gradle来向google添加依赖项。
问题是,当我尝试从脚本导入bigquery库时,它说它找不到它们。
组件/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.然后,我转到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
那么,我如何(正确地)在组件的脚本上使用外部库呢?
谢谢
发布于 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文件中以执行以下操作的示例:
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中。
https://stackoverflow.com/questions/52669284
复制相似问题