Replacing the package name/variable names blindly seems a bit risky, as it could replace other overlapping strings as well, which may lead to various issues. Assuming your package name is unique and you don't have any overlapping names and it doesn't result in any directory name changes, you can use different methods to replace the package name.
Option 1
Using shell to achieve this. There are plenty of different ways to do this, following is one option with grep
and sed
sh '''
grep -rl ${PACKAGE_NAME_TO_REPLACE} ${DESTINATION_DIR} | xargs sed -i "s&${PACKAGE_NAME_TO_REPLACE}&${PACKAGE_NAME_NEW}&g"
'''
You can take a look at this and this to understand different methods you can use.
Option 2
If you want a more controlled approach, you can achieve this with some groovy code as well. Simply run the following within your Pipeline.
def dirToSearchIn = "/where/to/replace"
// Change the content on specific files. You can improve the regex pattern below to fine-tune it. With the following pattern only files with extensions .java and .md will be changed.
def filterFilePattern = ~/.*\.java|.*\.md$/
def oldString = "replaceme"
def newString = "newme"
new File(dirToSearchIn).traverse(type: groovy.io.FileType.FILES, nameFilter: filterFilePattern) { file ->
println "Processing file: " + file.getPath()
def fileContent = file.text;
if (fileContent.contains(oldString)) {
println "Replacing the content of the file: " + file.getPath()
file.write(fileContent.replaceAll(oldString, newString));
} else {
println "Skipping file: " + file.getPath()
}
}