Skip to content

Commit

Permalink
Replace EclipseModel based classpath resolution with custom Gradle To…
Browse files Browse the repository at this point in the history
…olingModelBuilder implementation
  • Loading branch information
lhotari committed Mar 2, 2015
1 parent c5fb029 commit 1989584
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.grails.gradle.plugin.model

class DefaultGrailsClasspath implements GrailsClasspath {
private static final long serialVersionUID = 1L
List<URL> dependencies
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.grails.gradle.plugin.model

/**
* Gradle ToolingModel class that is used to return Classpath to Grails cli
*
* this file is also in grails-shell project
*/
interface GrailsClasspath extends Serializable {
List<URL> getDependencies()
}
1 change: 1 addition & 0 deletions grails-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apply plugin: 'groovy'

dependencies {
compile gradleApi()
compile project(":grails-gradle-model")
compile project(":grails-bootstrap"), {
exclude group:"org.fusesource.jansi", module:"jansi"
exclude group:"jline", module:"jline"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,32 @@ import org.gradle.api.tasks.TaskContainer
import org.gradle.api.tasks.compile.GroovyCompile
import org.gradle.api.tasks.testing.Test
import org.gradle.process.JavaForkOptions
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
import org.grails.build.parsing.CommandLineParser
import org.grails.gradle.plugin.agent.AgentTasksEnhancer
import org.grails.gradle.plugin.commands.ApplicationContextCommandTask
import org.grails.gradle.plugin.model.GrailsClasspathToolingModelBuilder
import org.grails.gradle.plugin.run.FindMainClassTask
import org.grails.io.support.FactoriesLoaderSupport

import javax.inject.Inject

class GrailsGradlePlugin extends GroovyPlugin {
public static final String APPLICATION_CONTEXT_COMMAND_CLASS = "grails.dev.commands.ApplicationCommand"
List<Class<Plugin>> basePluginClasses = [ProvidedBasePlugin, IntegrationTestGradlePlugin]
List<String> excludedGrailsAppSourceDirs = ['migrations', 'assets']
List<String> grailsAppResourceDirs = ['views', 'i18n', 'conf']
private final ToolingModelBuilderRegistry registry

@Inject
GrailsGradlePlugin(ToolingModelBuilderRegistry registry) {
this.registry = registry
}

void apply(Project project) {
super.apply(project)
registerToolingModelBuilder(project, registry)

registerGrailsExtension(project)

applyBasePlugins(project)
Expand All @@ -60,6 +72,10 @@ class GrailsGradlePlugin extends GroovyPlugin {
configureApplicationCommands(project)
}

protected void registerToolingModelBuilder(Project project, ToolingModelBuilderRegistry registry) {
registry.register(new GrailsClasspathToolingModelBuilder())
}

@CompileStatic
protected void applyBasePlugins(Project project) {
basePluginClasses.each { Class<Plugin> cls -> project.plugins.apply(cls) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.JavaExec
import org.gradle.api.tasks.bundling.Jar
import org.gradle.language.jvm.tasks.ProcessResources
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry

import javax.inject.Inject

/*
* Copyright 2014 original authors
Expand All @@ -31,6 +34,11 @@ import org.gradle.language.jvm.tasks.ProcessResources
*/
class GrailsPluginGradlePlugin extends GrailsGradlePlugin {

@Inject
GrailsPluginGradlePlugin(ToolingModelBuilderRegistry registry) {
super(registry)
}

@Override
void apply(Project project) {
super.apply(project)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.grails.gradle.plugin.model

import org.gradle.api.Project
import org.gradle.api.artifacts.ResolvedArtifact
import org.gradle.tooling.provider.model.ToolingModelBuilder

/**
* Builds the GrailsClasspath instance that contains the URLs of the resolved dependencies
*/
class GrailsClasspathToolingModelBuilder implements ToolingModelBuilder {

@Override
boolean canBuild(String modelName) {
return modelName == GrailsClasspath.name
}

@Override
Object buildAll(String modelName, Project project) {
List<URL> runtimeDependencies = project.getConfigurations().getByName("runtime").getResolvedConfiguration().getResolvedArtifacts().collect { ResolvedArtifact artifact ->
artifact.getFile().toURI().toURL()
}
new DefaultGrailsClasspath(dependencies: runtimeDependencies)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@
package org.grails.gradle.plugin.web

import org.gradle.api.Project
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
import org.grails.gradle.plugin.commands.ApplicationContextCommandTask
import org.grails.gradle.plugin.core.GrailsGradlePlugin

import javax.inject.Inject

/**
* Adds web specific extensions
*
* @author Graeme Rocher
* @since 3.0
*/
class GrailsWebGradlePlugin extends GrailsGradlePlugin {
@Inject
GrailsWebGradlePlugin(ToolingModelBuilderRegistry registry) {
super(registry)
}

@Override
void apply(Project project) {
Expand Down
1 change: 1 addition & 0 deletions grails-shell/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ext {

dependencies {
compile project(":grails-bootstrap")
compile project(":grails-gradle-model")
compile "org.apache.ant:ant:$antVersion"
compile "org.codehaus.groovy:groovy-ant:$groovyVersion"
compile "org.codehaus.groovy:groovy-json:$groovyVersion"
Expand Down
10 changes: 3 additions & 7 deletions grails-shell/src/main/groovy/org/grails/cli/GrailsCli.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import jline.console.completer.ArgumentCompleter
import jline.internal.NonBlockingInputStream
import org.gradle.tooling.BuildCancelledException
import org.gradle.tooling.ProjectConnection
import org.gradle.tooling.model.ExternalDependency
import org.gradle.tooling.model.eclipse.EclipseProject
import org.grails.build.parsing.CommandLine
import org.grails.build.parsing.CommandLineParser
import org.grails.build.parsing.DefaultCommandLine
Expand All @@ -45,6 +43,7 @@ import org.grails.cli.profile.commands.CommandRegistry
import org.grails.cli.profile.git.GitProfileRepository
import org.grails.config.CodeGenConfig
import org.grails.exceptions.ExceptionUtils
import org.grails.gradle.plugin.model.GrailsClasspath

import java.util.concurrent.*

Expand Down Expand Up @@ -363,11 +362,8 @@ class GrailsCli {

@Override
List<URL> readFromGradle(ProjectConnection connection) {
EclipseProject project = GradleUtil.runBuildActionWithConsoleOutput(connection, projectContext, new ClasspathBuildAction())

List<URL> classpathUrls = project.getClasspath().collect { dependency -> ((ExternalDependency)dependency).file.toURI().toURL() }

return classpathUrls
GrailsClasspath grailsClasspath = GradleUtil.runBuildActionWithConsoleOutput(connection, projectContext, new ClasspathBuildAction())
return grailsClasspath.dependencies
}
}.call()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ package org.grails.cli.gradle

import org.gradle.tooling.BuildAction
import org.gradle.tooling.BuildController
import org.gradle.tooling.model.eclipse.EclipseProject
import org.grails.gradle.plugin.model.GrailsClasspath

/**
* Gets the EclipseProject which helps obtain the classpath necessary
*
* @author Graeme Rocher
* @since 3.0
*/
class ClasspathBuildAction implements BuildAction<EclipseProject>, Serializable {
class ClasspathBuildAction implements BuildAction<GrailsClasspath>, Serializable {
@Override
EclipseProject execute(BuildController controller) {
controller.getModel(EclipseProject)
GrailsClasspath execute(BuildController controller) {
controller.getModel(GrailsClasspath)
}
}
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ include (
'grails-compat',

// Gradle Plugin
'grails-gradle-plugin'
'grails-gradle-plugin',
'grails-gradle-model'
)

0 comments on commit 1989584

Please sign in to comment.