Introduction

The Basics

The term Java means many things to many people: a class library, a bytecode interpreter, a JIT compiler, a language specification, etc. For the vast majority of users and developers, Java is a programming language and runtime environment that is architecture- and OS-agnostic. The normal flow of code is .java (source file) .class (Java bytecode) .jar (a zip archive). In the majority of cases, a user executes a Java program by specifying a class name containing a main method (just like C and C++). Often, this is done by invoking the java binary with a list of JAR files specifying the classpath like so:

java [-cp <jar1:jar2:jar3>] <main-class> [<args>]

Java Packaging

The JPackage Project has defined standard file system locations and conventions for use in Java packages. Many distributions have inherited these conventions and in the vast majority of cases, Fedora follows them verbatim. We include relevant sections of the JPackage guidelines here but caution that the canonical document will always reside upstream: JPackage Guidelines . Over time, we would like to remove any divergences in these documents, but where they are different, these Fedora guidelines will take precedence for Fedora packages.

Release tags

JAR file installation

The following applies to all JAR files except JNI-using JAR files, GCJ files and application-specific JAR files (ie. JAR files that can only reasonably be used as part of an application and therefore constitute application-private data).

Split JAR files

If a project offers the choice of packaging it as a single monolithic jar or several ones, the split packaging SHOULD be preferred.

Filenames

If the package provides a single JAR and the filename provided by the build is %{name}.jar or %{name}-%{version}.jar then filename %{name}.jarMUST be used.

If the package provides a single JAR and the filename provided by the build is neither %{name}-%{version}.jar nor %{name}.jar then this file MUST be installed as %{name}.jar and a symbolic link with the usual name MUST be provided.

If the package provides more than one JAR file, the filenames assigned by the build MUST be used (without versions).

If the project usually provides alternative JAR file names by installing symbolic links then such symlinks MAY be installed in the same directory as the JAR files.

NoteHere %{name} refers either to package name, or name of subpackage where the jar is installed.

Installation directory

All architecture-independent JAR files MUST go into %{_javadir} or a Java-version specific directory %{_javadir}-* as appropriate[1]. Packages CAN place JAR files into subdirectories.

Compatibility packages

In certain cases it might be necessary to create compatibility packages that provide older API/ABI level of the same library. However creating these compatibility packages is strongly discouraged. To standardize and simplify packaging of such compatibility packages following rules apply:

Compatibility packages are named in the same way as original except addition of version to package name,

Any JAR or POM files MUST be versioned.

Ant and Maven compatibilitybuild-classpath and related tools will resolve versioned jar files if versioned jar is asked for. Maven will use dependency information from main package and will return versioned jar if it matches the version asked for in the pom file.

Javadoc installation

Java API documentation uses a system known as Javadoc. All javadocs MUST be created and installed into a directory of %{_javadocdir}/%{name}.

Directory or symlink %{_javadocdir}/%{name}-%{version}SHOULD NOT exist.

The javadoc subpackage MUST be declared noarch even if main package is architecture specific.

Mapping between POM and JAR files

Maven identifies JAR files by a set of strings: groupId, artifactId and version (mostly). The macro %mvn_install reads the groupId and artifactId from the POM file and creates a file in %{_mavendepmapfragdir} that maps groupId:artifactId pairs to JAR files under %{_javadir}.

The macro %mvn_alias can be used to add additional mappings for given POM/JAR file. For example, if the pom file indicates that it contains groupId commons-lang, artifactId commons-lang, this macro ensures that we also add a mapping between groupId org.apache.commons and the installed JAR/POM files. This is necessary in cases where the groupId or artifactId may have changed, and other packages might require different IDs than those reflected in the installed POM.

NoteManually copying JAR/POM files and calling %add_maven_depmap macro in a spec file is discouraged. However, the packager can still use these older techniques, if the techniques described in this document cannot be applied from some reason.

Alternative JAR file names

In some cases, it may be important to be able to provide symlinks to actual JAR files. This can be achieved with %mvn_file macro. This macro allows packager to specify names of the JAR files, their location in %{_javadir} directory and also can create symlinks to the JAR files. These symlinks can be possibly located outside of the %{_javadir} directory.

Example of usage of %mvn_file macro:

%prep
...
%mvn_file :guice google/guice guice

This means that JAR file for artifact with ID "guice" (and any groupId) will be installed in %{_javadir}/google/guice.jar and there also will be a symlink to this JAR file located in %{_javadir}/guice.jar. Note the macro will add ".jar" extensions automatically.

Single artifact per package

If the project consists of multiple artifacts, it is recommended to install each artifact to the separate subpackage. The macro %mvn_build -s will generate separate .mfiles file for every artifact in the project. This file contains list of files related to specific artifact (typically JAR file, POM file and depmap). It can be later used in %files section of the spec file.

In above example, the artifacts plexus-compiler-jikes, plexus-compiler-eclipse, plexus-compiler-csharp will end up in package named plexus-compiler-extras. If there are some other artifacts beside these three mentioned (e.g. some parent POMs), then these will all end up in package named %{name}.

Patching Maven pom.xml files

