How can I checkout different Subversion tags/branches of the same Java project while using Ant/Jenkins?

17,597

Solution 1

You should parameterize your build by tag/branch name. The easiest way to do it is to add a parameter (say, SVN_BRANCH_DIR) to your Jenkins job which will have values such as trunk, branches/branch1, tags/sometag.

Now, if you use Jenkins ANT build step that parameter will be passed automatically to your ANT script as a property (by way of ANT -D option). So you can use ${SVN_BRANCH_DIR} in it (e.g. svn://myserver/myrepo/${SVN_BRANCH_DIR}).

Solution 2

Jenkins Subversion Plugin provides a "List subversion tags (and more)" project parameter since Version 1.24 (Mar 22, 2011).

Literally,

When used, this parameter will display a field at build-time so that the user is able to select a Subversion tag from which to create the working copy for this project. Once the two fields Name and Repository URL are set, you must (1) ensure the job uses Subversion and (2) set the Repository URL field of Subversion by concatenating the two fields of this parameter. For instance, if Name is set to SVN_TAG and Repository URL is set to https://svn.jenkins-ci.org/tags, then Subversion's Repository URL must be set to https://svn.jenkins-ci.org/tags/$SVN_TAG. Notice that you can set the Repository URL field to a Subversion repository root rather than just pointing to a tags dir (ie, you can set it to https://svn.jenkins-ci.org rather than https://svn.jenkins-ci.org/tags). In that case, if this repository root contains the trunk, branches and tags folders, then the dropdown will allow the user to pick the trunk, or a branch, or a tag.

For unattended integration builds you could use the default "trunk" parameter value.

I hope this helps.

Share:
17,597
Alain
Author by

Alain

I grew up in the snow in Montreal. I have been working in London in the last couple of years. Before moving to London, I was living in Lyon for 10 years.

Updated on June 04, 2022

Comments

  • Alain
    Alain almost 2 years

    Here is my dev configuration:

    Under Subversion,

    - I have my project_X/trunk (with my latest dev),
    - I have my project_X/tags (with different releases),
    - I am thinking of adding a branch folder.

    I am using Jenkins to build my project_X/trunk using an Ant script. My Ant script does many things, it checks-out, compiles, creates the documentation with graphs, runs the unit tests, performs pmd, creates a jar and zips everything.

    I would like to be able to use my Ant script on tags or branches (as well as the trunk) for the same Project.

    What's the easiest way to do this:

    I think it is just a question of checking out the right path to the Subversion repository, Right?

    - If I am correct, I should make the path to the Subversion dynamic.
    - In my Ant script should my Subversion path be a variable?
    - How do I pass the path value from Jenkins' interface?
    - Is there a plugin that lets me pass the Subversion path value from Jenkins to the Ant script?
    - Or should I just create a new job in Jenkins (with the same script but different path)?

    Hope

    Thanks in advance for your help,
    Best regards,

  • Alain
    Alain almost 12 years
    So basically I need to modify my ant script with the new Subversion url svn://myserver/myrepo/${SVN_BRANCH_DIR}. In Jenkins I have an Inject environment variables into the build process section. I can probably declare my new ${SVN_BRANCH_DIR} parameter here and give it the value "trunk" for now? So whenever I need to change the path I can find my parameter and change its value? Thanks for your help,
  • malenkiy_scot
    malenkiy_scot almost 12 years
    What I meant is to define it as a build parameter: Go to job definition page -> check 'This build is parameterized' -> push 'Add Parameter'. For now I'd go with 'Choice' parameter (with one choice - trunk). Injecting env variables will also work, but it has the following disadvantages: (1) It's harder to define (2) It's less visible (3) It won't be passed automatically to the ANT build step (you'll need to do something like SVN_BRANCH_DIR=${SVN_BRANCH_DIR} in properties section of the build step.
  • Alain
    Alain almost 12 years
    Super it's exactly what I needed! Thanks for the clarification!
  • Alain
    Alain almost 12 years
    I had another underlying question, before using Jenkins I was checking out the source code directly from the ant script with a task called checkout. Once I had configured the source control management section in Jenkins, it did not need the checkout task anymore. So basically with this new configuration as previously mentioned above, I should have no source control management configured anymore in jenkins and only use the checkout task in my ant script, is that correct?
  • malenkiy_scot
    malenkiy_scot almost 12 years
    It won't mesh well with SVN polling. However, you can define SVN hooks and do it via 'pushing' (as described here). The easiest way, though, is to do it "by hand" inside the build (e.g. in your ANT script or a separate shell build step).
  • Alain
    Alain almost 12 years
    Unfortunately our source control is under Windows. I am getting the impression from the link that it would be easier to implement the script in a post-commit.bat? If I choose this method, how is the batch script called, is it called by Subversion automatically? I never performed hooks before. Thanks
  • malenkiy_scot
    malenkiy_scot almost 12 years
    Meaning, your server is on a Windows machine? First thing to check is whether you have an SVN administrator that can help. If not, look in hooks directory under your repository root - there should be templates there. I'd write the hook in some full-blown language like Python or Perl. Now, if it is too much bother to deal with hooks - you can still do the polling. It is not as elegant, but will still work.