From 189873935d86b9788acf308acc132c4abe018c67 Mon Sep 17 00:00:00 2001 From: graemerocher Date: Wed, 4 Mar 2015 12:23:56 +0100 Subject: [PATCH] Search for the main class relative to the URI of the compiled class --- .../grails/io/support/MainClassFinder.groovy | 46 ++++++++++++++----- .../IntegrationTestMixinTransformation.groovy | 2 +- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/grails-bootstrap/src/main/groovy/org/grails/io/support/MainClassFinder.groovy b/grails-bootstrap/src/main/groovy/org/grails/io/support/MainClassFinder.groovy index 5d39343bae1..603a5f0dbdb 100644 --- a/grails-bootstrap/src/main/groovy/org/grails/io/support/MainClassFinder.groovy +++ b/grails-bootstrap/src/main/groovy/org/grails/io/support/MainClassFinder.groovy @@ -8,6 +8,8 @@ import groovyjarjarasm.asm.MethodVisitor import groovyjarjarasm.asm.Opcodes import groovyjarjarasm.asm.Type +import java.nio.file.Paths + /** * @author Graeme Rocher * @since 3.0 @@ -23,26 +25,30 @@ class MainClassFinder { static String mainClassName = null - static String searchMainClass() { + /** + * Searches for the main class relative to the give path that is within the project tree + * + * @param path The path as a URI + * @return The name of the main class + */ + static String searchMainClass(URI path) { if(mainClassName) return mainClassName + File file = path ? Paths.get(path).toFile() : null + def rootDir = findRootDirectory(file) + def classesDir = BuildSettings.CLASSES_DIR Collection searchDirs if (classesDir == null) { - def tokens = System.getProperty('java.class.path').split(System.getProperty('path.separator')) - def dirs = tokens.findAll() { String str -> !str.endsWith('.jar') }.collect() { String str -> new File(str) } - searchDirs = dirs.findAll() { File f -> f.isDirectory() } + searchDirs = [] } else { searchDirs = [classesDir] } - def userDir = new File(System.getProperty('user.dir')) - if(new File(userDir, 'settings.gradle').exists()) { - userDir.eachDir { File dir -> - // looking for subproject build directories... - def mainClassesDir = new File(dir, 'build/classes/main') - if(mainClassesDir.exists()) { - searchDirs << mainClassesDir - } + + if(rootDir) { + def rootClassesDir = new File(rootDir, "build/classes/main") + if(rootClassesDir.exists()) { + searchDirs << rootClassesDir } } @@ -55,6 +61,22 @@ class MainClassFinder { mainClass } + private static File findRootDirectory(File file) { + if(file) { + def parent = file.parentFile + + while(parent != null) { + if(new File(parent, "build.gradle").exists() || new File(parent, "grails-app").exists()) { + return parent + } + else { + parent = parent.parentFile + } + } + } + return null + } + static String findMainClass(File rootFolder = BuildSettings.CLASSES_DIR) { if(mainClassName) return mainClassName diff --git a/grails-plugin-testing/src/main/groovy/org/grails/compiler/injection/test/IntegrationTestMixinTransformation.groovy b/grails-plugin-testing/src/main/groovy/org/grails/compiler/injection/test/IntegrationTestMixinTransformation.groovy index 582be0b205d..997d4774088 100644 --- a/grails-plugin-testing/src/main/groovy/org/grails/compiler/injection/test/IntegrationTestMixinTransformation.groovy +++ b/grails-plugin-testing/src/main/groovy/org/grails/compiler/injection/test/IntegrationTestMixinTransformation.groovy @@ -79,7 +79,7 @@ class IntegrationTestMixinTransformation implements ASTTransformation { GrailsASTUtils.error(source, applicationClassExpression, "Invalid applicationClass attribute value [${applicationClassNode.getName()}]. The applicationClass attribute must specify a class which extends grails.boot.config.GrailsAutoConfiguration.", true) } } else { - String mainClass = MainClassFinder.searchMainClass() + String mainClass = MainClassFinder.searchMainClass(source.source.URI) if(mainClass) { applicationClassNode = ClassHelper.make(mainClass) }