Attempting to import ant build file fails

Hello,

I’m trying to import an existing ant build file into my build.gradle script by including the following:

ant.importBuild ‘build.xml’

… but it is failing. The build.xml I’m trying to import references another file in a different directory called “common.xml” which includes all of my common ant tasks.

The stacktrace I get is as follows:

Could not import Ant build file ‘\build.xml’.

  • Try: Run with --info or --debug option to get more log output.

  • Exception is: org.gradle.api.GradleScriptException: A problem occurred evaluating root project ‘globug-integration-tests’.

at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:54)

at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.apply(DefaultScriptPluginFactory.java:127)

at org.gradle.configuration.BuildScriptProcessor.evaluate(BuildScriptProcessor.java:38)

at org.gradle.configuration.LifecycleProjectEvaluator.evaluate(LifecycleProjectEvaluator.java:43)

at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:463)

at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:75)

at org.gradle.configuration.ProjectEvaluationConfigurer.execute(ProjectEvaluationConfigurer.java:23)

at org.gradle.configuration.ProjectEvaluationConfigurer.execute(ProjectEvaluationConfigurer.java:21)

at org.gradle.configuration.DefaultBuildConfigurer$1.execute(DefaultBuildConfigurer.java:38)

at org.gradle.configuration.DefaultBuildConfigurer$1.execute(DefaultBuildConfigurer.java:35)

at org.gradle.api.internal.project.AbstractProject.configure(AbstractProject.java:439)

at org.gradle.api.internal.project.AbstractProject.allprojects(AbstractProject.java:434)

at org.gradle.configuration.DefaultBuildConfigurer.configure(DefaultBuildConfigurer.java:35)

at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:139)

at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:110)

at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:78)

at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:38)

at org.gradle.launcher.exec.InProcessGradleLauncherActionExecuter.execute(InProcessGradleLauncherActionExecuter.java:39)

at org.gradle.launcher.exec.InProcessGradleLauncherActionExecuter.execute(InProcessGradleLauncherActionExecuter.java:25)

at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:50)

at org.gradle.launcher.cli.ActionAdapter.execute(ActionAdapter.java:30)

at org.gradle.launcher.cli.ActionAdapter.execute(ActionAdapter.java:22)

at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:200)

at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:173)

at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)

at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:138)

at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)

at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)

at org.gradle.launcher.Main.doAction(Main.java:48)

at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)

at org.gradle.launcher.Main.main(Main.java:39)

at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:50)

at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:32)

at org.gradle.launcher.GradleMain.main(GradleMain.java:26) Caused by: org.gradle.api.GradleException: Could not import Ant build file ‘\build.xml’.

at org.gradle.api.internal.project.DefaultAntBuilder.importBuild(DefaultAntBuilder.groovy:78)

at org.gradle.api.internal.project.DefaultAntBuilder$importBuild.call(Unknown Source)

at build_9nk811bjk2lvcjfv5uodt9ie1.run(\build.gradle:3)

at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:52)

… 33 more Caused by: \build.xml:6: The following error occurred while executing this line: \common.xml:8: Problem: failed to create task or type antlib:org.apache.ivy.ant:settings Cause: The name is undefined. Action: Check the spelling. Action: Check that any custom tasks/types have been declared. Action: Check that any / declarations have taken place. No types or tasks have been defined in this namespace yet

This appears to be an antlib declaration. Action: Check that the implementing library exists in one of:

-ANT_HOME\lib

-the IDE Ant configuration dialogs

at org.apache.tools.ant.ProjectHelper.addLocationToBuildException(ProjectHelper.java:551)

at org.apache.tools.ant.taskdefs.ImportTask.importResource(ImportTask.java:234)

at org.apache.tools.ant.taskdefs.ImportTask.execute(ImportTask.java:163)

at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)

at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)

at org.apache.tools.ant.Task.perform(Task.java:348)

at org.apache.tools.ant.Target.execute(Target.java:390)

at org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:180)

at org.apache.tools.ant.ProjectHelper.configureProject(ProjectHelper.java:82)

at org.apache.tools.ant.ProjectHelper$configureProject.call(Unknown Source)

at org.gradle.api.internal.project.DefaultAntBuilder.importBuild(DefaultAntBuilder.groovy:76)

… 36 more Caused by: \common.xml:8: Problem: failed to create task or type antlib:org.apache.ivy.ant:settings Cause: The name is undefined. Action: Check the spelling. Action: Check that any custom tasks/types have been declared. Action: Check that any / declarations have taken place. No types or tasks have been defined in this namespace yet

This appears to be an antlib declaration. Action: Check that the implementing library exists in one of:

-ANT_HOME\lib

-the IDE Ant configuration dialogs

at org.apache.tools.ant.UnknownElement.getNotFoundException(UnknownElement.java:487)

