Ant target fails then imported into gradle

I have a problem with an Ant target imported into a gradle build file.

There is no problem running the Ant target ‘generate-xsd’ from Ant. But I get an exception if I do it from gradle.

Im using gradle 2.14

Ant build file: build.xml

<?xml version="1.0" encoding="UTF-8" ?>
<project name="CAGenTUI2Forms" default="generate-xsd" basedir=".">
    <description>
        Generate Java source files from *.xsd in dk.xact.tui.
    </description>
    <property name="src" location="src" />
    <property name="xsd-dir" location="${src}/dk/xact/xsd" />
    <property name="java-dir" location="${src}/dk/xact/tui" />
    <property name="package" value="dk.xact.tui" />
    <taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
        <classpath>
            <pathelement location="lib/jaxb-xjc.jar" />
        </classpath>
    </taskdef>
    <!-- Generate Java source files. -->
    <target name="generate-xsd-to-java" description="Generate XSD to Java source files">
        <echo message="Generate XSD to Java source files" />
        <xjc destdir="${src}" package="${package}">
            <produces dir="${java-dir}" />
            <schema dir="${xsd-dir}" includes="*.xsd" />
        </xjc>
    </target>
    <!-- Remove time from timestamps. -->
    <target name="generate-xsd" depends="generate-xsd-to-java">
        <echo message="Remove time from timestamps" />
        <replaceregexp byline="true">
            <regexp pattern="^(\/\/ Generated on\: \d+\.\d+\.\d+) at .+$"/>
            <substitution expression="\1"/>
            <fileset dir="${java-dir}">
                <include name="*.java"/>
            </fileset>
        </replaceregexp>
    </target>
</project>

gralde build file: build.gradle

apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7
repositories {
    mavenCentral()
}
dependencies {
    compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.2.12'
}
sourceSets {
    main {
        java.srcDirs = ['src']
    }
}
ant.importBuild 'build.xml'
classes {
    dependsOn 'generate-xsd'
}

Stacktrace:

:compileJava
:generate-xsd-to-java
[ant:echo] Generate XSD to Java source files
:generate-xsd
[ant:echo] Remove time from timestamps
:generate-xsd FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':generate-xsd'.
> org/apache/tools/ant/launch/Launcher
* Try:
Run with --info or --debug option to get more log output.
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':generate-xsd'.
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
        at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)
        at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:66)
        at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
        at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:52)
        at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
        at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)
        at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:203)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:185)
        at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:66)
        at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:50)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:25)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:110)
        at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:37)
        at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:37)
        at org.gradle.execution.DefaultBuildExecuter.access$000(DefaultBuildExecuter.java:23)
        at org.gradle.execution.DefaultBuildExecuter$1.proceed(DefaultBuildExecuter.java:43)
        at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32)
        at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:37)
        at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:30)
        at org.gradle.initialization.DefaultGradleLauncher$4.run(DefaultGradleLauncher.java:153)
        at org.gradle.internal.Factories$1.create(Factories.java:22)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:91)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:53)
        at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:150)
        at org.gradle.initialization.DefaultGradleLauncher.access$200(DefaultGradleLauncher.java:32)
        at org.gradle.initialization.DefaultGradleLauncher$1.create(DefaultGradleLauncher.java:98)
        at org.gradle.initialization.DefaultGradleLauncher$1.create(DefaultGradleLauncher.java:92)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:91)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:63)
        at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:92)
        at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:83)
        at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:99)
        at org.gradle.tooling.internal.provider.ExecuteBuildActionRunner.run(ExecuteBuildActionRunner.java:28)
        at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
        at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:48)
        at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:30)
        at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:81)
        at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:46)
        at org.gradle.launcher.exec.DaemonUsageSuggestingBuildActionExecuter.execute(DaemonUsageSuggestingBuildActionExecuter.java:51)
        at org.gradle.launcher.exec.DaemonUsageSuggestingBuildActionExecuter.execute(DaemonUsageSuggestingBuildActionExecuter.java:28)
        at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:43)
        at org.gradle.internal.Actions$RunnableActionAdapter.execute(Actions.java:173)
        at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:239)
        at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:212)
        at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:35)
        at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:24)
        at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
        at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
        at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:205)
        at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)
        at org.gradle.launcher.Main.doAction(Main.java:33)
        at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
        at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:55)
        at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:36)
        at org.gradle.launcher.GradleMain.main(GradleMain.java:23)
        at org.gradle.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:30)
        at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:129)
        at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)
Caused by: java.lang.NoClassDefFoundError: org/apache/tools/ant/launch/Launcher
        at org.apache.tools.ant.ComponentHelper.diagnoseCreationFailure(ComponentHelper.java:893)
        at org.apache.tools.ant.UnknownElement.getNotFoundException(UnknownElement.java:499)
        at org.apache.tools.ant.UnknownElement.makeObject(UnknownElement.java:432)
        at org.apache.tools.ant.UnknownElement.maybeConfigure(UnknownElement.java:164)
        at org.apache.tools.ant.Task.perform(Task.java:347)
        at org.apache.tools.ant.Target.execute(Target.java:435)
        at org.apache.tools.ant.Target.performTasks(Target.java:456)
        at org.gradle.api.tasks.ant.AntTarget.executeAntTarget(AntTarget.java:37)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:75)
        at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:228)
        at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:221)
        at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:210)
        at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:621)
        at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:604)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
        ... 60 more
Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.launch.Launcher
        ... 76 more
BUILD FAILED
Total time: 5.16 secs

Hey,

I posted here the same problem with example, explanation and current fix.

It’s the issue with JAXB/XJB library and issue is reported: XJC task is closing wrong ClassLoader - ClassNotFoundException · Issue #51 · eclipse-ee4j/jaxb-istack-commons · GitHub

If you execute:

./gradlew generate_xsd --full-stacktrace --debug

You will get something like:

[DEBUG] [org.gradle.api.internal.project.ant.AntLoggingAdapter] Could not load class (org.apache.tools.ant.taskdefs.Replace) for type replace

We solved it with downgrading JAXB/XJC libraries until this is fixed. You can check GitHub - aspevec/xjc_ant_gradle: Repository for simulating bug in XJC implementation for example and more explanations.

1 Like

Thanks.

I had forgotten all about this.

My solution was to have Eclipse run the ANT task via a builder in order to generate Java files.