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

Sim*_*ell 3 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>
Run Code Online (Sandbox Code Playgroud)

现在我获取路径并将其放入 Groovy 变量中:

def basePath = ant.properties['base.path']
Run Code Online (Sandbox Code Playgroud)

然后在 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}")
Run Code Online (Sandbox Code Playgroud)

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

rod*_*ion 6

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

dependencies{
  compile 'commons-lang:commons-lang:2.2'
  compile 'log4j:log4j:1.2.16'
  ...
}
Run Code Online (Sandbox Code Playgroud)

但是,如果您必须参考本地 jar 文件,您几乎没有其他选择:

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

一开始需要做很多工作,但从长远来看可能会更有益。

2) 创建平面文件存储库

将此添加到您的 build.gradle

repositories { 
  flatDir{
    dirs 'lib'
  } 
}
Run Code Online (Sandbox Code Playgroud)

将您的 jars/wars 以这种格式<name>-<version>.jar或仅放入 lib 文件夹中<name>.jar:

e.g.
lib/acme-1.2.3.jar
lib/acme.jar
Run Code Online (Sandbox Code Playgroud)

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

e.g.
lib/acme-1.2.3.jar
lib/acme.jar
Run Code Online (Sandbox Code Playgroud)

注意:仅使用依赖名称和版本来查找 jar,并且假定没有传递依赖。

3)直接引用jar文件

例如:

dependencies{
  ...
  compile 'acme:acme:1.2.3'
  ...
}
Run Code Online (Sandbox Code Playgroud)

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

添加到compile配置中的所有 jar将自动用于默认编译任务,例如classes和jar。您可能希望创建一个自定义Compile 任务来ant.javac(...)以这种方式替换您当前的任务:

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

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

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

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')
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。