at org.apache.tools.ant.UnknownElement.makeObject(UnknownElement.java:419)

at org.apache.tools.ant.UnknownElement.maybeConfigure(UnknownElement.java:163)

at org.apache.tools.ant.Task.perform(Task.java:347)

at org.apache.tools.ant.Target.execute(Target.java:390)

at org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:169)

at org.apache.tools.ant.taskdefs.ImportTask.importResource(ImportTask.java:225)

… 45 more

BUILD FAILED

Thanks for any assistance.

Hello, it seems that gradle isn’t able to resolve the ivy library you use in your ant build. How did you setup ivy in your ant build. Have you defined an ivy-install task or did you just put the ivy jars to the ANT_HOME\lib directory. Can you retry with the following snippet:

repositories{
 mavenCentral()
}
  configurations{
 ivyconf
}
  dependencies{
 ivyconf 'org.apache.ivy:ivy:2.3.0-rc1'
}
  ClassLoader antClassLoader = org.apache.tools.ant.Project.class.classLoader
                 configurations.ivyconf.each { File f ->
                         antClassLoader.addURL(f.toURI().toURL())
                 }
                   ant.importBuild 'build.xml'

This script downloads ivy and puts it on the classpath of gradles antbuilder.

regards, René

Hmm, we should probably provide a better way to control ant’s effective classpath here.

Raised GRADLE-2312 for this.

Thanks for the reply. Unfortunately, it’s still failing. My setup is that I have a build.xml file which imports another Ant file called “common.xml”. In here is the following line:

<ivy:settings id="ivy.instance" file="${ivy.settings.file}" override="true" />

… which is where it’s falling over, I think. Will check which version of Ivy I’m running.

Stacktrace:

* Exception is:
org.gradle.api.GradleScriptException: A problem occurred evaluating root project 'myProject'.
        at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:54)
        at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.apply(DefaultScriptPluginFactory.java:127)
        at org.gradle.configuration.BuildScriptProcessor.evaluate(BuildScriptProcessor.java:38)
        at org.gradle.configuration.LifecycleProjectEvaluator.evaluate(LifecycleProjectEvaluator.java:43)
        at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:463)
        at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:75)
        at org.gradle.configuration.ProjectEvaluationConfigurer.execute(ProjectEvaluationConfigurer.java:23)
        at org.gradle.configuration.ProjectEvaluationConfigurer.execute(ProjectEvaluationConfigurer.java:21)
        at org.gradle.configuration.DefaultBuildConfigurer$1.execute(DefaultBuildConfigurer.java:38)
        at org.gradle.configuration.DefaultBuildConfigurer$1.execute(DefaultBuildConfigurer.java:35)
        at org.gradle.api.internal.project.AbstractProject.configure(AbstractProject.java:439)
        at org.gradle.api.internal.project.AbstractProject.allprojects(AbstractProject.java:434)
        at org.gradle.configuration.DefaultBuildConfigurer.configure(DefaultBuildConfigurer.java:35)
        at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:139)
        at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:110)
        at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:78)
        at org.gradle.launcher.cli.ExecuteBuildAction.run(ExecuteBuildAction.java:38)
        at org.gradle.launcher.exec.InProcessGradleLauncherActionExecuter.execute(InProcessGradleLauncherActionExecuter.java:39)
        at org.gradle.launcher.exec.InProcessGradleLauncherActionExecuter.execute(InProcessGradleLauncherActionExecuter.java:25)
        at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:50)
        at org.gradle.launcher.cli.ActionAdapter.execute(ActionAdapter.java:30)
        at org.gradle.launcher.cli.ActionAdapter.execute(ActionAdapter.java:22)
        at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:200)
        at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:173)
        at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)
        at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:138)
        at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
        at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
        at org.gradle.launcher.Main.doAction(Main.java:48)
        at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
        at org.gradle.launcher.Main.main(Main.java:39)
        at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:50)
        at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:32)
        at org.gradle.launcher.GradleMain.main(GradleMain.java:26)
Caused by: org.gradle.api.GradleException: Could not import Ant build file '<directory>\build.xml'.
        at org.gradle.api.internal.project.DefaultAntBuilder.importBuild(DefaultAntBuilder.groovy:78)
        at org.gradle.api.internal.project.DefaultAntBuilder$importBuild.call(Unknown Source)
        at build_9nk811bjk2lvcjfv5uodt9ie1.run(<directory>\build.gradle:20)
        at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:52)
        ... 33 more
