Java Packaging Tool jar Package Details

  • 2021-12-04 10:13:26
  • OfStack

The Java packaging tool is one of the most commonly used Java development tools, and the code repackaging tool jarjar can help you package and embed other java libraries into your own project jar package. The reasons for this are:

When you publish a project, package the libraries used into the existing project jar package, so that the published jar package is no better than the jar package that depends on other projects;

When the java library you use is upgraded, the newly released jar package may not match your existing project. In order to maintain the code stability of the project, you can package all the dependent jar packages used when writing the code into the current project jar package to avoid this problem.

jarjar can be used either through the Ant task or separately from the command line. When packaging code, if you want to rename some dependent packages, jarjar will call bytecode conversion (via ASM) to update the code and do other work automatically.

Using jar as an Ant task

Our existing Ant tasks can be packaged with jar tasks, such as:


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

In order to use the jarjar tool, we create a task called jarjar. Since JarJarTask is a subclass of Ant standard task Jar, if you don't need to use the unique functions of jarjar, you can call jarjar tool like this:

Just like the standard "jar" task 1, you can include other jar packages through the "zipfileset" element. But just including other jar packages does not keep you away from the "jar package trap", because the class name in the jar package you rely on remains unchanged and may still conflict with the class name in other versions of jar packages.

To rename class names, JarJarTask introduces a new element "rule". "rule" contains the "pattern" attribute, which allows you to use wildcards to select which classes need to be renamed, and the "result" attribute allows you to set how to rename selected classes.

In this example, we want to introduce a library called jaxen. jar. And rename all classes that begin with "org. jaxen" to begin with "org. example. jaxen":


<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 wildcard * * means to match all sub-packages of the loop. If you only want to match one sub-package, you can use *.

@ 1 for what the first ** matches, 1 analogy, @ 2 for what the second * or ** matches from left to right. @ 0 is a special flag that represents the full name of the entire matched class.

Use jar alone from the command line

java -jar jarjar.jar [help]

Print help information.

java -jar jarjar.jar strings

Print the string information under the class path classpath. If there is debug information in the class, the line number of the line will be printed.

For example, java-jar jarjar. jar strings servlet-api. jar will print:

...
javax.servlet.http.HttpServletRequest
"BASIC"
"FORM"
"CLIENT_CERT"
"DIGEST"
javax.servlet.http.HttpUtils
"javax.servlet.http.LocalStrings"
88: "javax.servlet.http.LocalStrings"
339: "://"
341: "http"
341: "https"
145: " & amp;"
238: "err.io.short_read"
254: "8859_1"
...

java -jar jarjar.jar find []

Print out the dependency of java class on the class under the class path. If omitted, use it instead. You can only take class or jar. The former represents printing dependencies between classes, while the latter prints dependencies between packages.

java -jar jarjar.jar process

Will be converted to the file according to the method specified in the file, and the original classes in the file will be deleted.

The writing of the document will be mentioned below.

Format of ClassPath Classpath

The ClassPath classpath is a set of directories, jar packages or zip packages, separated by commas or semicolons (specifically, which separator depends on the operating system). See java doc for detailed description of classpath. You can also use wildcard characters to write classpath: http://bugs. sun. com/bugdatabase/view_bug. do? bug_id=6268383.

Rules Rule File Format

Rules rule file is actually a kind of text file, and every line represents one rule Rule. The spaces at the beginning and end of the line will be ignored. There are three different styles of Rule writing:


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

The first one is used to set how jar renames class files. All classes, as long as it refers to a class whose name needs to be changed, its related contents will be automatically synchronized to ensure that there will be no reference errors. If a class matches a different rule, only the first matching rule will take effect. The settings of and are the same as those in Ant mentioned above.

Matching classes in the zap rule will not be added to the generated new jar package.


Related articles: