Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use alternate version of Mojarra with Glassfish 4 (Netbeans/Maven)

I've been Using Eclipse/Ant for some time and decided to take a look at Netbeans/Maven for my JSF development. Along the way, I thought I'd try to use a version of Mojarra that is newer than the one I have installed in glassfish/modules. I can't seem to get that to work, and since I'm new to both Maven and Netbeans, I'm not sure if it's something with those, or something else.

I've made a very simple app which uses a backing bean to print out the Mojarra version from the index page:

Info.java:

@Named
@RequestScoped
public class Info
{
  public String getVersion()
  {
    return FacesContext.class.getPackage().getImplementationVersion();
  }
}

index.xhtml:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
  <h:body>
    <h:outputText value="The Mojarra version is #{info.version}" />
  </h:body>
</html>

When I run this, I see the page contents of

The Mojarra version is 2.2.5

as I'd expect (I put the 2.2.5 jar into glassfish/modules some time back). I then added the dependency for Mojarra 2.2.7 that I found from the Mojarra Web Site:

<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.faces</artifactId>
  <version>2.2.x</version>
</dependency>

with "x" replaced by "7". The resulting POM file looks like this (most of it is boilerplate from Netbeans):

POM.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany</groupId>
  <artifactId>mavenproject1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>mavenproject1</name>

  <properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-web-api</artifactId>
      <version>7.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.glassfish</groupId>
      <artifactId>javax.faces</artifactId>
      <version>2.2.7</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <compilerArguments>
            <endorseddirs>${endorsed.dir}</endorseddirs>
          </compilerArguments>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <phase>validate</phase>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <outputDirectory>${endorsed.dir}</outputDirectory>
              <silent>true</silent>
              <artifactItems>
                <artifactItem>
                  <groupId>javax</groupId>
                  <artifactId>javaee-endorsed-api</artifactId>
                  <version>7.0</version>
                  <type>jar</type>
                </artifactItem>
              </artifactItems>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

When I run the app, there is no difference in the output, it still shows version 2.2.5. I found this article with an answer from @BalusC that mentions Glassfish must be explicitly told to override the installed version of faces to use an alternate version deployed with the app, by putting this in WEB-INF/glassfish-web.xml for early versions of GF3:

<class-loader delegate="false" />
<property name="useBundledJsf" value="true" />

Even though I'm using GF4, I thought I'd give it a try, and added a glassfish-web.xml with this:

glassfish-web.xml:

<glassfish-web-app error-url="">
  <class-loader delegate="false" />
  <property name="useBundledJsf" value="true" />
</glassfish-web-app>

The result is a page that has shows the message with an empty version string:

The Mojarra version is

The GF log also indicates no errors. The .war file that is created includes WEB-INF/lib/javax.faces-2.2.7.jar, so I suspect the problem has something to do with the GF configuration, rather than the Netbeans/Maven packaging.

I know I can download the 2.2.7 jar and put it in the glassfish/modules directory (which is how I began using 2.2.5). But the ultimate goal is to try alternate versions of JSF in an app-specific way (ideally, by just changing Maven coordinates for the JSF dependency), rather than change the GF install. Is there anyway to do this through configuration of the app and without altering the GF install?


Update 9/14/2015: I built a new Netbeans project:
  • GlassFish 4.1 (out of the box), which has Mojarra 2.2.7, by default.
  • NB Maven/Web Application project, with JSF 2.2 Framework.
  • Modified the index.xhtml file as shown above.
  • Added a Info.java class, containing the contents, above (javax.enterprise.context.RequestScoped, javax.inject.Named).
  • Added a WEB-INF/glassfish-web.xml and added the lines shown above.
  • Added the dependency to the pom.xml file as shown above, but using javax.faces version 2.2.12.

Essentially, I'm getting the same result. If the useBundledJsf property setting is not in the glassfish-web.xml file, the version displays as 2.2.7 (even though the war file has 2.2.12 in it). And if the useBundledJsf property setting is there, along with the change to the class-loader to "false", the version is blank.

I also installed Mojarra 2.2.10 by changing the javax.faces file in the glassfish install to use 2.2.10. When I do that, I get the same results -- either the version is empty, or it shows 2.2.10 (2.2.12 is still configured, and in the war in both cases).

like image 907
CraftWeaver Avatar asked Jul 24 '14 13:07

CraftWeaver


1 Answers

An alternative solution can be to try Payara 4.1.1.154 as it comes bundled with Mojarra 2.2.12

I think this can be a good solution for your needs.

This section details the modules that have been updated since the last release (4.1.153).

Mojarra 2.2.12
Webservices 2.3.2-b608
JAXB 2.2.12-b141219.1637
JAXB-API 2.2.13-b141020.1521
Weld 2.2.16.Final
Tyrus 1.11
JBatch 1.0.1-b09
Grizzly 2.3.23
HK2 2.4.0-b32
Jersey 2.22
Hazelcast 3.5.2
like image 184
Jason Bourne Avatar answered Oct 20 '22 00:10

Jason Bourne