0

In a Jenkins freestyle project, how to build an ant project that is in a subfolder?

With SVN, we would just check out the folder with the ant files, but now with GIT, checking out a subfolder of a repo is not supported (as per questions on this site, like this one) and I also see no option to CD into a subfolder before the ant task, or a similar option in the "Invoke Ant" step.

David Balažic
  • 1,319
  • 1
  • 23
  • 50
  • Consider doing a sparse checkout, if you are worried about the space that it takes to checkout a whole commit. – eftshift0 May 23 '23 at 10:48

1 Answers1

0

IMHO the linked question does show how it works with git (it suggests sparse checkouts, similar to what eftshift0 suggests in a comment or another popular Q&A about it).

But that only as an early commentary, there is an alternative to sparse checkouts working for checking out sub directories from a git repository. It works with git(1) pipelining to tar(1). Given we're talking about build systems here, it should be very likely you've got both tools at hand.

The actual git command is git-archive and this is conceptually a tarpipe.

You can specify the revision to "checkout", the path you want to "checkout" and where you want to checkout the files to (in this answer, an imaginary build/ant target directory):

git archive --format tar \
    --prefix build/ant/ \
    HEAD \
    subdirectory/path/to/check-out \
  | tar x -vf -
  • git archive:
    • --format tar use tar (tape archiver) format. required.
    • --prefix build/ant/ the target directory, that is where to checkout to. If not with slash (/) at the end, the first component would be prefixed by this only (so not resulting in a directory). If you're checking out into a git repository work-tree, mind that you're checking out into a git repository work-tree. The target directory will be created, incl. all necessary parents.
    • HEAD the revision to checkout, replace with the revision you have.
    • subdirectory/path/to/check-out the subdirectory you want to checkout from the repository.
  • tar x -vf-:
    • x - extract
    • -v - verbose, outputs the files being extracted on standard output
    • -f- - file to read, here - (minus sign) stands for standard input.
    • tip: you can replace x with t for test, that is showing all file information that would be "checked out", including the final paths.

From your question it is not fully clear to me which limitations there are next to not being able to change the working directory. With the git archive | tar pipe, you can extract to any directory, including root, however you need to manage what happens then. E.g. when I had this in an answer recently, this was over the top for the user leaving them with a chaotic state in the work-tree of their build-system, they had problems to handle it any longer. YMMV.


  1. https://stackoverflow.com/a/76046285/367456
hakre
  • 193,403
  • 52
  • 435
  • 836