33

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.

The directory with the pom.xml is in a subdirectory of a git project.

Max Nanasy
  • 5,871
  • 7
  • 33
  • 38

9 Answers9

13

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.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
ton
  • 3,827
  • 1
  • 42
  • 40
  • 6
    I don't see the "Change folder of workspace" option anywhere (Jenkins ver. 1.605). – Erez Cohen May 11 '15 at 08:48
  • @rogerdpack, not sure if I understood your question, but no, it's not a manual copy (well, of couse you copy the path, but not the files), it's just a reference to a folder inside the complete project workspace – ton Sep 15 '16 at 22:47
  • So are you creating two different jenkins "projects" here, in essence? – rogerdpack Sep 16 '16 at 23:43
11

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)

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Foon
  • 6,148
  • 11
  • 40
  • 42
  • In my case I tried this and it appeared to not work, but it turned out I had a custom "post build" step that was building everything from the root, whereas the initial build was working properly. So note to self: addd "-f XXX" to manual build steps if you use this option :) – rogerdpack Sep 16 '16 at 23:42
  • The OP asked about CWD. This answer doesn't solve issues with poorly written project hierarchies where running **mvn -f submodule/pom.xml** and **cd submodule && mvn** yield different results (eg: only one way works due to relative paths in poms). Jenkins Maven and Freestyle jobs do not appear to provide a way to change the working directory the way Pipeline jobs do, so Pipeline is really the only solution. Note that Maven and Freestyle jobs execute **mvn -f submodule/pom.xml** differently - specifically in different directories. – Akom Jan 12 '18 at 20:53
5

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.

Hua2308
  • 451
  • 6
  • 14
  • Perhaps you were referring to build pre-step here? That's where I find the option (regardless, see Foon's answer for how to do it in a non pre-step FWIW...) – rogerdpack Sep 16 '16 at 23:41
2

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
madhead
  • 31,729
  • 16
  • 153
  • 201
  • 2
    This does not change the working directory; the script that runs as part of the Maven build assumes the current working directory is the directory with `pom.xml` in it. – Max Nanasy Oct 09 '13 at 18:54
  • This does not answer the question at all. It answers *a* question, but not the one asked... – Jasper Aug 05 '15 at 09:00
  • This is what Foon's answer in essence "adds" (except it gets added to all phases, ex; sonar post-build so you don't have to worry about manually adding it in as many places). And it did work for me :) – rogerdpack Sep 16 '16 at 23:37
2

I came here because I had the same situation described, The version of Hudson / Jenkins that I have has the same problem, it does not allow setting the working directory before calling maven.

The workaround is to configure a parameter that has the desired working directory, create a new command "mvn" that makes the change directory and then invoke the original command "mvn" of maven with all the arguments.

steps to follow:

  1. In the project it should be noted that the project needs to be parameterized, in my case create the parameter WORKING_DIRECTORY with value $ {WORKSPACE}/maven, being "maven" the subfolder that will be the working directory. enter image description here

  2. Create a "custom" version of maven to modify the "mvn" command:

cp -ip /opt/apache-maven-3.5.4 /opt/maven_custom

mv /opt/maven_custom/mvn /opt/maven_custom/mvn_original

vi /opt/maven_custom/mvn:

-- content new mvn command --

cd $WORKING_DIRECTORY

/opt/maven_custom/bin/mvn_original "$@"

-- end content new mvn command --

chmod ugo+x /opt/maven_custom/mvn

  1. Add the new maven_custom in hudson/Jenking:

    Hudson Admin --> System Configurations --> Maven --> Add maven

  2. Use the custom maven in the project

With these changes maven runs in a different working directory

1

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)

tbrugz
  • 470
  • 1
  • 7
  • 17
  • 1
    Are you referring to `Advanced Project Options > Use custom workspace`? If so, according to the documentation, "This is the directory where the code is checked out and builds happen", which would not solve my use case of performing a build with the working directory set to a subdirectory of the project. – Max Nanasy Aug 16 '13 at 22:22
1

When the subdirectory is a direct child of the parent root, you could use mvn --projects since that changes into the subdirectory. Here a proof (see last line):

U:\git\ics-VCCUSTOM-422>mvn --projects icsIntegrationTest clean verify
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< com.afklm.vccustom:icsIntegrationTest >----------------
[INFO] Building icsIntegrationTest 13.3.4-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ icsIntegrationTest ---
[INFO] Deleting U:\git\ics-VCCUSTOM-422\icsIntegrationTest\target
[INFO]
[INFO] --- buildnumber-maven-plugin:1.3:create (default) @ icsIntegrationTest ---
[INFO] ShortRevision tag detected. The value is '7'.
[INFO] Executing: cmd.exe /X /C "git rev-parse --verify --short=7 HEAD"
[INFO] Working directory: U:\git\ics-VCCUSTOM-422\icsIntegrationTest
0

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

André Stannek
  • 7,773
  • 31
  • 52
  • A. `Change folder of workspace` changes the folder where the project is downloaded to, not the working directory of the process that executes the Maven build. B. No matter which pom.xml is built, the working directory is the top level directory of the workspace. – Max Nanasy Jun 26 '13 at 18:30
  • Hmm, OK. Sorry I couldn't help. – André Stannek Jun 27 '13 at 06:07
0

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.

tkruse
  • 10,222
  • 7
  • 53
  • 80