4

My project includes the nd4j-native-platform dependency, which includes .jars for windows, linux, and mac. The app is developed on windows/mac machines then deployed to Linux, so I'd like to save space on deployment by excluding these other platform jars that take up > 400 MB when the .war is built. Tl;dr, I want to exclude all the .jars that don't end with linux-x86_64.

Dependency in pom:

<dependency>
    <groupId>org.nd4j</groupId>
    <artifactId>nd4j-native-platform</artifactId>
    <version>1.0.0-beta7</version>
    <classifier>linux-x86_64</classifier>
</dependency>

I don't see any way to exclude them by classifier in the dependency tag, it seems you can only exclude by groupId and artifactId. I also tried using packagingExcludes and warSourceExcludes in the .war plugin, but that didn't do anything:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.2.3</version>
    <configuration>
        <packagingExcludes>
            WEB-INF/lib/nd4j-native-1.0.0-beta7-windows-x86_64.jar,
            WEB-INF/lib/nd4j-native-1.0.0-beta7-android-x86.jar,
            WEB-INF/lib/nd4j-native-1.0.0-beta7-macosx-x86_64.jar,
            WEB-INF/lib/nd4j-native-1.0.0-beta7-android-x86_64.jar,
            WEB-INF/lib/nd4j-native-1.0.0-beta7-linux-ppc64le.jar,
            WEB-INF/lib/nd4j-native-1.0.0-beta7-android-arm64.jar,
            WEB-INF/lib/nd4j-native-1.0.0-beta7-android-arm.jar,
            WEB-INF/lib/openblas-0.3.9-1-1.5.3-windows-x86_64.jar,
            WEB-INF/lib/nd4j-native-1.0.0-beta7-linux-armhf.jar,
            WEB-INF/lib/openblas-0.3.9-1-1.5.3-windows-x86.jar,
            WEB-INF/lib/openblas-0.3.9-1-1.5.3-windows-x86.jar,
            WEB-INF/lib/openblas-0.3.9-1-1.5.3-linux-armhf.jar,
            WEB-INF/lib/openblas-0.3.9-1-1.5.3-linux-ppc64le.jar,
            WEB-INF/lib/openblas-0.3.9-1-1.5.3-linux-arm64.jar
        </packagingExcludes>
    </configuration>
</plugin>

Using Maven 3.6.3.

allenfromspace
  • 120
  • 1
  • 13

1 Answers1

5

The correct way to deal with this for most things that use JavaCPP is to set the javacpp.platform property.

When building with mvn -Djavacpp.platform=linux-x86_64 you will get only that specific platform and nothing else. This will also apply to all other transitive dependencies, e.g. opencv.

You can try running mvn -Djavacpp.platform=linux-x86_64 dependency:tree to see that it works.

Paul Dubs
  • 798
  • 4
  • 8
  • This unfortunately doesn't work. When I add the nd4j-native dependency with the linux-x86_64 classifier, nd4j tries to be "helpful" and still only pulls the windows versions since I'm building on Windows. nd4j-native-platform contains all of the different platforms .jars, so I have to use that dependency instead, but I don't see how I can tell it to exclude all of its transitive dependencies (which are the nd4j-native .jars; yeah, confusing, I know) EXCEPT the linux version. – allenfromspace Dec 21 '20 at 15:13
  • Your comment reminded me that there is actually a proper way to do this that is built into DL4J and other javacpp using libraries which provide platform artifacts. I've updated my answer accordingly. – Paul Dubs Dec 21 '20 at 15:23
  • 1
    Great, that works, thanks! How can I add that argument to the pom? I thought -D arguments mapped to , but when I add linux-x86_64 there, the dependency tree goes back to all the windows versions. – allenfromspace Dec 21 '20 at 15:32
  • The way the `-D` options work is that it applies at all levels and for all dependencies, while the properties tag only applies to the current pom file and its children (pom files that declare it to be their parent). So unfortunately you can't easily set that within your pom.xml file. But take a look at https://github.com/bytedeco/javacpp-presets/wiki/Reducing-the-Number-of-Dependencies for more options – Paul Dubs Dec 21 '20 at 18:02
  • 1
    To support POM properties, we would need to create a Maven extension, essentially a custom version of Maven: https://github.com/bytedeco/javacpp-presets/issues/846 We can do that kind of thing with Gradle JavaCPP more easily though: https://github.com/bytedeco/gradle-javacpp#the-platform-plugin – Samuel Audet Dec 23 '20 at 01:20