How to fix maven error in jenkins on windows: ‘mvn’ is not recognized as an internal or external command …
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.
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).
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.