|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.grails.gradle.plugin.run |
| 20 | + |
| 21 | +import groovy.transform.CompileStatic |
| 22 | +import org.gradle.api.DefaultTask |
| 23 | +import org.gradle.api.Project |
| 24 | +import org.gradle.api.file.FileCollection |
| 25 | +import org.gradle.api.file.RegularFile |
| 26 | +import org.gradle.api.plugins.ExtraPropertiesExtension |
| 27 | +import org.gradle.api.provider.Provider |
| 28 | +import org.gradle.api.tasks.CacheableTask |
| 29 | +import org.gradle.api.tasks.InputFiles |
| 30 | +import org.gradle.api.tasks.OutputFile |
| 31 | +import org.gradle.api.tasks.PathSensitive |
| 32 | +import org.gradle.api.tasks.PathSensitivity |
| 33 | +import org.gradle.api.tasks.SourceSet |
| 34 | +import org.gradle.api.tasks.SourceSetOutput |
| 35 | +import org.gradle.api.tasks.TaskAction |
| 36 | +import org.grails.gradle.plugin.util.SourceSets |
| 37 | +import org.grails.io.support.MainClassFinder |
| 38 | +import org.springframework.boot.gradle.dsl.SpringBootExtension |
| 39 | +import org.springframework.boot.gradle.plugin.SpringBootPlugin |
| 40 | + |
| 41 | +/** |
| 42 | + * A task that finds the main task, differs slightly from Boot's version as expects a subclass of GrailsConfiguration |
| 43 | + * |
| 44 | + * @author Graeme Rocher |
| 45 | + * @since 3.0 |
| 46 | + */ |
| 47 | +@CompileStatic |
| 48 | +@CacheableTask |
| 49 | +class FindMainClassTask extends DefaultTask { |
| 50 | + |
| 51 | + @TaskAction |
| 52 | + void setMainClassProperty() { |
| 53 | + Project project = this.project |
| 54 | + |
| 55 | + def bootRunTask = project.tasks.findByName('bootRun') |
| 56 | + if (!bootRunTask || !bootRunTask.enabled) { |
| 57 | + project.logger.info('The bootRun task does not exist or is disabled, so this must not be a runnable grails application. Skipping finding main class.') |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + def bootJarTask = project.tasks.findByName(SpringBootPlugin.BOOT_JAR_TASK_NAME) |
| 62 | + def bootWarTask = project.tasks.findByName(SpringBootPlugin.BOOT_WAR_TASK_NAME) |
| 63 | + if ((!bootJarTask || !bootJarTask.enabled) && (!bootWarTask || !bootWarTask.enabled)) { |
| 64 | + project.logger.info('There is neither a {} or {} task that will run. Skipping finding main Application class.', SpringBootPlugin.BOOT_JAR_TASK_NAME, SpringBootPlugin.BOOT_WAR_TASK_NAME) |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + String mainClass = findMainClass() |
| 69 | + if (mainClass) { |
| 70 | + def extraProperties = project.extensions.getByType(ExtraPropertiesExtension) |
| 71 | + extraProperties.set('mainClassName', mainClass) |
| 72 | + |
| 73 | + def springBootExtension = project.extensions.getByType(SpringBootExtension) |
| 74 | + springBootExtension.mainClass.convention(mainClass) |
| 75 | + } else { |
| 76 | + project.logger.warn('No main class found. Please set \'springBoot.mainClass\'.') |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @InputFiles |
| 81 | + @PathSensitive(PathSensitivity.RELATIVE) |
| 82 | + FileCollection getClassesDirs() { |
| 83 | + SourceSet mainSourceSet = SourceSets.findMainSourceSet(project) |
| 84 | + if(mainSourceSet) { |
| 85 | + return resolveClassesDirs(mainSourceSet.output, project) |
| 86 | + } |
| 87 | + |
| 88 | + project.files(project.layout.buildDirectory.dir('classes/main')) |
| 89 | + } |
| 90 | + |
| 91 | + @OutputFile |
| 92 | + Provider<RegularFile> getMainClassCacheFile() { |
| 93 | + project.layout.buildDirectory.file('resolvedMainClassName') |
| 94 | + } |
| 95 | + |
| 96 | + protected String findMainClass() { |
| 97 | + Project project = this.project |
| 98 | + |
| 99 | + File buildDir = project.layout.buildDirectory.get().asFile |
| 100 | + buildDir.mkdirs() |
| 101 | + |
| 102 | + File mainClassFile = getMainClassCacheFile().getOrNull()?.asFile |
| 103 | + if (mainClassFile.exists()) { |
| 104 | + return mainClassFile.text |
| 105 | + } else { |
| 106 | + // Look up the main source set. |
| 107 | + SourceSet mainSourceSet = SourceSets.findMainSourceSet(project) |
| 108 | + if (!mainSourceSet) { |
| 109 | + return null |
| 110 | + } |
| 111 | + |
| 112 | + MainClassFinder mainClassFinder = createMainClassFinder() |
| 113 | + // Get the directories from which to try to find the main class. |
| 114 | + Set<File> classesDirs = getClassesDirs().files |
| 115 | + String mainClass = null |
| 116 | + for (File classesDir in classesDirs) { |
| 117 | + mainClass = mainClassFinder.findMainClass(classesDir) |
| 118 | + if (mainClass != null) { |
| 119 | + mainClassFile.text = mainClass |
| 120 | + break |
| 121 | + } |
| 122 | + } |
| 123 | + if (mainClass == null) { |
| 124 | + // Fallback attempt on a legacy directory. |
| 125 | + mainClass = mainClassFinder.findMainClass(project.layout.buildDirectory.dir('classes/groovy/main').getOrNull()?.asFile) |
| 126 | + if (mainClass != null) { |
| 127 | + mainClassFile.text = mainClass |
| 128 | + } else { |
| 129 | + if (project.plugins.hasPlugin('org.grails.gradle.plugin.core.GrailsPluginGradlePlugin')) { |
| 130 | + // this is ok if the project is a plugin because it's likely not going to be a runnable grails app |
| 131 | + project.logger.lifecycle('WARNING: this plugin project does not have an Application.class and thus the bootJar / bootRun will be invalid.') |
| 132 | + return null |
| 133 | + } |
| 134 | + |
| 135 | + throw new RuntimeException('Could not find Application main class. Please set \'springBoot.mainClass\' or disable BootJar & BootArchive tasks.') |
| 136 | + } |
| 137 | + } |
| 138 | + return mainClass |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + protected FileCollection resolveClassesDirs(SourceSetOutput output, Project project) { |
| 143 | + output?.classesDirs ?: project.files(project.layout.buildDirectory.dir('classes/main')) |
| 144 | + } |
| 145 | + |
| 146 | + protected MainClassFinder createMainClassFinder() { |
| 147 | + new MainClassFinder() |
| 148 | + } |
| 149 | +} |
0 commit comments