提问者:小点点

gradle任务和groovy文件上的导入


我有以下build. gradle

apply plugin: "groovy"

repositories {
    mavenCentral()
        jcenter()
}

dependencies {
    groovy group: "org.codehaus.groovy", name:"groovy-all", version: "1.8.6"
    compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '2.53.0'
    compile "org.testng:testng:6.3.1"
    compile group: 'com.jcraft', name: 'jsch', version: '0.1.53'
    compile group: 'net.schmizz', name: 'sshj', version: '0.3.1'
    compile group: 'commons-lang', name: 'commons-lang', version: '2.3'
    compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.21'
    compile group: 'org.bouncycastle', name: 'bcprov-jdk16', version: '1.46'
    compile group: 'net.sf.expectit', name: 'expectit-core', version: '0.8.1'
    compile group: 'net.sf.expectit', name: 'expectit-ant', version: '0.8.1'
    compile group: 'net.sf.expectit', name: 'expectit-parent', version: '0.8.1', ext: 'pom'
    compile "log4j:log4j:1.2.17"
    testCompile "org.spockframework:spock-core:0.7-groovy-1.8"
    testCompile "junit:junit:4.10"
    testCompile "cglib:cglib-nodep:2.2.2"
    testCompile "org.objenesis:objenesis:1.2"
    testRuntime "org.slf4j:slf4j-api:1.7.10"
}

sourceSets {
    test { groovy { 
        srcDir 'foo/bar/'
    } }
}

buildscript {
    repositories { 
           mavenCentral() 
           jcenter() 
        }
}

task runA << {
    new GroovyShell().run(file('foo/bar/ATest.groovy'));
}

如果我运行gradle清洁-Dtest. one=A测试,它可以工作并且测试成功运行,但如果我运行gradle-q runA,它会显示无法识别的导入,例如:

foo/bar/ATest.groovy: 16: unable to resolve class net.schmizz.sshj.SSHClient
   @ line 16, column 1.
     import net.schmizz.sshj.SSHClient;
     ^
  foo/bar/ATest.groovy: 28: unable to resolve class org.openqa.selenium.firefox.FirefoxDriver
   @ line 28, column 1.
     import org.openqa.selenium.firefox.FirefoxDriver;
     ^

这是我的测试. groovy

package foo.bar;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets;
import java.nio.file.Files
import java.nio.file.Paths;
import java.security.PublicKey

import org.apache.log4j.Logger;
import org.junit.Before
import org.junit.After
import org.junit.Test;
import org.junit.*;
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Shell;
import net.schmizz.sshj.transport.verification.HostKeyVerifier;
import net.sf.expectit.Expect;
import net.sf.expectit.ExpectBuilder;
import static net.sf.expectit.filter.Filters.removeColors;
import static net.sf.expectit.filter.Filters.removeNonPrintable;
import static net.sf.expectit.matcher.Matchers.contains;
import static net.sf.expectit.matcher.Matchers.regexp;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select

import java.util.regex.Pattern

import javax.naming.directory.InvalidAttributesException;

import java.util.concurrent.TimeUnit
import org.apache.log4j.Logger;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

public class ATest{
    private WebDriver driver;
    private static final Logger logger = Logger.getLogger(ATest.class);
    Properties props = new Properties();
    private StringBuffer verificationErrors = new StringBuffer();

    private Expect expect = null;
    Session session = null;
    SSHClient ssh = null;

    @Before
    public void setUp() throws Exception {
        logger.info("========================================================================");
        logger.info("Starting...");

        // setting up Webdriver instance
        try {
            driver = new FirefoxDriver();           
        }
        catch (Exception e) {
            logger.info("Could not open Firefox instance: " + e.getMessage());
            fail("Could not open Firefox instance: " + e.getMessage());
        }

        // setting up SSH connection
        try {
            ssh = new SSHClient();
            ssh.addHostKeyVerifier(
                    new HostKeyVerifier() {
                        @Override
                        public boolean verify(String s, int i, PublicKey publicKey) {
                            return true;
                        }
                    });

            ssh.connect(props.getProperty("host"), Integer.parseInt(props.getProperty("port")));
            ssh.authPassword(props.getProperty("user"), props.getProperty("password"));
            session = ssh.startSession();
            session.allocateDefaultPTY();
        } catch (Exception e) {
            fail("Could not open SSH connection with " + props.getProperty("host") + "\nError: " + e.getMessage());
        }

        // start a interactive remote shell
        Shell shell = session.startShell();
        expect = new ExpectBuilder().withOutput(shell.getOutputStream())
                .withInputs(shell.getInputStream(), shell.getErrorStream())
                //.withEchoInput(System.out)
                .withInputFilters(removeColors(), removeNonPrintable())
                .withExceptionOnFailure()
                .build();       
        try {
            Thread.sleep(3000);
            // log as 'su' user in the remote shell
            expect.sendLine(props.getProperty("suuser"));
            expect.expect(contains("Password: "));
            expect.sendLine(props.getProperty("supassword"));
            expect.expect(regexp("root@"));
        }
        catch (Exception e) {
            fail("Error: " + e.getMessage());
        }
    }

    @Test
    public void run() {
        ...
    }

    @After
    public void tearDown() throws Exception {
        if (driver != null) {
            driver.quit();
        }
    }
}

我错过了一些配置吗?


共1个答案

匿名用户

这是预期的行为。当您使用gradle运行测试时,配置类路径、类加载器(即整个环境)是gradle作业。

如果您运行该文件,则您的工作是提供配置。

查看GroovyShells,构造函数摘要。它需要一个ClassLoader对象和一个CompileConfiguration。提供这些对象是你的工作。