Skip to content

Commit 621517a

Browse files
authored
Merge pull request #336 from themkat/maven-wrapper
Implemented maven wrapper (mvnw) support
2 parents cfd1717 + 58e7402 commit 621517a

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ 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.findProjectCommandWithName
56
import org.javacs.kt.util.execAndReadStdoutAndStderr
67
import java.nio.file.Path
78
import java.nio.file.Files
@@ -103,14 +104,14 @@ private fun mavenJarName(a: Artifact, source: Boolean) =
103104

104105
private fun generateMavenDependencyList(pom: Path): Path {
105106
val mavenOutput = Files.createTempFile("deps", ".txt")
106-
val command = "$mvnCommand dependency:list -DincludeScope=test -DoutputFile=$mavenOutput -Dstyle.color=never"
107+
val command = "${mvnCommand(pom)} dependency:list -DincludeScope=test -DoutputFile=$mavenOutput -Dstyle.color=never"
107108
runCommand(pom, command)
108109
return mavenOutput
109110
}
110111

111112
private fun generateMavenDependencySourcesList(pom: Path): Path {
112113
val mavenOutput = Files.createTempFile("sources", ".txt")
113-
val command = "$mvnCommand dependency:sources -DincludeScope=test -DoutputFile=$mavenOutput -Dstyle.color=never"
114+
val command = "${mvnCommand(pom)} dependency:sources -DincludeScope=test -DoutputFile=$mavenOutput -Dstyle.color=never"
114115
runCommand(pom, command)
115116
return mavenOutput
116117
}
@@ -125,8 +126,14 @@ private fun runCommand(pom: Path, command: String) {
125126
}
126127
}
127128

128-
private val mvnCommand: Path by lazy {
129-
requireNotNull(findCommandOnPath("mvn")) { "Unable to find the 'mvn' command" }
129+
private val mvnCommandFromPath: Path? by lazy {
130+
findCommandOnPath("mvn")
131+
}
132+
133+
private fun mvnCommand(pom: Path): Path {
134+
return requireNotNull(mvnCommandFromPath ?: findProjectCommandWithName("mvnw", pom)?.also {
135+
LOG.info("Using mvn wrapper (mvnw) in place of mvn command")
136+
}) { "Unable to find the 'mvn' command or suitable wrapper" }
130137
}
131138

132139
fun parseMavenArtifact(rawArtifact: String, version: String? = null): Artifact {

shared/src/main/kotlin/org/javacs/kt/util/ShellPathUtils.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,17 @@ private fun findExecutableOnPath(fileName: String): Path? {
3333

3434
return null
3535
}
36+
37+
fun findProjectCommandWithName(name: String, projectFile: Path): Path? =
38+
if (isOSWindows()) {
39+
findFileRelativeToProjectFile("$name.cmd", projectFile)
40+
} else {
41+
findFileRelativeToProjectFile(name, projectFile)
42+
}
43+
44+
private fun findFileRelativeToProjectFile(name: String, projectFile: Path): Path? =
45+
projectFile.resolveSibling(name).toFile().takeIf { file ->
46+
file.isFile && file.canExecute()
47+
}?.let { file ->
48+
Paths.get(file.absolutePath)
49+
}

0 commit comments

Comments
 (0)