Gradle“错误:包com.google.common.collect不存在"

Gradle“错误:包com.google.common.collect不存在

问题描述:

我正在遵循一个小的测试脚本,并为它提供第一段代码以使其绿色.代码是Java,测试是通过Java进行的.Java是Mac OSX"El Capitan"上的版本"1.8.0_60". gradle 是2.8版.

I am following a little test script and providing it with the first piece of code to make it green. The code is java and the testing is gradle with java. Java is version "1.8.0_60" on Mac OSX "El Capitan". gradle is version 2.8.

使用 gradle build 后,这是显示的错误:

After using gradle build, here is the error shown:

$ gradle build
:compileJava
/Users/rsalazar/exercism/java/etl/src/main/java/Etl.java:1: error: package  com.google.common.collect does not exist
import com.google.common.collect.ImmutableMap;
                            ^
/Users/rsalazar/exercism/java/etl/src/main/java/Etl.java:9: error: cannot find symbol
    return ImmutableMap.of("a", 1);
           ^
  symbol:   variable ImmutableMap
  location: class Etl
2 errors
:compileJava FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 2.597 secs

这是 build.gradle 文件:

apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"

repositories {
  mavenCentral()
}

dependencies {
  testCompile "junit:junit:4.10"
  testCompile "org.easytesting:fest-assert-core:2.0M10"
  testCompile "com.google.guava:guava:16+"
}

这是测试:( EtlTest.java )

import com.google.common.collect.ImmutableMap;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.fest.assertions.api.Assertions.assertThat;

public class EtlTest {
  private final Etl etl = new Etl();

  @Test
  public void testTransformOneValue() {
    Map<Integer, List<String>> old = ImmutableMap.of(1, Arrays.asList("A"));
    Map<String, Integer> expected = ImmutableMap.of("a", 1);

    assertThat(etl.transform(old)).isEqualTo(expected);
  }
}

以下是受测试的代码:( Etl.java )

Here is the code under test: (Etl.java)

import com.google.common.collect.ImmutableMap;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Etl {
  public Map<String, Integer> transform(Map <Integer, List<String>> from) {
    return ImmutableMap.of("a", 1);
  }
}

我不是在寻求通过测试方面的帮助.只需帮助使测试用gradle编译即可.很遗憾地说,在使用提供的错误消息进行编译时,我被卡住了.我在网上找不到任何帮助.非常感谢!

I am not looking for help on making the test pass. Just help on making the testing compile with gradle. I am sorry to say I am stuck when compiling with the error message provided. I couldn't find any help on the net. Thanks much!

由于您需要 ImmutableMap 来编译源代码(不仅是测试源代码),因此还需要更改以下行:

Since you need ImmutableMap to compile source (not only test sources) as well you need to change this line:

testCompile "com.google.guava:guava:16+"

对此:

compile "com.google.guava:guava:16+"

它将使番石榴在编译时可用于源代码,从而解决了问题.

It will make guava available at compile time for source code, hence resolve the problem.