You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Buck添加依赖并构建Java代码?Jedis依赖构建报错求助

Fixing Jedis Dependency Issue in Buck + Guide to Java Builds with Buck

1. Correct prebuilt_jar Definition for Jedis

Your existing Buck config has two critical issues that are causing the "package not found" error:

  • The binary_jar you specified is jedis-3.0.2-SNAPSHOT.jar, but your maven_coords points to the stable 3.0.2 release—these version identifiers don't match.
  • When using maven_coords, Buck can automatically pull the correct JAR from Maven Central, so you don't need to manually specify binary_jar (this parameter is only for local, pre-downloaded JAR files).

Here's the fixed prebuilt_jar configuration:

prebuilt_jar(
    name = "jedis",
    maven_coords = "redis.clients:jedis:3.0.2",
    visibility = [
        "PUBLIC",
    ],
)

Quick note: I adjusted the maven_coords to the standard format for Jedis—correct coordinates are redis.clients:jedis:3.0.2, not redis.clients.jedis:jar:3.0.2. This mismatched coordinate format was likely one of the main reasons Buck couldn't locate the package.

If you absolutely need to use a local, pre-downloaded JAR (e.g., no access to remote Maven repos), use this instead:

prebuilt_jar(
    name = "jedis",
    binary_jar = "path/to/your/local/jedis-3.0.2.jar",
    visibility = [
        "PUBLIC",
    ],
)

Double-check that the local file path and version number in the filename match exactly.

2. Adding Dependencies & Building Java Code with Buck

Let's walk through a complete workflow for building Java code that depends on Jedis using Buck:

Step 1: Define Your Java Target

Suppose you have a simple Java class RedisClient.java:

import redis.clients.jedis.Jedis;

public class RedisClient {
    public static void main(String[] args) {
        try (Jedis jedis = new Jedis("localhost", 6379)) {
            jedis.set("test-key", "Hello Buck + Jedis!");
            System.out.println(jedis.get("test-key"));
        }
    }
}

Create a BUCK file in the same directory to define your Java binary (use java_library instead if building a reusable library) and link the Jedis dependency:

# First, define the Jedis dependency
prebuilt_jar(
    name = "jedis",
    maven_coords = "redis.clients:jedis:3.0.2",
    visibility = [
        "PUBLIC",
    ],
)

# Define the Java executable
java_binary(
    name = "redis-client",
    srcs = ["RedisClient.java"],
    deps = [
        ":jedis",
    ],
    main_class = "RedisClient",
)

Step 2: Build & Run Your Code

  • To build the project, run this in your terminal:
    buck build //path/to/your/directory:redis-client
    
  • To run the compiled executable:
    buck run //path/to/your/directory:redis-client
    

Key Tips for Buck Java Dependencies

  • Add multiple dependencies by including more targets (like other prebuilt_jar entries or your own java_library targets) in the deps array.
  • Buck automatically handles transitive dependencies for Maven artifacts (e.g., Jedis depends on commons-pool2—you won't need to add that manually).
  • To inspect your full dependency tree, use this command:
    buck query "deps(//path/to/your/directory:redis-client)"
    

内容的提问来源于stack exchange,提问作者Pradeep Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:44:57