Reduce jar size in Maven Design

Asked

Viewed 534 times

5

I’m generating a jar from a Maven project and the size is absurdly high.

What measures can I take to reduce the size of this jar?

  • 2

    Perhaps the most important thing is to check if all the libs attached to the jar are really necessary, because if there is lib that is not used, it can be removed as it is only "weighing" your jar.

  • I have added the Scope of each lib. It is an executable jar, and you add a 3 libs that I use in the project. The check form Voce refers to is Scope in the pom.xml settings ?

3 answers

3

Check if any dependencies added to your POM.XML can be removed.

Also, often a dependency depends on several other libs. If you are sure that some of them are not in use you can remove it using

Ex:

<dependency>
  <groupId>br.com.teste</groupId>
    <artifactId>artefato-teste</artifactId>
    <version>2.0</version>
    <exclusions>
      <exclusion>
        <groupId>br.com.teste</groupId>
        <artifactId>sub-artefato-teste</artifactId>
      </exclusion>
    </exclusions>
</dependency>

3

In addition to following the advice of colleagues and reviewing dependencies and transitive dependencies, there are tools that can help you.

A first way to reduce jar size is to compact using Pac200. You can do this with Maven using the plugin Maven Pack200 Plugin.

Another way is to use a plugin like Apache Maven Shade Plugin capable of creating a minified Uber Jar (i.e., which bundles only classes actually used by the application) or even something like the Proguard that in addition to removing unused classes also decreases, "overshadows" and optimizes code.

All these tools have their limitations. For example, part of the application servers do not work with pac200 compressed jars. Mini-finders and optimizers can also be dangerous, they can’t detect reflection-laden classes and usually require a certain manual setup effort (exclude classes from the minification process) when used with more complex frameworks and libraries. It is also not uncommon to have problems using an obfuscator with new versions of Java (it usually takes a while for Proguard to be updated).

1


An alternative is to create the jar without the dependencies. The jar will contain only the project classes and will be greatly reduced. In that case, you’ll need to put the jars the project needs somewhere and add those jars to the project’s classpath.

Your pom.xml will look something like this:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
                <mainClass>testes.testes.App</mainClass>
            </manifest>
            <manifestEntries>
                <Class-Path>poi-ooxml-3.14.jar poi-3.14.jar</Class-Path>
            </manifestEntries>
          </archive>
        </configuration>
    </plugin>

In this way, the jar will be created without dependencies. the stretch with the <Class-Path> will be added in the MANIFEST.MF file.

  • Excellent I’ll take a look over.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.