在我替换Ant构建时使用Gradle中的路径

时间:2012-01-06 19:33:14

标签: gradle

我正在研究使用Gradle替换现有Ant构建的概念验证 - 对于Java EE应用程序 - 这开始很简单,但随着时间的推移变得越来越复杂。 (Ant构建是通用的,由在构建开始时导入的两个文件中的适当属性和路径控制。这是一个好主意,但现在变得有点吱吱作响。)

我现在有很多作品被Gradle取代而且进展顺利,直到我需要一个已定义的路径。 Ant文件中的路径很棒,因为它们允许您只定义一次依赖项并在任何需要的位置引用它们。

我的搜索引擎foo今天一定很弱,但我无法找到任何可以解释如何在Gradle中定义某些东西的东西,它会像路径一样,并且我可以传递。

具体如何,如何使用Gradle将使用的东西替换Javac Ant任务中的以下路径和对该路径的引用?

首先,路径:

<path id="base.path">
    <pathelement location="ApacheCommons/commons-lang/commons-lang.jar" />
    <pathelement location="log4j/log4j.jar" />
    <pathelement location="Acme/acme.jar" />
    <fileset dir="." erroronmissingdir="false">
        <include name="**/*.jar"/>
    </fileset>
    <fileset dir="../../${project.name}Model" erroronmissingdir="false">
        <include name="**/${project.name}Model.jar"/>
    </fileset>
    <fileset dir="../../${project.name}Persistance" erroronmissingdir="false">
        <include name="**/${project.name}Persistance.jar"/>
    </fileset>      
    <fileset dir="../../${project.name}Service" erroronmissingdir="false">
        <include name="**/${project.name}Service.jar"/>
    </fileset>
    <path refid="server.path" />
</path>

现在我抓住路径并将其放在Groovy变量中:

def basePath = ant.properties['base.path']

然后在Javac任务中使用它:

ant.javac(srcDir: "${compileSourcecode}", destDir: "WebContent/WEB-INF/classes", source: "${compileSourceversion}", target: "${compileTargetversion}", verbose: "${compileVerbose}", debug: "${compileDebug}", deprecation: "${compileDeprecation}", optimize: "${compileOptimize}", classpathref: "${basePath}", includeAntRuntime: "${compileIncludeAntRuntime}")

除了basePath变量最终为null,然后没有编译。

1 个答案:

答案 0 :(得分:4)

由于您要迁移到Gradle,我建议您在Gradle中设置类路径。在Gradle中管理依赖项的首选方法(正如您可能已经知道的)是使用依赖库(Maven或Ivy),然后从每个gradle项目的dependencies闭包中对其进行限定

dependencies{
  compile 'commons-lang:commons-lang:2.2'
  compile 'log4j:log4j:1.2.16'
  ...
}

但是,如果你必须引用本地jar文件,你几乎没有其他选择:

1)设置自己的Maven或Ivy存储库

一开始这是一项相当多的工作,但从长远来看可能会更有益。

2)创建flat file repository

将此添加到您的build.gradle

repositories { 
  flatDir{
    dirs 'lib'
  } 
}

将您的罐子/战争以这种格式<name>-<version>.jar或仅<name>.jar放入lib文件夹:

e.g.
lib/acme-1.2.3.jar
lib/acme.jar

然后你可以像往常一样引用它:

dependencies{
  ...
  compile 'acme:acme:1.2.3'
  ...
}

注意:只使用依赖项名称和版本来查找jar,并且不会假定传递依赖项。

3)Reference the jar files directly

例如:

dependencies{
  ...
  compile file("../../${project.name}Model/${project.name}Model.jar")
  compile fileTree(dir: '../../${project.name}Model', include: '**/*.jar')
  ...
}

注意: ${project.name}将解析为gradle project的名称。

添加到compile配置的所有jar都将自动用于默认编译任务,例如classesjar。您可能希望创建自定义Compile task以此方式替换当前的ant.javac(...)

task compileSomething(type: Compile){
  source = file("${compileSourcecode}")
  destinationDir = file("WebContent/WEB-INF/classes")
  sourceCompatibility = "${compileSourceversion}"
  targetCompatibility = "${compileTargetversion}"
  options.verbose = "${compileVerbose}".toBoolean()
  options.debug = "${compileDebug}".toBoolean()
  options.optimize = "${compileOptimize}".toBoolean()
  options.deprecation = "${compileDeprecation}".toBoolean()

  classpath = configurations.compile
  // or
  classpath = files('lib/library1.jar')
  // or any combination
  classpath = configuraions.compile + files('lib/library1.jar')
}

注意:不要认为你可以设置includeAntRuntime,但我认为它默认设置为true

当然,您可以在变量中保存文件列表,以便进行更细粒度的重用:

libs = files('lib/library1.jar', 'lib/library2.jar')

dependencies{
  compile libs
}

task compileSomething(type: Compile){
  classpath = libs
}

希望这有帮助。