1

I am having the requirement of running a maven built using go lang without installing maven to the running system. With the use of os.exec package in go I am able to run any program which is already defined in my PATH variable. But setting M2_HOME in PATH variable is not an option here and I am having the maven distribution extracted in specific location. According to the answer given in here it is possible to run a maven build by providing the specific location of mvn, how do I achieve the same from go.
Thanks in advance

Kasun Siyambalapitiya
  • 3,956
  • 8
  • 38
  • 58
  • 1
    What OS? Did you try passing *full path* of the specific location to the `exec.Command` argument, e.g. `your/specific/folder/mvn`? Please provides your code snippets for executing the command, then if there is error, show us the error message. – putu Nov 08 '17 at 08:34
  • Are you assuming that `maven` is already installed on the target machine or do you bundle the `maven` distribution with your software? – dev.bmax Nov 08 '17 at 11:44
  • See also [os.ExpandEnv](https://golang.org/pkg/os/#ExpandEnv). – Peter Nov 08 '17 at 12:46

1 Answers1

0

As with the comment by @putu and with specifying the required maven goals one by one as separate arguments to the exe.Command() I was able to run maven build without installing maven or setting M2_HOME in $PATH variable. Below is the code snippet for anyone trying to achieve the same.

// Use os package to run the maven build using location where maven(headless) resides
func updateDistribution() error {
    command := exec.Command("apache-maven-3.5.2/bin/mvn", "clean", "install")
    command.Dir = "."
    output, err := command.Output()
    if err != nil {
        return err
    }
    fmt.Printf("%s", output)
    return nil
}
Kasun Siyambalapitiya
  • 3,956
  • 8
  • 38
  • 58