Skip to content

Commit 19ee4ce

Browse files
author
Paul Marbach
committed
use execAndReadStdoutAndStderr to avoid hanging process
1 parent 1225e7d commit 19ee4ce

File tree

1 file changed

+9
-28
lines changed

1 file changed

+9
-28
lines changed

shared/src/main/kotlin/org/javacs/kt/classpath/MavenClassPathResolver.kt

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package org.javacs.kt.classpath
22

33
import org.javacs.kt.LOG
44
import org.javacs.kt.util.findCommandOnPath
5+
import org.javacs.kt.util.execAndReadStdoutAndStderr
56
import java.nio.file.Path
67
import java.nio.file.Files
78
import java.io.File
8-
import java.io.InputStream
99

1010
/** Resolver for reading maven dependencies */
1111
internal class MavenClassPathResolver private constructor(private val pom: Path) : ClassPathResolver {
@@ -59,35 +59,16 @@ private fun mavenJarName(a: Artifact, source: Boolean) =
5959
if (source) "${a.artifact}-${a.version}-sources.jar"
6060
else "${a.artifact}-${a.version}.jar"
6161

62-
private fun readInputStream(process: Process, inputStream: InputStream) {
63-
inputStream.bufferedReader().use { reader ->
64-
while (process.isAlive) {
65-
val line = reader.readLine()?.trim() ?: break
66-
if (line.isNotEmpty() && !line.startsWith("Progress")) {
67-
LOG.info("Maven: {}", line)
68-
}
69-
}
70-
}
71-
}
72-
7362
private fun generateMavenDependencyList(pom: Path): Path {
7463
val mavenOutput = Files.createTempFile("deps", ".txt")
75-
val workingDirectory = pom.toAbsolutePath().parent.toFile()
76-
val cmd = "$mvnCommand dependency:list -DincludeScope=test -DoutputFile=$mavenOutput"
77-
LOG.info("Run {} in {}", cmd, workingDirectory)
78-
val process = Runtime.getRuntime().exec(cmd, null, workingDirectory)
79-
80-
val inputStream = Thread() { readInputStream(process, process.inputStream) }
81-
val errorStream = Thread() { readInputStream(process, process.errorStream) }
82-
83-
// start the streams in parallel...
84-
inputStream.start()
85-
errorStream.start()
86-
87-
// ...then block on both of them
88-
inputStream.join()
89-
errorStream.join()
90-
64+
val command = "$mvnCommand dependency:list -DincludeScope=test -DoutputFile=$mavenOutput"
65+
val workingDirectory = pom.toAbsolutePath().parent
66+
LOG.info("Run {} in {}", command, workingDirectory)
67+
val (result, errors) = execAndReadStdoutAndStderr(command, workingDirectory)
68+
LOG.debug(result)
69+
if ("BUILD FAILURE" in errors) {
70+
LOG.warn("Maven task failed: {}", errors.lines().firstOrNull())
71+
}
9172
return mavenOutput
9273
}
9374

0 commit comments

Comments
 (0)