如何用Buck添加依赖并构建Java代码?Jedis依赖构建报错求助
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_jaryou specified isjedis-3.0.2-SNAPSHOT.jar, but yourmaven_coordspoints to the stable3.0.2release—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 specifybinary_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_coordsto the standard format for Jedis—correct coordinates areredis.clients:jedis:3.0.2, notredis.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_jarentries or your ownjava_librarytargets) in thedepsarray. - 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

