how to build a jar with maven for a specific OS?

陌路散爱 提交于 2019-12-10 13:35:22

问题


I am using maven for Eclipse to build a jar that will run on a remote server. My system is running OS X, the server is running CestOS. For the project I need tensorflow library. Maven successfully resolves dependencies so I am able to run the project locally. However, on the server I am getting error that tensorflow library is not there because by default maven includes only macosx version. How can I force maven to substitute macosx version of tensorflow by the linux version during build?

TensorFlow java libraries for different platforms can be found here.

P.S. I already tried adding a dependency in pom with the system scope pointing to jar.


回答1:


Try this in your POM:

<dependency>
    <groupId>org.bytedeco.javacpp-presets</groupId>
    <artifactId>tensorflow</artifactId>
    <version>0.9.0-1.2</version>
</dependency>
<dependency>
    <groupId>org.bytedeco.javacpp-presets</groupId>
    <artifactId>tensorflow</artifactId>
    <version>0.9.0-1.2</version>
    <classifier>linux-x86_64</classifier>
</dependency>

Or linux-x86 instead, if your server is 32-bit.

Of course, defining a conditional dependency with profiles would be nice.




回答2:


Judging by the jar names on the page you linked, the difference between the MacOs and Linux versions lies in the text after the version part on the jar name.

That is called the classifier (see Maven coordinates) and is an optional coordinate that gives an additional differentiation after the artifact version.

As already suggested by nandsito, and to expand on its answer, try this (untested, let me know and I'll update):

<profiles>
    <profile>
        <id>osx</id>

        <dependency>
            <groupId>org.bytedeco.javacpp-presets</groupId>
            <artifactId>tensorflow</artifactId>
            <version>0.9.0-1.2</version>
            <classifier>macosx-x86_64</classifier>
        </dependency>

    </profile>

    <profile>
        <id>linux</id>

        <dependency>
            <groupId>org.bytedeco.javacpp-presets</groupId>
            <artifactId>tensorflow</artifactId>
            <version>0.9.0-1.2</version>
            <classifier>linux-x86_64</classifier>
        </dependency>

    </profile>

</profiles>

And remove all the related <dependency> nodes elsewere in your POM (so that without the <profiles> part there would be no dependency for tensorflow).

After this change you'll necessarily have to specify a profile each time (as there will be no tensorflow dependency in the POM): when preparing the package on MacOs mvn clean package -Pmacos and when preparing the package on Centos mvn clean package -Plinux

Eclipse allows you to set a list of active profiles under Project properties > Maven (you can get to this window by right-clicking on the project folder in the Project explorer.



来源:https://stackoverflow.com/questions/40535909/how-to-build-a-jar-with-maven-for-a-specific-os

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!