Optional dependency and NoClassDefFoundError in Descriptor - how to avoid this?

196 views
Skip to first unread message

Gerd Zanker

unread,
Feb 5, 2012, 10:37:01 AM2/5/12
to Jenkins Developers
Hello all,

I need an idea how to avoid a NoClassDefFoundError exception.

The trac-plugin has now a dependency to the git-plugin (see
JENKINS-11887) and I want to change it into an optional dependency.
Therefore I changed the pom.xml as described in
https://wiki.jenkins-ci.org/display/JENKINS/Dependencies+among+plugins
(see https://github.com/GerdZanker/trac-plugin/blob/JENKINS-12588/pom.xml).

The trac-plugin works now as expected, but throws this exception
during runtime if the git-pluin is not installed
Caused by: java.lang.NoClassDefFoundError: hudson/plugins/git/browser/
GitRepositoryBrowser
at java.lang.ClassLoader.defineClass1(Native Method)
...
at java.lang.ClassLoader.loadClass(Unknown Source)
at hudson.plugins.trac.TracGitRepositoryBrowser
$DescriptorImpl.<init>(TracGitRepositoryBrowser.java:97)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

The problem is that I have no idea how to check if the plugin is
installed with "if (Hudson.getInstance().getPlugin("git-plugin") !=
null) { ...".
I can't add the check to the Descriptor (e.g. at
https://github.com/GerdZanker/trac-plugin/blob/JENKINS-12588/src/main/java/hudson/plugins/trac/TracGitRepositoryBrowser.java#L97)
because no code can be placed before the super constructor and the
TracGitRepositoryBrowser is derived from GitRepositoryBrowser which
means that each access to TracGitRepositoryBrowser leads to the
exception.

How do I create the Descriptor only if the git-plugin is installed?
How/when can Descriptors be created? What class and function do I have
to write to place the "if (Hudson.getInstance().getPlugin("git-
plugin") != null) " statement in it?
Does somebody knows code I can use as an example/reference
implementation?

Thanks.

Bye, bye
Gerd

Sandell, Robert

unread,
Feb 6, 2012, 9:33:22 AM2/6/12
to jenkin...@googlegroups.com
Change the @Extension annotation to @Extension(optional = true)

/B

Ullrich Hafner

unread,
Feb 6, 2012, 10:47:06 AM2/6/12
to jenkin...@googlegroups.com

Gerd Zanker

unread,
Feb 6, 2012, 3:29:54 PM2/6/12
to Jenkins Developers
Hello,

Thank you for the tips. The "Dependencies among plugins" Wiki page is
updated.
I really enjoy the Jenkins community because since I started
contributing in December last year, I got all the answers and support
I need within one day. Great!

Gerd

On 6 Feb., 16:47, Ullrich Hafner <ullrich.haf...@gmail.com> wrote:
> Here is an example that uses the optional parameter:
>
> https://github.com/jenkinsci/warnings-plugin/blob/master/src/main/jav...
>
> Ulli
>
> On 02/06/2012 03:33 PM, Sandell, Robert wrote:
>
>
>
>
>
>
>
> > Change the @Extension annotation to @Extension(optional = true)
>
> > /B
>
> >> -----Original Message-----
> >> From: jenkin...@googlegroups.com [mailto:jenkinsci-
> >> d...@googlegroups.com] On Behalf Of Gerd Zanker
> >> Sent: den 5 februari 2012 16:37
> >> To: Jenkins Developers
> >> Subject: Optional dependency and NoClassDefFoundError in Descriptor -
> >> how to avoid this?
>
> >> Hello all,
>
> >> I need an idea how to avoid a NoClassDefFoundError exception.
>
> >> The trac-plugin has now a dependency to the git-plugin (see
> >> JENKINS-11887) and I want to change it into an optional dependency.
> >> Therefore I changed the pom.xml as described in
> >>https://wiki.jenkins-ci.org/display/JENKINS/Dependencies+among+plugins
> >> (seehttps://github.com/GerdZanker/trac-plugin/blob/JENKINS-
Reply all
Reply to author
Forward
0 new messages