Skip to content

Commit 8c81c89

Browse files
committed
Gradle - cleanup ProjectConnection handling
1 parent 1752b1b commit 8c81c89

File tree

3 files changed

+33
-59
lines changed

3 files changed

+33
-59
lines changed

grails-shell/src/main/groovy/org/grails/cli/GrailsCli.groovy

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import jline.console.UserInterruptException
2929
import jline.console.completer.ArgumentCompleter
3030
import jline.console.completer.Completer
3131
import jline.internal.NonBlockingInputStream
32+
import org.gradle.impldep.org.apache.commons.io.output.NullOutputStream
3233
import org.gradle.tooling.BuildActionExecuter
3334
import org.gradle.tooling.BuildCancelledException
3435
import org.gradle.tooling.ProjectConnection
@@ -374,7 +375,6 @@ class GrailsCli {
374375
console.updateStatus("Initializing application. Please wait...")
375376
try {
376377
initializeApplication(context.commandLine)
377-
GradleUtil.prepareConnection(projectContext.baseDir)
378378
setupCompleters()
379379
} finally {
380380
tiggerAppLoad = false
@@ -434,9 +434,6 @@ class GrailsCli {
434434
boolean firstRun = true
435435
while(keepRunning) {
436436
try {
437-
if(integrateGradle) {
438-
GradleUtil.prepareConnection(projectContext.baseDir)
439-
}
440437
if(firstRun) {
441438
console.addStatus("Enter a command name to run. Use TAB for completion:")
442439
firstRun = false
@@ -537,9 +534,13 @@ class GrailsCli {
537534
@Override
538535
Map<String, List<URL>> readFromGradle(ProjectConnection connection) {
539536
def config = applicationConfig
540-
GrailsClasspath grailsClasspath = GradleUtil.runBuildActionWithConsoleOutput(connection, projectContext, new ClasspathBuildAction()) { BuildActionExecuter<GrailsClasspath> executer ->
541-
executer.withArguments("-Dgrails.profile=${config.navigate("grails", "profile")}")
542-
}
537+
538+
BuildActionExecuter buildActionExecuter = connection.action(new ClasspathBuildAction())
539+
buildActionExecuter.standardOutput = new NullOutputStream()
540+
buildActionExecuter.standardError = new NullOutputStream()
541+
buildActionExecuter.withArguments("-Dgrails.profile=${config.navigate("grails", "profile")}")
542+
543+
def grailsClasspath = buildActionExecuter.run()
543544
if(grailsClasspath.error) {
544545
GrailsConsole.instance.error("${grailsClasspath.error} Type 'gradle dependencies' for more information")
545546
exit 1

grails-shell/src/main/groovy/org/grails/cli/gradle/GradleUtil.groovy

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -41,50 +41,6 @@ import org.grails.cli.profile.ProjectContext
4141
@CompileStatic
4242
class GradleUtil {
4343
private static final boolean DEFAULT_SUPPRESS_OUTPUT = true
44-
private static ProjectConnection preparedConnection = null
45-
private static File preparedConnectionBaseDir = null
46-
47-
public static ProjectConnection refreshConnection(File baseDir) {
48-
preparedConnection = openGradleConnection(baseDir)
49-
preparedConnectionBaseDir = baseDir.getAbsoluteFile()
50-
51-
try {
52-
Runtime.addShutdownHook {
53-
try {
54-
Thread.start {
55-
clearPreparedConnection()
56-
}.join(1000)
57-
} catch (Throwable e) {
58-
// ignore
59-
}
60-
61-
}
62-
} catch (e) {
63-
// ignore
64-
}
65-
return preparedConnection
66-
}
67-
68-
public static ProjectConnection prepareConnection(File baseDir) {
69-
if (preparedConnection == null || preparedConnectionBaseDir != baseDir.getAbsoluteFile()) {
70-
refreshConnection(baseDir)
71-
}
72-
return preparedConnection
73-
}
74-
75-
public static clearPreparedConnection() {
76-
if (preparedConnection != null) {
77-
try {
78-
Thread.start {
79-
preparedConnection?.close()
80-
}.join(2000)
81-
} catch (Throwable e) {
82-
}
83-
84-
preparedConnection = null
85-
preparedConnectionBaseDir = null
86-
}
87-
}
8844

8945
public static ProjectConnection openGradleConnection(File baseDir) {
9046
DefaultGradleConnector gradleConnector = (DefaultGradleConnector)GradleConnector.newConnector().forProjectDirectory(baseDir)
@@ -98,8 +54,7 @@ class GradleUtil {
9854

9955
public static <T> T withProjectConnection(File baseDir, boolean suppressOutput = DEFAULT_SUPPRESS_OUTPUT,
10056
@ClosureParams(value = SimpleType.class, options = "org.gradle.tooling.ProjectConnection") Closure<T> closure) {
101-
boolean preparedConnectionExisted = preparedConnection != null
102-
ProjectConnection projectConnection = prepareConnection(baseDir)
57+
ProjectConnection projectConnection = openGradleConnection(baseDir)
10358
try {
10459
if (suppressOutput) {
10560
SystemOutErrCapturer.withNullOutput {
@@ -111,8 +66,7 @@ class GradleUtil {
11166
}
11267
}
11368
} finally {
114-
if (!preparedConnectionExisted)
115-
clearPreparedConnection()
69+
projectConnection.close();
11670
}
11771
}
11872

grails-shell/src/main/groovy/org/grails/cli/gradle/cache/CachedGradleOperation.groovy

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
*/
1616
package org.grails.cli.gradle.cache
1717

18+
import grails.io.support.SystemOutErrCapturer
1819
import grails.util.BuildSettings
1920
import groovy.transform.CompileStatic
21+
import org.gradle.tooling.GradleConnector
2022
import org.gradle.tooling.ProjectConnection
23+
import org.gradle.tooling.internal.consumer.ConnectorServices
24+
import org.gradle.tooling.internal.consumer.DefaultGradleConnector
2125
import org.grails.cli.gradle.GradleUtil
2226
import org.grails.cli.profile.ProjectContext
2327

@@ -38,7 +42,6 @@ abstract class CachedGradleOperation<T> implements Callable<T> {
3842
CachedGradleOperation(ProjectContext projectContext, String fileName) {
3943
this.fileName = fileName
4044
this.projectContext = projectContext
41-
GradleUtil.refreshConnection(projectContext.baseDir)
4245
}
4346

4447
abstract T readFromCached(File f)
@@ -62,9 +65,25 @@ abstract class CachedGradleOperation<T> implements Callable<T> {
6265
throw e
6366
}
6467

65-
def data = GradleUtil.withProjectConnection(projectContext.baseDir, true) { ProjectConnection projectConnection -> readFromGradle(projectConnection) }
66-
storeData(data)
67-
return data
68+
try {
69+
DefaultGradleConnector dgc = (DefaultGradleConnector)GradleConnector.newConnector()
70+
.forProjectDirectory(projectContext.baseDir)
71+
72+
dgc.embedded(true)
73+
def projectConnection = dgc.connect()
74+
try {
75+
def data = SystemOutErrCapturer.withNullOutput {
76+
readFromGradle(projectConnection)
77+
}
78+
storeData(data)
79+
return data
80+
} finally {
81+
projectConnection.close()
82+
}
83+
} finally {
84+
DefaultGradleConnector.close()
85+
ConnectorServices.reset()
86+
}
6887
}
6988

7089
protected void storeData(T data) {

0 commit comments

Comments
 (0)