Sometimes Maven pom.xml files need to be patched before they are used to build packages. One could use traditional patches to maintain changes, but package maintainers SHOULD use %pom_* macros developed specially to ease this task.

These macros are designed to be called from %prep section of spec files. There are documented in /etc/rpm/macros.fjava configuration file, which is also available online. See the documentation for technical details how to use these macros. Below are some examples added for convenience.

Often dependencies specified in Maven pom.xml files need to be removed because of different reasons. %pom_remove_dep macro can be used to ease this task:

The above macros cover the most common cases of modifying pom.xml files, however if there is a need to apply some less-common patches there are also two generic macros for modifying pom.xml files. %pom_xpath_remove can be used to remove arbitrary XML nodes, described by XPath expressions. %pom_xpath_inject macro is capable of injecting arbitrary XML code to any pom.xml file. Below you can find some examples for these macros.

NotePOM files use a specific namespace - http://maven.apache.org/POM/4.0.0. The easiest way to respect this namespace in XPath expressions is prefixing all node names with pom:. For example, pom:environment/pom:os will work because it selects nodes from pom namespace, but environment/os won't find anything because it looks for nodes that don't belong to any XML namespace.

Using %pom_* macros not only increases readability of the spec file, but also improves maintainability of the package as there are no patches that would need to be rebased with each upstream release.

Wrapper Scripts

Applications wishing to provide a convenient method of execution SHOULD provide a wrapper script in %{_bindir}.

The jpackage-utils package contains a convenience %jpackage_script macro that can be used to create scripts that work for the majority of packages. See its definition and documentation in /etc/rpm/macros.jpackage. One thing to pay attention to is the 6th argument to it - whether to prefer a JRE over a full SDK when looking up a JVM to invoke - most packages that don't require the full Java SDK will want to set that to true to avoid unexpected results when looking up a JVM when some of the installed JRE's don't have the corresponding SDK (*-devel package) installed.

The previous example installs the "msv" script (5th argument) with main class being com.sun.msv.driver.textui.Driver (1st argument). No optional flags (2nd argument) or options (3rd argument) are used. This script will add several libraries to classpath before executing main class (4th argument, jars separated with ":"). build-classpath is run on every part of 4th argument to create full classpaths.

GCJ

Building GCJ AOT bits is discouraged unless you have a very strong reason to include them in the packages.
Even when AOT bits are built and included in packages it is recommended to not require java-1.5.0-gcj because this will force every single user to install it even if one wants to use another JVM.

Packages using APIs

Packaging JAR files that use JNI

Applicability

Java programs that wish to make calls into native libraries do so via the Java Native Interface (JNI). A Java package uses JNI if it contains a .so file. Note that this file can be embedded within JAR files themselves.

Note that GCJ packages contain .sos in %{_libdir}/gcj/%{name} but they are not JNI .sos.

Guideline

JAR files using JNI or containing JNI shared objects themselves MUST be placed in %{_jnidir} and CAN BE symlinked to %{_libdir}/%{name}.

JNI shared objects MUST be placed in %{_libdir}/%{name}

NoteIf the JNI-using code calls System.loadLibrary you'll have to patch it to use System.load, passing it the full path to the dynamic shared object.

Macro expansions%{_jnidir} usually expands into %{_prefix}/lib/java. %{_prefix}/lib64/java will cease its existence and will be decomissioned

Example

To satisfy this Fedora requirement of using "System.load()" instead of "System.loadLibrary()" while still providing 32-bit versus 64-bit usability as well as complying with Java's write-once-run-anywhere goal, most JNI jar file should contain code similar to the following (as used in the pki-symkey JNI package):

Packages utilizing approach of bundling so files as resources within JAR files themselves do not have these issues and are more self-contained.

Notes on multiarch

Our guidelines have never been completely multiarch-aware. So it was never really possible to install both i686 and x86_64 JNI-using java libraries. However guidelines complicated things by introducing usage of %{_libdir} and other directories. This version makes it clear we do not support multiarch for JNI-using packages.

Some of the complications with multiarch for JNI packages are:

build-classpath and related tools would need to be aware what will be architecture of executing JVM

build-jar-classpath would still not work for creating symlinks because it would create them on build architecture instead of runtime architecture

Previous reasons cause creating of /usr/bin wrappers impractical

Handling proper requires in RPM is impossible. For example package Z-native.i686 and JDK.x86_64 are installed. As far as RPM is concerned this would be enough to provide Z.noarch with needed "Requires: Z-native", but it would not work during runtime.

Things to avoid

Pre-built JAR files / Other bundled software

Many Java projects re-ship their dependencies in their own releases. This is unacceptable in Fedora. All packages MUST be built from source and MUST enumerate their dependencies with Requires. They MUST NOT build against or re-ship the pre-included JAR files but instead symlink out to the JAR files provided by dependencies. There may arise rare cases that an upstream project is distributing JAR files that are actually not re-distributable
by Fedora. In this situation, the JAR files themselves should not be redistributed -- even in the source zip. A modified source zip should be created with some sort of modifier in the name (ex. -CLEAN) along with instructions for reproducing. It is a good idea to have something similar to the following at the end of %prep (courtesy David Walluck):