Running Maven from Java code in Windows?

前端 未结 4 827
名媛妹妹
名媛妹妹 2021-01-05 11:21

I am trying to run maven from my java class based on this suggestion:

How to run maven from java?

Runtime.getRuntime().exec(\"mvn\");
相关标签:
4条回答
  • 2021-01-05 11:35

    Error 2 means that the executable cannot be found by the JRE environment. This means that the PATH environment variable does not contain the Maven binary directory.

    2 choices here:

    1. Make sure that the Maven bin directory is in the PATH environment variable
    2. Use an absolute path to the mvn command.

    Alternatively, this could also be due to a permission denied, but it is less likely the case.

    0 讨论(0)
  • 2021-01-05 11:46

    User "mvn.cmd" instead of mvn or mvn.bat. It works fine.

    0 讨论(0)
  • 2021-01-05 11:50

    Try:

    Runtime.getRuntime().exec("cmd \c mvn");
    

    Edit: In response to the firs question...

    Yes. See: Process#getInputStream. Basically you will need to consume the output from the sub-process being created.

    I also like this article: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

    0 讨论(0)
  • 2021-01-05 11:52

    You can view mvn.bat and echo which java command is actually executed and run it directly.

    In mvn.bat:

    %MAVEN_JAVA_EXE% %MAVEN_OPTS% -classpath %CLASSWORLDS_JAR% "-Dclassworlds.conf=%M2_HOME%\bin\m2.conf" "-Dmaven.home=%M2_HOME%" %CLASSWORLDS_LAUNCHER% %MAVEN_CMD_LINE_ARGS%

    In my machine executing dependency:tree is:

    "java -classpath \"C:\dev\tools\apache-maven-3.1.1\boot\plexus-classworlds-2.5.1.jar\" -Dclassworlds.conf=C:\dev\tools\apache-maven-3.1.1\bin\m2.conf -Dmaven.home=\"C:\dev\tools\apache-maven-3.1.1\" org.codehaus.plexus.classworlds.launcher.Launcher dependency:tree"

    0 讨论(0)
提交回复
热议问题