Maven Projects trigger RunListener twice

23 views
Skip to first unread message

Kyle Flavin

unread,
Sep 3, 2019, 7:33:26 PM9/3/19
to Jenkins Developers
Hi,
I'm working on a Jenkins plugin to gather metrics from completed jobs.  This primarily involves gathering label information from Executors.  I'm using  RunListener that looks something like this:

@Extension
public class MyRunListener extends RunListener<Run> implements Describable<MyRunListener> {
 
@Override
 
public void onCompleted(Run run, @Nonnull TaskListener listener) {
     
// collect executor labels, send them off to an API endpoint
 
}
}

This is working fine for most of my projects.  Pipeline, Multibranch, and Freestyle jobs each trigger onCompleted() when the build finishes.  I've noticed some odd behavior from Maven projects though; they trigger onCompleted twice for each build, instead of just once.

Here are some things I've noticed:
  • The first time my code is triggered, run.getParent().getClass() is of type hudson.maven.MavenModule, and the executor is null (run.getExecutor() == null), so I can't pull any information from the Executor.  The url in the build data also adds the artifact id into the url like so:  http://localhost/jenkins/job/maven-job/com.company.jenkins.plugins$artifactidname/5/ which makes for a non-working link.
  • The second time it is triggered, run.getParent().getClass is of type hudson.maven.MavenModuleSet, and run.getExecutor() returns an Executor with node information.  The url in the build data points to the correct build location: http://localhost/jenkins/job/maven-job/5/.  This appears to be the information I want.
I'm wondering what the difference is between MavenModule and MavenModuleSet.  Can I safely ignore anything of type MavenModule for my purposes, without losing any job data?  In other words, are there any scenarios where a Maven project would NOT behave like this, and only trigger my Listener once, as a MavenModule or MavenModuleSet, but not both?

Thanks!
Kyle

Jesse Glick

unread,
Sep 4, 2019, 11:54:37 AM9/4/19
to Jenkins Dev
`maven-plugin` does weird things with nested jobs/builds. We advise
people to not use this plugin.

You may want to test against `matrix-project`, which also does
something similar, though in that case each sub-build would in fact
have different executor labels.

I am not sure what you tested for Pipeline but if you are trying to
collect executor labels, `RunListener` is not going to do what you
want, since a given Pipeline build might have run zero or more `node`
blocks. If you do not need this information in real time, I guess you
can wait for the whole build to complete, then traverse the flow graph
looking for `WorkspaceAction`.

Kyle Flavin

unread,
Sep 4, 2019, 6:07:30 PM9/4/19
to Jenkins Developers
Got it.  We're encouraging people to move towards pipeline, but we still have a lot of these Maven jobs in our environment (across multiple masters).  I'll share this internally.

Collecting labels on the pipeline jobs has been a bit of a challenge, but what I've settled on is what you describe.  I wait until the build is complete, and then I traverse the flow graph.  We don't really need the data in real time, so it seems to work well enough.  If you can suggest a different approach though, I'm open to improving it.

Thanks!
Reply all
Reply to author
Forward
0 new messages