Caused by: <directory>\build.xml:6: The following error occurred while executing this line:
<commonDirectory>\common.xml:8: ivy:settings doesn't support the "override" attribute
        at org.apache.tools.ant.ProjectHelper.addLocationToBuildException(ProjectHelper.java:551)
        at org.apache.tools.ant.taskdefs.ImportTask.importResource(ImportTask.java:234)
        at org.apache.tools.ant.taskdefs.ImportTask.execute(ImportTask.java:163)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
        at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
        at org.apache.tools.ant.Task.perform(Task.java:348)
        at org.apache.tools.ant.Target.execute(Target.java:390)
        at org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:180)
        at org.apache.tools.ant.ProjectHelper.configureProject(ProjectHelper.java:82)
        at org.apache.tools.ant.ProjectHelper$configureProject.call(Unknown Source)
        at org.gradle.api.internal.project.DefaultAntBuilder.importBuild(DefaultAntBuilder.groovy:76)
        ... 36 more
Caused by: <commonDirectory>\common.xml:8: ivy:settings doesn't support the "override" attribute
        at org.apache.tools.ant.RuntimeConfigurable.maybeConfigure(RuntimeConfigurable.java:396)
        at org.apache.tools.ant.RuntimeConfigurable.maybeConfigure(RuntimeConfigurable.java:344)
        at org.apache.tools.ant.UnknownElement.configure(UnknownElement.java:198)
        at org.apache.tools.ant.UnknownElement.maybeConfigure(UnknownElement.java:163)
        at org.apache.tools.ant.Task.perform(Task.java:347)
        at org.apache.tools.ant.Target.execute(Target.java:390)
        at org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:169)
        at org.apache.tools.ant.taskdefs.ImportTask.importResource(ImportTask.java:225)
        ... 45 more
Caused by: The <antlib:org.apache.ivy.ant:settings> type doesn't support the "override" attribute.
        at org.apache.tools.ant.IntrospectionHelper.setAttribute(IntrospectionHelper.java:402)
        at org.apache.tools.ant.RuntimeConfigurable.maybeConfigure(RuntimeConfigurable.java:388)
        ... 52 more

Have tried with all 2.x versions of Ivy and am getting the same error.

So you tried Rene’s workaround?

I did, yes. Sorry if this was unclear. I think I’m using Ivy version 2.0.0.

According to the docs, ‘override’ is not a valid attribute for the ‘ivy:settings’ element.

http://ant.apache.org/ivy/history/latest-milestone/use/settings.html

If I remove the ‘override=“true”’ attribute from the ivy:settings tag in common.xml and try to run the script again, I get the following:

Caused by: org.gradle.api.InvalidUserDataException: Cannot add task ':test' as a task with that name already exists.
        at org.gradle.api.internal.tasks.DefaultTaskContainer.add(DefaultTaskContainer.java:58)
        at org.gradle.api.internal.tasks.DefaultTaskContainer.add(DefaultTaskContainer.java:73)
        at org.gradle.api.tasks.TaskContainer$add.call(Unknown Source)
        at org.gradle.api.internal.project.DefaultAntBuilder$_importBuild_closure3.doCall(DefaultAntBuilder.groovy:91)
        at org.gradle.api.internal.project.DefaultAntBuilder.importBuild(DefaultAntBuilder.groovy:89)
        at org.gradle.api.internal.project.DefaultAntBuilder$importBuild.call(Unknown Source)
        at build_9nk811bjk2lvcjfv5uodt9ie1.run(C:\Users\harrism\workspace\globug-integration-tests\build.gradle:20)
        at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:52)

There is an target called “test” in the ant file that I’m trying to import. Is there a clash of names happening here?

Yes. You have to make sure that the project you are importing into doesn’t have any tasks with the same name as the Ant targets you are importing.

The Ant script cannot be changed. How should I get around this issue? Do I need to create a new task that duplicates the Gradle “test” task functionality?

Don’t declare a ‘test’ task in the project that you are importing into, or import the Ant build into a separate (sub)project.

I need to create a test task as the ultimate aim of the Gradle script is to run a suite of JUnit tests for the associated Java code in the project.

Even if I import the Ant build into a separate project, won’t the name conflict exist? My Gradle script will need to reference the imported Ant build in some way.

I need to create a test task

Just give it a different name then.

Even if I import the Ant build into a separate project, won’t the name conflict exist?

There won’t be a conflict because tasks are scoped per project.

My Gradle script will need to reference the imported Ant build in some way.

No problem. Tasks from different projects can depend on each other just fine.

I have created a new task. I am using the java plugin; how do I associate my new task with the “testCompile” dependency configuration available in the plugin?

I can do this:

task myTest
    myTest.dependsOn test

… but that defeats the object of not using the “test” task name.

Thanks.

task myTest(type: Test) {
  // configure task here
}

See dsl:org.gradle.api.tasks.testing.Test for the configurations of the test task, including how you specify the compile classpath.

Is this issue solved? Is it possible to import Ant+Ivy build into Gradle?

I still have a problem with importing Ant+Ivy build into Gradle, having the same exception as in the question of this thread.