• Stars
    star
    734
  • Rank 61,748 (Top 2 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Jar Jar Links is a utility that makes it easy to repackage Java libraries and embed them into your own distribution.

Jar Jar Links is a utility that makes it easy to repackage Java libraries and embed them into your own distribution. This is useful for two reasons:

You can easily ship a single jar file with no external dependencies.

You can avoid problems where your library depends on a specific version of a library, which may conflict with the dependencies of another library.

How does it work?

Jar Jar Links includes an Ant task that extends the built-in jar task. The normal zipfileset element is used to embed jar files. A new rule element is added which uses wildcards patterns to rename the embedded class files. Bytecode transformation (via ASM) is used to change references to the renamed classes, and special handling is provided for moving resource files and transforming string literals.

Using with ant

In our imaginary project, the Ant "jar" target looks like:

<target name="jar" depends="compile">
    <jar jarfile="dist/example.jar">
        <fileset dir="build/main"/>
    </jar>
</target>

To use Jar Jar Links, we define a new task named "jarjar", and substitute it wherever we used the jar task. Because the JarJarTask class extends the normal Ant Jar task, you can use jarjar without any of its additional features, if you want:

<target name="jar" depends="compile">
    <taskdef name="jarjar" classname="com.tonicsystems.jarjar.JarJarTask"
        classpath="lib/jarjar.jar"/>
    <jarjar jarfile="dist/example.jar">
        <fileset dir="build/main"/>
    </jarjar>
</target>

Just like with the "jar" task, we can include the contents of another jar file using the "zipfileset" element. But simply including another projects classes is not good enough to avoid jar hell, since the class names remain unchanged and can still conflict with other versions.

To rename the classes, JarJarTask adds a new "rule" element. The rule takes a "pattern" attribute, which uses wildcards to match against class names, and a "result" attribute, which describes how to transform the matched names.

In this example we include classes from jaxen.jar and add a rule that changes any class name starting with "org.jaxen" to start with "org.example.jaxen" instead (in our imaginary world we control the example.org domain):

<target name="jar" depends="compile">
    <taskdef name="jarjar" classname="com.tonicsystems.jarjar.JarJarTask"
        classpath="lib/jarjar.jar"/>
    <jarjar jarfile="dist/example.jar">
        <fileset dir="build/main"/>
        <zipfileset src="lib/jaxen.jar"/>
        <rule pattern="org.jaxen.**" result="org.example.@1"/>
    </jarjar>
</target>

The ** in the pattern means to match against any valid package substring. To match against a single package component (by excluding dots (.) from the match), a single * may be used instead.

The @1 in the result is a reference to the ** portion of the rule. For every * or ** in the rule, a numbered reference is available for use in the result. References are numbered from left to right, starting with @1, then @2, and so on.

The special @0 reference refers to the entire class name.

Using with gradle

	dependencies {
		// Use jarjar.repackage in place of a dependency notation.
		compile jarjar.repackage {
			from 'com.google.guava:guava:18.0'

			classDelete "com.google.common.base.**"

			classRename "com.google.**" "org.private.google.@1"
		}
	}

See (jarjar-gradle/example/build.gradle) for some complete examples.

Using from the command line

From the command-line

java -jar jarjar.jar [help]

Prints this help message.

java -jar jarjar.jar strings <cp>

Dumps all string literals in classpath <cp>. Line numbers will be included if the classes have debug information.

java -jar jarjar.jar find <level> <cp1> [<cp2>]

Prints dependencies on classpath <cp2> in classpath <cp1>. If <cp2> is omitted, <cp1> is used for both arguments.

The level argument must be class or jar. The former prints dependencies between individual classes, while the latter only prints jar->jar dependencies. A "jar" in this context is actually any classpath component, which can be a jar file, a zip file, or a parent directory (see below).

java -jar jarjar.jar process <rulesFile> <inJar> <outJar>

Transform the <inJar> jar file, writing a new jar file to <outJar>. Any existing file named by <outJar> will be deleted.

The transformation is defined by a set of rules in the file specified by the rules argument (see below). Classpath format

The classpath argument is a colon or semi-colon delimited set (depending on platform) of directories, jar files, or zip files. See the following page for more details: http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/classpath.html

Mustang-style wildcards are also supported: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6268383 Rules file format

The rules file is a text file, one rule per line. Leading and trailing whitespace is ignored. There are three types of rules:

rule <pattern> <result>
zap <pattern>
keep <pattern>

The standard rule (rule) is used to rename classes. All references to the renamed classes will also be updated. If a class name is matched by more than one rule, only the first one will apply.

<pattern> is a class name with optional wildcards. ** will match against any valid class name substring. To match a single package component (by excluding . from the match), a single * may be used instead.

<result> is a class name which can optionally reference the substrings matched by the wildcards. A numbered reference is available for every * or ** in the <pattern>, starting from left to right: @1, @2, etc. A special @0 reference contains the entire matched class name.

The zap rule causes any matched class to be removed from the resulting jar file. All zap rules are processed before renaming rules.

The keep rule marks all matched classes as "roots". If any keep rules are defined all classes which are not reachable from the roots via dependency analysis are discarded when writing the output jar. This is the last step in the process, after renaming and zapping.

More Repositories

1

jcpp

The C Preprocessor as a Java library
Java
107
star
2

lzo-java

Pure Java implementation of the liblzo2 LZO compression algorithm
C
66
star
3

qemu-java

Java client for QEmu and KVM: Execution, management and QApi
Java
60
star
4

parallelgzip

ParallelGZIPOutputStream and ParallelGZIPInputStream
Java
58
star
5

libspf2

Implementation of the Sender Policy Framework for SMTP authorization
C
37
star
6

ipmi4j

A pure Java IPMI and RMCP implementation.
Java
36
star
7

dhcp4j

A Java DHCP server and protocol handler
Java
28
star
8

tftp4j

A high-performance pure Java TFTP server.
Java
26
star
9

libsrs2

Implementation of the Sender Rewriting Scheme for SMTP forwarding
C
16
star
10

graphviz4j

GraphViz graph builder and parser.
Java
15
star
11

polyglot

The Polyglot LR parser generator
Java
13
star
12

spring-rich-client

A fork of the official Spring Rich Client project, ported to Spring 3.x
Java
11
star
13

jrat

The Java Runtime Analysis Toolkit
Java
11
star
14

gradle-velocity-plugin

A gradle plugin for the Velocity preprocessor.
Java
10
star
15

gradle-jnaerator-plugin

Java
8
star
16

jdiagnostics

Support bundle builder and classpath debugger for Java applications
Java
7
star
17

gradle-license3j-plugin

Gradle plugin for license3j license management
Java
6
star
18

lisp

A trivial LISP machine in Java.
Java
6
star
19

lynxfs

LynxOS FUSE filesystem driver for Linux
Shell
6
star
20

jccfe

Java C Compiler Front End - C, C++ and ObjC parsers in pure Java
HTML
6
star
21

linguistics

Linguistics for Java: Multilingual pluralization, declension, conjugation.
Java
5
star
22

simple-xml

A mirror of Niall Gallagher's simple-xml. See http://simple.sourceforge.net/
Java
5
star
23

gradle-sablecc-plugin

A Gradle plugin for the SableCC parser generator.
Java
2
star
24

vfsjfilechooser

An enhanced JFileChooser for virtual file systems.
Java
2
star
25

weaklistener

Java EventListener using WeakReference
Java
2
star
26

simple-xml-serializers

Additional utilities and serializers for SimpleXML - http://simple.sourceforge.net/
Java
2
star
27

gradle-stdproject-plugin

A standard gradle project convention for Java projects.
Java
2
star
28

udev4j

Java bindings to libudev
Java
2
star
29

imagebuilder

Gradle-based virtual machine image builder
Java
1
star
30

typeserializer

Serialization plugins for Java generic types for various serializers.
Java
1
star
31

jallocator

A high performance parallel resource allocator for arbitrary resources.
Java
1
star
32

sheepdog-testsuite

Test suite for the sheepdog distributed storage daemon
Java
1
star
33

dblx

Dr Brainlove LED lighting control
Java
1
star
34

gradle-jcpp-plugin

A gradle plugin for the JCPP C Preprocessor
Java
1
star