Sunday, August 5, 2012

Bundled PrimeFaces Themes

I have created a sub-project in PrimeFaces Extensions for everybody who would not like to confront with adding multiply theme JAR files and only needs one bundled JAR with all PrimeFaces themes. One JAR file would also reduce the scanning time during startup (JSF, CDI, ... look for marker XML files) in comparison to 35+ separate files.

Last release of the bundled themes can be found in the Maven Central repository. The release version is synchronized with the current release of PrimeFaces themes. Add this dependency to your pom.xml and you are done.
<dependencies>
    ...    
    <dependency>
        <groupId>org.primefaces.extensions</groupId>
        <artifactId>all-themes</artifactId>
        <version>1.0.6</version>
    </dependency>
    ...
</dependencies>
Non Maven users can download the JAR file direct from the Maven Central repository.

You can consider this as an "all-in-one" themes add-on. We don't modify themes, we only collect them by means of the Maven Assembly Plugin. A simple Maven project aggregates PrimeFaces themes and builds a single JAR file with all available themes. Here is a hint how it works. The Maven project with packaging pom is configured as follows:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
            <executions>
                <execution>
                    <id>package-all</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <descriptor>src/main/assembly/all-themes.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.primefaces.themes</groupId>
        <artifactId>afterdark</artifactId>
        <version>${primefaces.theme.version}</version>
        <scope>runtime</scope>        
    </dependency>
    ...
    <dependency>
        <groupId>org.primefaces.themes</groupId>
        <artifactId>vader</artifactId>
        <version>${primefaces.theme.version}</version>
        <scope>runtime</scope>        
    </dependency>
</dependencies>

<properties>
    <primefaces.theme.version>1.0.6</primefaces.theme.version>    
</properties>
The Maven Assembly Plugin takes all dependencies and re-packs them as a single JAR. Repacking instructions are defined in the assembly descriptor all-themes.xml.
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="...">
    <id>all-themes</id>
    <formats>
        <format>jar</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <dependencySets>
        <dependencySet>
            <unpack>true</unpack>
            <useProjectArtifact>false</useProjectArtifact>
            <useTransitiveDependencies>false</useTransitiveDependencies>
        </dependencySet>
    </dependencySets>
</assembly>
That's all.

10 comments:

  1. Great post Oleg and excellent solution as always!. Thanks a lot

    ReplyDelete
  2. Nice one Oleg...what if there are new themes...?

    ReplyDelete
  3. As I said in the related forum post, we will maintain new themes. When 1.0.7 is released, I will try to release bundled themes as 1.0.7 too. No worries.

    ReplyDelete
  4. +1 is there an option to specify required themes (comma separated theme names)in all-themes.xml instead of selecting all?

    ReplyDelete
  5. No. It would mean 100+ custom builds. There are a lot of possible theme permutations. Every permutation would mean a separate release in the Maven central repo. You should either add required themes "theme by theme" (normal approach) or use "all-in-one" theme JAR with all themes.

    ReplyDelete
  6. <exclusion> wont help in this case?

    <dependency>
    <groupId>org.primefaces.extensions</groupId>
    <artifactId>all-themes</artifactId>
    <version>1.0.6</version>
    <exclusions>
    ...
    </exclusions>
    </dependency>


    Anyway, great post!

    ReplyDelete
  7. No, you can not exclude sources inside JAR with exclusions :-) You can only exclude dependencies (files) with exclusions.

    ReplyDelete
  8. yes you're right i misread the post, the themes are bundled ;)

    thanks.

    ReplyDelete
  9. Excuse me being a relative newb, but how to incorporate inside Netbeans
    currently all I have to do is the following inside web.xml

    <context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>blitzer</param-value>
    </context-param>

    ReplyDelete

Note: Only a member of this blog may post a comment.