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

前端 未结 9 1809
半阙折子戏
半阙折子戏 2020-12-14 16:13

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 direc

相关标签:
9条回答
  • 2020-12-14 16:23

    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)

    0 讨论(0)
  • 2020-12-14 16:28

    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.

    0 讨论(0)
  • 2020-12-14 16:35

    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

    0 讨论(0)
  • 2020-12-14 16:35

    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 讨论(0)
  • 2020-12-14 16:43

    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.

    0 讨论(0)
  • 2020-12-14 16:44

    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

    0 讨论(0)
提交回复
热议问题