Maven error in Jenkins: The goal you specified requires a project to execute but there is no POM in this directory

Maven error in Jenkins

Problem Scenario

As a developer, we usually start working on a project using an IDE of our choice. Hence, we initially execute maven goals like compile, package, deploy etc while working from the IDE in the project directory or workspace.

When we try to run the same maven goals from Jenkins, we are actually executing them under the Jenkins workspace. So a goal like mvn compile will look for pom.xml within the Jenkins project (job) workspace.

As pom.xml is not available in the Jenkins project directory/folder by default, the goal will not get executed and the following error will be thrown.

The goal you specified requires a project to execute but there is no POM in this directory (...\Jenkins\workspace\...). Please verify you invoked Maven from the correct directory.

Apart from the above mentioned scenario, this error can also occur if for some reason we have not named the POM file as pom.xml

Resolution

The process usually followed to run CI/CD pipelines is to check-in the code to a version control repository. We then fetch the code from version control repository and build the code using an orchestration tool like Jenkins. When we fetch the source code from a version control repository supported by Jenkins, the code by default gets fetched in our Jenkins project workspace. Hence we will not face any problem while running maven goals directly, the same way that we execute them from our IDE.

If for some reason, we need to run the code from a different directory, the resolution is to provide the path for pom.xml while executing the maven goals. We can achieve this by using  -f  or file option. So instead of mentioning the goal as

mvn compile

we need to mention it as

mvn – f < pom.xml path > compile

The – f option will enable Jenkins to use the pom.xml mentioned next to it for executing the maven goal. Moreover, if we are running the code from Jenkins agent in windows, we will need to escape backslashes in the file path by using double backslashes as shown in code excerpt.

mvn - f ("C://projectworkspace//projectfolder//pom.xml") compile

We can also mention just the path to the folder which contains pom.xml. Jenkins will automatically search for pom.xml file in this folder.

Resolution based on Jenkins Project Type

Pipeline project

If we are calling maven goals from Jenkins pipeline, most probably we would be using pom.xml within multiple stages in your pipeline code. Hence it is recommended to define the path to pom.xml using def keyword, before we start writing the code for stages. 

def pomfilepath= <pom.xml path>

Another way is to use the dir directive as mentioned below. The statements under this directive will use the path as a string after dir directive. So we can execute maven goals the same way as we do while building the application from our project IDE.

dir("/../.."){
  mvn clean test
}

Freestyle project

If we are executing maven goal from a freestyle project, we have 2 options.

Goto Add Build Step -> Invoke top-level Maven targets

Here we can mention the maven goal that we want to execute. Under Advanced tab, we will get the option to specify the path of POM file.

Goto Add Build Step -> Execute windows batch command/ Execute shell

We can use batch command or shell script depending upon the OS where we want to execute the maven goals.

Build step “Execute Windows batch command”

We will provide the file path in a similar manner as in the pipeline project. As also mentioned earlier, if we are running the code from jenkins agent in windows, we will need to escape backslashes in the file path by using double backslashes as shown below:

mvn - f "...\\project\\pom.xml" clean test
Build step: Execute shell


For linux/ unix/ macOS, we will invoke maven targets from shell script as per following:

mvn - f .../project/pom.xml clean test

Conclusion

If we are running a project in Jenkins, we need to fetch the source code with pom.xml into Jenkins workspace or explicitly mention the path of the project directory where POM file is located.

Let us know if you found this post useful.

You may also like...

Leave a Reply

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