Gerolmed
Gerolmed

Reputation: 403

Cannot run program "mvn" although maven on path

Before marking this as duplicate please read carefully. A gradle task (kotlin dsl) I am executing is executing a maven command in a sub directory. The weird thing is I have maven on the PATH variable both on System and User. If I navigate into that directory using the CMD or git bash I can execute any maven command. So it can't be the issue of it simply not being in the environment variables. I actually had that issue in every project where a mvn command is executed by code.

fun cmd(vararg args: String, directory: File, printToStdout: Boolean = false): Pair<Int, String?> {
    val p = ProcessBuilder()
            .command(*args)
            .redirectErrorStream(true)
            .directory(directory)
            .start()
    val output = p.inputStream.bufferedReader().use {
        val lines = LinkedList<String>()
        it.lines().peek(lines::add).forEach { line ->
                println(line)
        }
        lines.joinToString(separator = "\n")
    }
    val exit = p.waitFor()
    return exit to output
}

Calling it like so:

cmd("mvn", "install:install-file", "-q", "-Dfile=${project.projectDir.resolve("work/1.15.2-mojang-mapped.jar").absolutePath}", "-Dpackaging=jar", "-DgroupId=me.minidigger", "-DartifactId=minecraft-server", "-Dversion=\"$minecraftversion-SNAPSHOT\"", directory = project.projectDir)

It results in:

java.io.IOException: Cannot run program "mvn" (in directory "....."): CreateProcess error=2, Das System kann die angegebene Datei nicht finden

Edit: It shouldn't be the code, as others dont have this issue. I am btw using windows 10

Update: so it appears that while "mvn" works when calling it manually, it has to be "mvn.bat" if called from code. Being forced to manually change and check that in the code does not seem like an optimal solution though. Especially because some of the code calling mvn is downloaded in the task and can't be edited manually before

Upvotes: 2

Views: 1736

Answers (1)

carlspring
carlspring

Reputation: 32647

Check what your ProcessBuilder.environment shows as available variables. You may have to define these variables yourself.

Upvotes: 1

Related Questions