我正在开发我的第一个Java应用程序。一位同事启动了它,但没有时间完成它。她是一位经验丰富的Java程序员,但我不是。但是,该应用程序足够小,以至于看起来像是我可以作为第一个Java应用程序进行的项目(我确实使用JVM语言(例如Clojure)进行工作,因此我对JVM领域有一定的了解)。

我正在添加将数据输出为JSON的功能。我想使用Google的Gson类:

http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.google.code.gson%22

com.google.code.gson

我的同事没有外部依赖性,因此她只是从命令行编译代码。现在,我要添加第三方依赖项,我认为我应该切换到构建工具。我选择Buildr:http://buildr.apache.org/extending.html

我决定下载构成com.google.code.gson的3个Jar文件:

gson-2.2.4-javadoc.jar

gson-2.2.4-sources.jar

gson-2.2.4.jar

我不确定如何将它们包含在Buildr项目中。

我使用“ buildr”命令来建立Buildr首选的默认目录。但是没有明显的文件夹来存放3rd Party Jars。

我尝试了构建文件的许多变体。我最新的迭代是:

# Version number for this release
VERSION_NUMBER = "1.0.0"
# Group identifier for your projects
GROUP = "buildr_fdg"
COPYRIGHT = "2014, Open"

GOOGLEGSON = ['gson-2.2.4-javadoc.jar', 'gson-2.2.4-sources.jar', 'gson-2.2.4.jar']

# Specify Maven 2.0 remote repositories here, like this:
repositories.remote << "http://repo1.maven.org/maven2"

desc "The Buildr_fdg project -- creating JSON object full of fake data since we don't have enough real data ."
define "buildr_fdg" do

  project.version = VERSION_NUMBER
  project.group = GROUP
  manifest["Implementation-Vendor"] = COPYRIGHT
  compile.with GOOGLEGSON
  resources
  test.compile.with # Add classpath dependencies
  package(:jar)
end


但是我得到这些错误:

    buildr build
(in /Users/cerhov/projects/openz/lofdg/buildr_fdg, development)
Building buildr_fdg
Compiling buildr_fdg into /Users/cerhov/projects/openz/lofdg/buildr_fdg/target/classes
/Users/cerhov/projects/openz/lofdg/buildr_fdg/src/main/java/Main.java:8: package com.google.code does not exist
import com.google.code.*;
^
/Users/cerhov/projects/openz/lofdg/buildr_fdg/src/main/java/Main.java:27: cannot find symbol
symbol  : class Gson
location: class com.company.Main
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    ^
/Users/cerhov/projects/openz/lofdg/buildr_fdg/src/main/java/Main.java:27: cannot find symbol
symbol  : class GsonBuilder
location: class com.company.Main
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
                    ^
Note: /Users/cerhov/projects/openz/lofdg/buildr_fdg/src/main/java/Main.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
3 errors
Buildr aborted!
RuntimeError : Failed to compile, see errors above

(See full trace by running task with --trace)
cerhov : 15:33:25 : ~/projects/openz/lofdg/buildr_fdg $ buildr build
(in /Users/cerhov/projects/openz/lofdg/buildr_fdg, development)
Building buildr_fdg
Buildr aborted!
RuntimeError : Don't know how to build task '/Users/cerhov/projects/openz/lofdg/buildr_fdg/com.google.code.gson'


我如何告诉Buildr我的第三方罐子?

最佳答案

向Buildr项目添加依赖项的最简单方法是使用库的“行家”坐标。因此,我相信您想要做的是将GOOGLEGSON替换为

GOOGLEGSON = ['com.google.code.gson:gson:jar:2.2.4']


结合对Maven Central的引用,应意味着Buildr下载依赖项并将其添加到您的类路径中。

10-05 18:08