How to fix maven error in jenkins on windows: ‘mvn’ is not recognized as an internal or external command …

Maven error in Jenkins

If you have installed jenkins on windows and are facing this error while running maven command from your jenkins pipeline: ‘mvn’ is not recognized as an internal or external command, operable program or batch file, then the first step for you is to check whether the environment variables are setup correctly. To check environment variables goto Computer => properties=> Advanced System Settings=> Environment Variables.

Then check that JAVA_HOME and M2_HOME variables are pointing to their installation directory. Similarly, PATH variable should include the path of JDK as well as M2_HOME/bin directories.

Secondly you need to ensure that JAVA_HOME and M2_HOME is setup properly in Jenkins. For this goto Jenkins=>Manage Jenkins => Global tool Configuration.

Now check JDK and Maven installations.

You have the option of checking install automatically where a maven and jdk installation will be done specifically for jenkins and the system installation will not be used. However, here we go ahead with the option of explicitly specifying the path to the installations as this is the more common way where we have control over the installed versions.

JDK should be setup by providing installation path manually or by selecting install automatically option and providing the sub option to be used for installation. Similarly maven installation has to be setup.

java home in jenkins
maven home in jenkins

Most importantly if you are running jenkins on windows, and still getting the same error after performing the above two steps and have installed jenkins on windows machine as a windows service, you need to make sure that the environment variables are defined at system level. This means the process can’t see the environment variables of your user (user level).

environment variables for jenkins maven

Make sure the environment variables for M2_HOME and PATH (append M2_HOME/bin; and JDK path; to value of PATH variable) are defined at system level.

If still facing the problem, you should check if you have specified the maven installation to be used within jenkins jobs.

For declarative pipeline jobs you can use the tools directive to define maven and jdk tools before using them in the pipeline code.

You can use the withMaven() method with scripted pipeline where you can define the tool name to be used in your pipeline from the tool names configured in Global Tools Configuration section. This option would require you to use the Pipeline Maven Integration Plugin.

For freestyle job, you can use the Build step to Invoke top level maven targets where you can choose the maven installation to be used from the ones configured in Global Tools Configuration.

Let us know if you found this post useful.

You may also like...

2 Responses

  1. Christian says:

    Hello, This still failed for me.
    — Win10 Pro
    — apache-maven-3.6.3
    — apache-tomcat-8.5.64
    — Jenkins 2.277.1 LTS for Windows
    Installed and configured and input all the settings per shown above.
    JDK, Maven… all set!
    System Variables all set!
    Run Build and get the ” ‘mvn’ is not recognized as an internal or external command ”
    But in the execute windows batch command,
    Instead of entering “mvn package”
    I entered “C:\apache-maven-3.6.3\bin\mvn package”
    And then…IT WORKS!!! Why is that? Where is the disconnect… I cannot find it!

    • says:


      The scenario that you have mentioned is pointing towards issue in setup of environment variables.

      1.You can check for any typos in the path setup for M2 and M2_HOME.

      2.Try testing the mapping of environment variables by running “echo %M2_HOME%” and “echo %PATH%” from command line. Make sure to open a new command window after making any change to environment variables.

      3.Check for maven tool specified in the jenkins jobs.

Leave a Reply

Your email address will not be published. Required fields are marked *