Skip to content

Reducing the Number of Dependencies

Samuel Audet edited this page Jun 6, 2019 · 7 revisions

Introduction

Native binaries are inherently large in size, and we need to use a different set of libraries for each supported platform. However, if your application does not depend on all these libraries or does not need to support all these platforms, there are a few ways to reduce easily the number of dependencies, apart from manually excluding artifacts one by one or removing files from uber JARs.

Include binaries only for one or more platforms

To limit dependencies to a single platform, set the javacpp.platform system property of Maven to the desired platform, for example:

mvn -Djavacpp.platform=linux-x86_64 ...

To include binaries for 2 or more platforms, we can rely on a different set of profiles, but always starting by setting the javacpp.platform.none system property to reset the default one from javacpp.platform. For example, in the case of Linux and Windows, we can use commands starting with:

mvn -Djavacpp.platform.none -Djavacpp.platform.linux-x86_64 -Djavacpp.platform.windows-x86_64 ...

Note: This works only with Maven. It does not work with Gradle, sbt, or any other build system.

Excluding all binaries via the platform artifact

On the other hand, to remove all dependencies related to a given library for all platforms, the mechanisms already in place for the pom.xml file are most efficient. For example, if the application uses JavaCV but without anything related to OpenCV, we can add a dependency such as the following to avoid all of its large artifacts:

    <dependency>
        <groupId>org.bytedeco</groupId>
        <artifactId>javacv-platform</artifactId>
        <version>1.5</version>
        <exclusions>
            <exclusion>
                <groupId>org.bytedeco</groupId>
                <artifactId>opencv-platform</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

Note: This works the same with similar features in Gradle, sbt, and other build systems.


For additional assistance, please feel free to post your questions on the mailing list or open new issues on GitHub.