// Redefine the compileJava and compileTestJava tasks in order to compile sources with ajc instead of javac

configurations {
	rt
	ajc
	aspects
	ajInpath
}

compileJava  {
	actions = []
	dependsOn configurations.ajc.getTaskDependencyFromProjectDependency(true, "compileJava")

	def outputDir = project.sourceSets.main.output.classesDir
	inputs.files(project.sourceSets.main.allSource + project.sourceSets.main.compileClasspath)
	outputs.dir outputDir

	doLast{
		// Assemble runtime classpath from folders and JARs that actually exist
		def runtimeClasspath = project.files(sourceSets.main.runtimeClasspath.files.findAll({ it.exists() }))

		ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties",
				classpath: configurations.ajc.asPath)

		ant.iajc(source: sourceCompatibility, target: targetCompatibility,
				maxmem: "1024m", fork: "true", Xlint: "ignore",
				destDir: outputDir.absolutePath,
				aspectPath: configurations.aspects.asPath,
				inpath: configurations.ajInpath.asPath,
				sourceRootCopyFilter: "**/*.java,**/*.aj",
				classpath: (runtimeClasspath + configurations.rt).asPath) {
			sourceroots {
				sourceSets.main.java.srcDirs.each {
					pathelement(location:it.absolutePath)
				}
			}
		}
	}
}

compileTestJava  {
	actions = []
	dependsOn configurations.ajc.getTaskDependencyFromProjectDependency(true, "compileTestJava")
	dependsOn jar

	def outputDir = project.sourceSets.test.output.classesDir
	inputs.files(project.sourceSets.test.allSource + project.sourceSets.test.compileClasspath)
	outputs.dir outputDir

	doLast{
		// Assemble runtime classpath from folders and JARs that actually exist
		def runtimeClasspath = project.files(sourceSets.test.runtimeClasspath.files.findAll({ it.exists() }))

		ant.taskdef(resource: "org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties",
				classpath: configurations.ajc.asPath)

		ant.iajc(source: sourceCompatibility, target: targetCompatibility,
				maxmem: "1024m", fork: "true", Xlint: "ignore",
				destDir: outputDir.absolutePath,
				aspectPath: jar.archivePath,
				inpath: configurations.ajInpath.asPath,
				classpath: (runtimeClasspath + project.files(jar.archivePath) + configurations.rt).asPath) {
			sourceroots {
				sourceSets.test.java.srcDirs.each {
					pathelement(location:it.absolutePath)
				}
			}
		}
	}
}
