Use JavaExec to invoke Ant with required dependencies on its classpath

Previously, we were adding dependencies to Ant's ClassLoader within
Gradle. It is suspected that this was causing sporadic loader
contraint violations as types that Gradle itself uses (from Commons
Compress) were then available from two different ClassLoaders.

This commit reworks the Ant smoke test to use JavaExec and Ant's
launcher to run the build. This allows us to make the necessary
dependencies available to Ant in an isolated manner. The javac
invocation within Ant is now forked to allow it to find the tools jar
even when the build itself is running on a JRE.

Closes gh-19839
This commit is contained in:
Andy Wilkinson
2020-01-21 12:09:17 +00:00
parent 62a848f1b1
commit 4486da8ef3
2 changed files with 11 additions and 13 deletions

View File

@@ -25,6 +25,8 @@ plugins.withType(EclipsePlugin) {
dependencies {
antDependencies 'org.apache.ivy:ivy:2.4.0'
antDependencies project(path: ':spring-boot-project:spring-boot-tools:spring-boot-antlib')
antDependencies 'org.apache.ant:ant-launcher:1.9.3'
antDependencies 'org.apache.ant:ant:1.9.3'
testRepository project(path: ':spring-boot-project:spring-boot-tools:spring-boot-loader', configuration: 'mavenRepository')
testRepository project(path: ':spring-boot-project:spring-boot-starters:spring-boot-starter', configuration: 'mavenRepository')
@@ -44,22 +46,18 @@ task syncTestRepository(type: Sync) {
}
}
ant.importBuild('build.xml') {
'ant' + it
}
ant.properties['ant-spring-boot.version'] = project.version
ant.properties['projectDir'] = project.layout.projectDirectory
antresolve {
task antRun(type: JavaExec) {
dependsOn syncTestRepository, configurations.antDependencies
doFirst {
ClassLoader antClassLoader = org.apache.tools.ant.Project.class.classLoader
configurations.antDependencies.each { antClassLoader.addURL it.toURI().toURL() }
}
classpath = configurations.antDependencies;
main = 'org.apache.tools.ant.launch.Launcher'
systemProperties = [
'ant-spring-boot.version' : version,
'projectDir': project.layout.projectDirectory
]
}
task test(type: Test) {
dependsOn antbuild
dependsOn antRun
testClassesDirs = sourceSets.test.output.classesDirs
classpath = sourceSets.test.runtimeClasspath
}

View File

@@ -29,7 +29,7 @@
</target>
<target name="compile" depends="init" description="compile">
<javac srcdir="src/main/java" destdir="build/ant/classes" classpathref="compile.classpath" />
<javac srcdir="src/main/java" destdir="build/ant/classes" classpathref="compile.classpath" fork="true" />
</target>
<target name="clean" description="cleans all created files/dirs">