【问题标题】:Gradle 'error: package com.google.common.collect does not exist'Gradle '错误:com.google.common.collect 包不存在'
【发布时间】:2015-10-22 18:11:43
【问题描述】:

我正在跟踪一个小测试脚本,并为其提供第一段代码以使其变为绿色。代码是java,测试是用java gradle。 Java 是 Mac OSX“El Capitan”上的“1.8.0_60”版本。 gradle 是 2.8 版。

使用gradle build后,出现如下错误:

$ 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)

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 编译测试。很抱歉,在使用提供的错误消息进行编译时我被卡住了。我在网上找不到任何帮助。非常感谢!

【问题讨论】:

    标签: java testing gradle


    【解决方案1】:

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

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

    到这里:

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

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

    【讨论】:

    • 非常感谢!它工作得很好。我将不得不阅读有关 gradle 的更多信息,但是开始学习 java 和 groovy 看起来很棒。再次感谢。我建议大家关注 'exercism' java 分支,并在那里为正在衰落的 'groovy' 分支提供动力。
    • 我试图给你一些因果报应,但我似乎必须达到第 13 级才能做到这一点。
    • 有同样的问题,但使用 maven。你的回应帮助我意识到这一点。谢谢。
    猜你喜欢
    • 2020-09-03
    • 1970-01-01
    • 2013-08-21
    • 2014-10-02
    • 2023-03-04
    • 1970-01-01
    • 2013-06-08
    • 2017-07-29
    • 1970-01-01
    相关资源
    最近更新 更多