douchuanchai2793 2017-11-08 06:54
浏览 413
已采纳

如何从Go语言代码运行Maven构建

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

  • 写回答

1条回答 默认 最新

  • dongpu8935 2017-11-10 07:33
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器