Is there any way to change the working directory of a Jenkins Maven build?

Is there any way to change the working directory of a Jenkins Maven build?

Use Case

There is a Maven build that will only work if the current working directory is the directory with pom.xml in it, so I want the Maven build to execute in that directory.

  • Turning Sonar off for certain code
  • Why do I see a “force promotion” button when using the Jenkins Promoted Builds plugin?
  • Git Variables in Jenkins Workflow plugin
  • How to promote a specific build number from another job in Jenkins?
  • Building tags using Continuous Integration
  • Lcov report from Karma is not getting uploaded into sonar
  • The directory with the pom.xml is in a subdirectory of a git project.

  • How do I assure that a Jenkins pipeline stage is always executed, even if a previous one failed?
  • Restore snapshot before build in jenkins using virtualbox plugin
  • What's (by default) on the classpath when you run a Jenkins job?
  • sonar build stability plugin gives 403 forbidden error for sonarqube version 3.1.1
  • Jenkins auto deploy tomcat 7
  • Selenium tests fail when run by Jenkins, but succeed when run by command line
  • 7 Solutions collect form web for “Is there any way to change the working directory of a Jenkins Maven build?”

    Create a target to checkout the parent.

    • Checkout the parent dir of your build target
    • Set Build -> Goal = clean
    • Run build and ascertain the destination workspace directory (second line of console output, in my sample /var/lib/jenkins/workspace/my_project)

    Create a target to build your path

    • DO NOT check “Delete workspace before build starts”
    • In Build -> Advanced – Check “Change folder of workspace” and paste your /yourtargetdirectory

    It worked for me.

    Not sure when this was added, but for the Jenkins in front of me (Jenkins ver. 1.627 per the bottom of the page), there’s now an option for defining where the root pom.xml is (help message says:

    If your workspace has the top-level pom.xml in somewhere other than
    the 1st module’s root directory, specify the path (relative to the
    module root) here, such as parent/pom.xml. If left empty, defaults to
    pom.xml

    ). I was facing the same issue but then realized the answer was staring me in the face (note that I did not have to hit advanced as Hua2308 suggested, probably because they changed the UI)

    For a reference, here is what works for me. In the “Invoke top-level Maven targets” build step, there is an “Advanced…” button. If you click this button, you can specify the relative path to your POM.xml file. This should ensure you are only building the desired project in the subdirectory.

    You can use Maven’s -f option

    -f,--file <arg>    Force the use of an alternate POM file (or directory with pom.xml).
    

    For example if your project has a structure:

    README.md
    sources
    |----pom.xml
    |----services
    |    |----pom.xml
    |    +----src
    |----webservices
    |    |----pom.xml
    |    +----src
    +----....
    useful
    docs
    

    then you can use Goals in Invoke top-level Maven targets:

    -f sources/pom.xml clean install
    

    Go to the configuration of your maven project, then:

    Build > click the “Advanced” button > check “Use custom workspace” > set the directory

    (I’m using Jenkins version 1.508)

    The Jenkins we are working with is in German so I’m trying to guess how the options are translated.

    Go to the configuration of your project and there go to enhanced project configuration (it’s the second section) and expand it. The last point should be something like Change folder of workspace. I think that might be what you are looking for.

    Otherwise you can always go to the Build options and change the path to your pom.xml

    Solution:

    cd subfolder; mvn -f ../pom.xml task
    

    Maven does not provide an option to do it the other way round, meaning the sensible way for this use-case, probably because Maven hates you.

    Git Baby is a git and github fan, let's start git clone.