jenkins (or groovy) using pom.xml from previous execution

616 views Asked by At

I have a Jenkins job which receives as parameter a svn url to a maven project. It executes groovy scripts and some steps like mvn package, mvn deploy,... but I am finding a problem, when I launch the job for first time everything is fine, however when I execute it a second time and I change the input project, Jenkins or groovy seem to be using the previous execution's pom.xml, I mean, when I try to retrieve in groovy the artifactId from the projects's pom I always get the artifactId of the first project, it doesn't matter how many times I launch the job or if I do wipe out the current workspace, it will always return the first pom values.

The groovy code I use to read the pom.xml values is:

def thr = Thread.currentThread() //This line may be the problem?
def currentBuild = thr?.executable
def pomMAIN = currentBuild.getParent().getModules().toArray().size()-1
def mavenVer = currentBuild.getParent().getModules().toArray()[pomMAIN].getVersion()
def mavenArtifactId = currentBuild.getParent().getModules().toArray()[pomMAIN].getArtifactId()
def mavenGroupId = currentBuild.getParent().getModules().toArray()[pomMAIN].getGroupId()

I have tried to delete and create the job again but the behaviour is always the same, it only gets the right pom during the first execution.

Is there a way to clean up the job/groovy data?

EDIT 06/07/2015:

My job retrieves the svn code using the section Source Code Management: enter image description here

The Check-out Strategy should be enough to be sure that the code is updated, but I have also checked the job workspace to ensure the pom is the right one and it is.

1

There are 1 answers

0
Javi On BEST ANSWER

Finally I found the solution, now I have a groovy script at the begining of the job that refreshs it:

def jobNameToRefresh = "nameOfMyJob";
def hudson = hudson.model.Hudson.instance;

for(job in hudson.model.Hudson.instance.items) {   
    if (job.name == jobNameToRefresh) {
        def configXMLFile = job.getConfigFile();
        def file = configXMLFile.getFile();
        InputStream is = new FileInputStream(file);
        job.updateByXml(new StreamSource(is));
        job.save();
        return true        
    }      
} 

And, in the end of the job, I remove the folder "modules" with a shell script that basically does:

cd..
rm -r modules