Ant-javac看不到Gradle的依赖项 [英] Ant-javac doesn't see dependencies from Gradle

查看:107
本文介绍了Ant-javac看不到Gradle的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从蚂蚁迁移到gradle.第一步是将所有依赖移至gradle.build并仍通过ant进行战争.

I'm trying to migrate from ant to gradle. First phase of this is to move all dependecies to gradle.build and still build war via ant.

构建蚂蚁的任务看起来像这样:

In ant building task looks like that:

<fileset id="project-libraries" dir="${project.libs.path}">
    <include name="*jar"/>
</fileset>

<path id="master-classpath">
    <fileset refid="project-libraries"/>
    <fileset refid="tomcat"/>
    <fileset refid="hibernate-tools"/>
    <fileset refid="findbug"/>
    <pathelement path="${build.dir}"/>
</path>

<target name="build" description="Build the application">
    <javac destdir="${build.dir}" target="${javac.version}" source="${javac.version}" nowarn="true" deprecation="false" optimize="false" failonerror="true" encoding="utf-8" debug="on">
        <src refid="src.dir.set"/>
        <classpath refid="master-classpath${master-classpath-version}"/>
        <compilerarg value="-Xlint:-unchecked"/>
    </javac>
</target>

在Gradle中,我使用以下代码导入build.xml:

In Gradle I'm importing build.xml with this code:

ant.importBuild('build.xml') { antTargetName ->
    'ant_' + antTargetName
}

问题是ant任务(./gradlew ant_build)没有来自Gradle(dependencies { ... })的依赖关系.我如何将它们放入类路径(不修改蚂蚁的构建)?

The problem is that ant task (./gradlew ant_build) doesn't have dependencies from Gradle (dependencies { ... }). How can I put them into classpath (without modifying ant build)?

推荐答案

您可以执行以下操作,将依赖项添加到项目的AntBuilder实例中:

You can do the following to add the dependencies to the project's AntBuilder instance:

task antClasspathSetter {        
    doLast {
        def antClassLoader = org.apache.tools.ant.Project.class.classLoader
        configurations.compile.each { File f ->
            antClassLoader.addURL(f.toURI().toURL())
        }
    }
}

ant_build.dependsOn antClasspathSetter

但是,这是一个"hacky"解决方案.

However, this is a 'hacky' solution.

如果可以将ant构建脚本移动到单独的ant任务文件,则使用taskdef是更好的解决方案.在这种情况下,您可以执行以下操作:

Using taskdef is a better solution, if the ant build script can be moved to a separate ant task file. In that case, you can do the following:

ant.taskdef(name: 'myAntTask',
            classname: 'my.ant.Task',
            classpath: configurations.compile.asPath)

这篇关于Ant-javac看不到Gradle的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