gpt4 book ai didi

gradle - gradle任务和groovy文件上的导入

转载 作者:行者123 更新时间:2023-12-03 05:13:21 25 4
gpt4 key购买 nike

我有以下buil.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 clean -Dtest.single =测试,则可以正常运行,并且测试运行成功,但是如果我运行 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;
^

这是我的ATest.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();
}
}
}

我是否缺少一些配置?

最佳答案

这是预期的行为。使用gradle运行测试时,gradle的工作是配置类路径,类加载器-即整个环境。

如果您运行该文件,请在脚本上提供配置。

看看 GroovyShell constructor summary。它需要一个ClassLoader对象和一个CompileConfiguration。提供这些对象是您的工作。

关于gradle - gradle任务和groovy文件上的导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39279170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com