In my opinion, the overall problem here is trying to define the build
process in the same place as what you're building. I have always felt
that the Jenkinsfile and related scripting should be in a separate
repository from what you're building. If you simply accept this
approach, you will never have these problems again.
Note that it still makes sense to have your Maven or Gradle build
script in the same repository as what it's building. Developers need
to be able to build a codebase out of the box, but the CI environment
is different.