Antjavac不';看不到Gradle的依赖项

Antjavac不';看不到Gradle的依赖项,ant,groovy,gradle,Ant,Groovy,Gradle,我正试图从ant迁移到gradle。第一阶段是将所有从属项移动到gradle.build,并通过ant继续构建war 在ant构建中,任务如下所示: <fileset id="project-libraries" dir="${project.libs.path}"> <include name="*jar"/> </fileset> <path id="master-classpath"> <fileset refid="

我正试图从ant迁移到gradle。第一阶段是将所有从属项移动到gradle.build,并通过ant继续构建war

在ant构建中,任务如下所示:

<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>

问题是ant任务(
/gradlew ant_build
)没有来自Gradle的依赖项(
依赖项{…}
)。如何将它们放入类路径(而不修改ant build)?

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

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
然而,这是一个“黑客”解决方案

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

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

我使用复制任务将所有gradle依赖项放入我在ant主类路径上声明的{libs}文件夹中

//add property
<property name="lib.dir"   value="${basedir}/lib" /></pre>

//tell ANT to put all jars in folder on classpath
<path id="master-classpath">
    <fileset dir="${lib.dir}">
        <include name="**/*.jar" />
    </fileset>

....
</path>

// copy task goes in your build.gradle file
task copyGradleDependenciesInAntFolder(type: Copy) {
    from configurations.compile
    into 'lib'
}

// make sure to run it before your {ant_build} target
{ant_build}.dependsOn copyGradleDependenciesInAntFolder
//添加属性
//告诉ANT将所有JAR放在类路径上的文件夹中
....
//复制任务进入build.gradle文件
任务CopyGradleDependenciesiantFolder(类型:复制){
从configurations.compile
进入“自由”
}
//确保在{ant_build}目标之前运行它
{ant_build}.dependsOn copygradledependenciesiantFolder
//add property
<property name="lib.dir"   value="${basedir}/lib" /></pre>

//tell ANT to put all jars in folder on classpath
<path id="master-classpath">
    <fileset dir="${lib.dir}">
        <include name="**/*.jar" />
    </fileset>

....
</path>

// copy task goes in your build.gradle file
task copyGradleDependenciesInAntFolder(type: Copy) {
    from configurations.compile
    into 'lib'
}

// make sure to run it before your {ant_build} target
{ant_build}.dependsOn copyGradleDependenciesInAntFolder