I want to be notified about Items changed locations, to modify some properties of jobs with the new value.
I use the ItemListener:
@Extension
public class ItemOperations extends ItemListener {
public void onLocationChanged(Item item, String oldFullName, String newFullName) {
System.out.println("ITEM location changed " + oldFullName + " " + newFullName + " " + item.getFullName());
if (!item instanceof InheritanceProject) {
return;
}
System.out.println("It is an InheritanceProject");
InheritanceProject inherited = (InheritanceProject) item;
InheritanceProject.clearBuffers(inherited);
//And then fixing all named references
for (InheritanceProject p : inherited.getProjectsMap().values()) {
for (AbstractProjectReference ref : p.getParentReferences()) {
System.out.println("ParentReference " + ref.getName());
if (ref.getName().equals(oldFullName)) {
ref.switchProject(inherited);
}
}
for (AbstractProjectReference ref : p.compatibleProjects) {
System.out.println("compatibleProjects " + ref.getName());
if (ref.getName().equals(oldFullName)) {
ref.switchProject(inherited);
}
}
try {
p.save();
} catch (Exception e) {
e.printStackTrace();
}
}
}
AbstractProjectReference is a property of the job, and I want change the project which points to (it is a String property). After switching the project (switchProject) all seems work right.
When I visit config page of the job I can see the new name, so it is OK.
Now I shutdown Jenkins, and I start it again.
I visit config page of the job again, but I see the old name, so it is wrong.
Why the configuration of the job was not saved to disk?
P.D:
I have tried with:
try {
p.save();
} catch (Exception e) {
e.printStackTrace();
}
too, and the same result.