Only do Maven incremental build when on same workspace
I have a Maven build that I want to run on almost every commit (poll SCM every 5 minutes). It is a rather slow build (>15mins) so in order to speed things up I use goal "test" (no packages are created/deployed).
I then tried using the incremental build feature to only compile/test what changed. So in the advanced section of the Maven build step I checked:
"Incremental build - only build changed modules"
Unfortunately the build fails randomly and I realized now that it happens when we move from one build agent to another. The problem is that if we do build #55 in build agent A and then build #56 happens in build agent B (e.g. because A was busy) a different workspace is used. In this workspace the repository does not have the output of build #55, so when doing build #56 we get errors because it is missing modifications.
How do I go about fixing this? Basically I want to use the incremental build, but ONLY if I am running in the same workspace. If I move to another workspace I would like to do a full build instead...
Is there maybe a pre-build step that can enforce this?