Updating Jersey 2 in GlassFish 4

Different life-cycles of Jersey 2 and GlassFish 4 arise a question how to make sure that ones GlassFish instance contains always the latest version of Jersey. This question is even more important in case you don’t want to download the nightly/promoted build every-time a new version of Jersey is released but you still want to use the latest and greatest Jersey.

Note: The script below is not compatible with Jersey 2.6 at the moment. I’ll update it as soon as possible.

In this article we’ll see if your GlassFish installation is ready for update and what steps you need to take to actually update it:

Jersey in GlassFish nightly

New version of Jersey is released approximately once in 4-5 weeks (see road-map). Together with releasing a new version of Jersey (announced on our mailing list or via @gf_jersey on Twitter) we’re also integrating Jersey with GlassFish trunk (currently version 4.0.1) to make sure nothing gets broken. This means that all major/minor Jersey versions are ready to be used in the nightly build of GlassFish within few days after the release (usually it takes 1-2 day to have a version of GlassFish with the latest Jersey). The nightly build of GlassFish can be downloaded from:

What Jersey am I using?

People are often curious what Jersey version do they use in a GlassFish instance they’d downloaded. The simplest way to find out is by running the following command in your shell:

From the output we can see that the Bundle-Version is 2.0.0 which means that the GlassFish I have contains Jersey 2.0 (released in May 2013).

Other quick way to find out is by deploying an JAX-RS application and taking a look into the logs where you ought to find a line similar to:

As you can see my other instance of GlassFish contains Jersey 2.5.1.

Is it possible to update Jersey in my GlassFish?

Unfortunately it’s not possible to update Jersey 2.0 in release of GlassFish 4.0 (or Java EE 7 RI GlassFish) downloaded from here. HK2 (dependency injection framework Jersey uses) went through some big changes between versions 2.1.x and 2.2.0 that affected also the core GlassFish classes and because of this fact the simple change of jars does not work in this case.

To make sure you’re able to update Jersey in your GlassFish you need to check whether you’re using version 4.0.1.

Again see the Bundle-Version which is 4.0.1.b01 (first promoted build of 4.0.1).

For each version of Jersey that is in one of the promoted or nightly builds of 4.0.1 it is possible to update to the latest version of Jersey (currently 2.5.1). Here are the builds that can be updated:

Updating Jersey in GF 4.0.1

Basically, to update Jersey there are five things that need to be done:

  • stop GF domain
  • replace Jersey bits
  • replace HK2  bits (dependency injection framework) to the one Jersey uses
  • erase OSGi cache
  • start GF domain

Erasing OSGi cache (glassfish4/glassfish/domains/domain/osgi-cache/felix) is a good practice in case you’re trying to replace one or more modules that are present in GF and you don’t want to see weird OSGi exceptions.

Since the number of Jersey and HK2 bits in GlassFish is pretty high (~18 for Jersey and ~16 for HK2 and it’s repackaged modules) I’ve prepared a simple script that takes care of downloading these libraries for you (incl. backing-up the ones you already got, just in case) and erasing the OSGi cache. Few comments on the script:

  • versions of Jersey and HK2 are hard-coded
  • it needs to be executed from glassfish4/glassfish directory
  • domain should be located at  glassfish4/glassfish/domains/domain1

The whole script (see this Gist):

You can download it from here, i.e.:

In case you’re experiencing problems after updating Jersey replace new modules with backed-up versions (.bak suffix).

After running this script you’ll see an output similar to:

Now, let’s try to deploy bean-validation-webapp sample to see if our GlassFish really uses the latest (2.5.1) version of Jersey:

Enjoy!

What if GlassFish with updated Jersey stinks?

Revert the process. The previous script also makes a backup copy of all jars it’s replacing. The script in this Gist restores the jars from backup if needed.

Run it.

And make sure you have old Jersey jars restored.

 

  • Pingback: Using updated release of Jersey, Tyrus, Weld, … in GlassFish 4 | Google Trender

  • http://www.gfz-potsdam.de rkorn

    Can you tell us, what have to be done manually to update glassfish 4.0.1 to use jersey 2.6?
    Even one month after jersey 2.6 was released the newest glassfish nightly does yet contain jesers 2.5.1 :(

    • Michal Gajdoš

      I’ll try to cover it in the next week. However GF nightly with Jersey 2.7 will be most likely released at the start of next week.