如何从Jenkins Pipeline Script访问SQLite DB表?

时间:2019-03-31 17:33:45

标签: java sqlite jenkins groovy jenkins-pipeline

我有一个jenkinsfile,它将使用Maven运行一些黄瓜测试。我正在尝试添加数据库连接,以便脚本可以从数据库表中选择测试ID并在maven命令中使用它。

但是尝试连接数据库(SQLite)时出现错误。在pom中还添加了一些依赖项,但没有太大帮助。任何线索将不胜感激。

默认情况下,数据库位于maven项目“。\ src \ test \ resources \ RunHistoryDB.db”中,并在提交代码时将其推送到Jenkins,从而触发管道脚本。

import java.sql.*
import org.sqlite.SQLite
import groovy.sql.Sql


pipeline {
    agent any
    stages {
        stage ('Checkout Code') {
            steps {
                script {                

                    Class.forName('org.sqlite.JDBC')
                    def sql = Sql.newInstance('jdbc:sqlite:.\\src\\test\\resources\\RunHistoryDB.db', 'org.sqlite.JDBC')
                    sql.eachRow('select * from run_data') {  
                        println('id=${it.sid}, name= ${it.path}')
                    }

                    def exists = fileExists 'TestProject'
                    if (!exists){
                        new File('TestProject').mkdir()
                    }                   
                    dir ('TestProject') {
                        git url: 'http://localhost:3000/some/tours.git'
                        bat 'mvn -f TestProject\\pom.xml clean'
                    }
                }
            }
        }}}

Jenkins控制台错误

Started by an SCM change
Obtained TestProject/src/test/resources/Jenkinsfile from git http://localhost:3000/some/tours.git
Running in Durability level: MAX_SURVIVABILITY
java.lang.ClassNotFoundException: org.ibex.nestedvm.UnixRuntime
    at jenkins.util.AntClassLoader.findClassInComponents(AntClassLoader.java:1374)
    at jenkins.util.AntClassLoader.findClass(AntClassLoader.java:1327)
    at jenkins.util.AntClassLoader.loadClass(AntClassLoader.java:1080)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
Caused: java.lang.NoClassDefFoundError: org/ibex/nestedvm/UnixRuntime
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
    at jenkins.util.AntClassLoader.defineClassFromData(AntClassLoader.java:1140)
    at hudson.ClassicPluginStrategy$AntClassLoader2.defineClassFromData(ClassicPluginStrategy.java:862)
    at jenkins.util.AntClassLoader.getClassFromStream(AntClassLoader.java:1311)
    at jenkins.util.AntClassLoader.findClassInComponents(AntClassLoader.java:1364)
    at jenkins.util.AntClassLoader.findClass(AntClassLoader.java:1327)
    at java.base/jdk.internal.reflect.GeneratedMethodAccessor3.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at jenkins.ClassLoaderReflectionToolkit.invoke(ClassLoaderReflectionToolkit.java:44)
    at jenkins.ClassLoaderReflectionToolkit._findClass(ClassLoaderReflectionToolkit.java:81)
    at hudson.PluginManager$UberClassLoader.findClass(PluginManager.java:2023)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxResolvingClassLoader$1.compute(SandboxResolvingClassLoader.java:29)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxResolvingClassLoader$1.compute(SandboxResolvingClassLoader.java:26)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxResolvingClassLoader$4$1.load(SandboxResolvingClassLoader.java:85)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxResolvingClassLoader$4$1.load(SandboxResolvingClassLoader.java:78)
    at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3568)
    at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2350)
    at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2313)
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2228)
Caused: com.google.common.util.concurrent.ExecutionError
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2232)
    at com.google.common.cache.LocalCache.get(LocalCache.java:3965)
    at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3969)
    at com.google.common.cache.LocalCache$LocalManualCache.get(LocalCache.java:4829)
    at com.google.common.cache.LocalCache$LocalManualCache.getUnchecked(LocalCache.java:4834)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxResolvingClassLoader.loadClass(SandboxResolvingClassLoader.java:51)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:575)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell$TimingLoader.loadClass(CpsGroovyShell.java:168)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:575)
    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:677)
    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:787)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:575)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell$TimingLoader.loadClass(CpsGroovyShell.java:168)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:575)
    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:677)
    at groovy.lang.GroovyClassLoader.loadClass(GroovyClassLoader.java:545)
    at org.codehaus.groovy.control.ClassNodeResolver.tryAsLoaderClassOrScript(ClassNodeResolver.java:185)
    at org.codehaus.groovy.control.ClassNodeResolver.findClassNode(ClassNodeResolver.java:170)
    at org.codehaus.groovy.control.ClassNodeResolver.resolveName(ClassNodeResolver.java:126)
    at org.codehaus.groovy.control.ResolveVisitor.resolveToOuter(ResolveVisitor.java:676)
    at org.codehaus.groovy.control.ResolveVisitor.resolve(ResolveVisitor.java:313)
    at org.codehaus.groovy.control.ResolveVisitor.visitClass(ResolveVisitor.java:1236)
    at org.codehaus.groovy.control.ResolveVisitor.startResolving(ResolveVisitor.java:176)
    at org.codehaus.groovy.control.CompilationUnit$12.call(CompilationUnit.java:663)
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:943)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:554)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.lambda$doParse$0(CpsGroovyShell.java:135)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:136)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:132)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:560)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:521)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:320)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE

添加了特定依赖项

<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.27.2.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java -->
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.7.0</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.xerial.thirdparty/nestedvm -->
        <dependency>
            <groupId>org.xerial.thirdparty</groupId>
            <artifactId>nestedvm</artifactId>
            <version>1.0</version>
        </dependency>

0 个答案:

没有答案