如何仅通过Maven复制特定于操作系统的依赖项jar?

时间:2020-02-21 09:58:19

标签: maven maven-dependency-plugin

我目前正在开发一个使用nd4j和deeplearning4j依赖项的项目。这些依赖项包括适用于不同操作系统的版本,例如“ nd4j-native-1.0.0-beta6-android-arm.jar”或“ nd4j-native-1.0.0-beta6-linux-armhf.jar”等。

由于该项目只能在linux-x64上运行,所以我想知道是否有一种方法可以告诉Maven 复制不需要的JAR。我希望减少不必要的库所需的磁盘空间。

当前pom.xml看起来像这样,可以产生可部署的输出:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>MyGroup</groupId>
    <artifactId>MyApp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.eclipse.paho</groupId>
            <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.clerezza.ext</groupId>
            <artifactId>org.json.simple</artifactId>
            <version>0.4</version>
        </dependency>
        <dependency>
            <groupId>com.kohlschutter.junixsocket</groupId>
            <artifactId>junixsocket-core</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.kohlschutter.junixsocket</groupId>
            <artifactId>junixsocket-server</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.nd4j</groupId>
            <artifactId>nd4j-native-platform</artifactId>
            <version>1.0.0-beta6</version>
        </dependency>
        <dependency>
            <groupId>org.deeplearning4j</groupId>
            <artifactId>deeplearning4j-core</artifactId>
            <version>1.0.0-beta6</version>
        </dependency>
        <dependency>
            <groupId>org.deeplearning4j</groupId>
            <artifactId>deeplearning4j-parallel-wrapper</artifactId>
            <version>1.0.0-beta6</version>
        </dependency>
    </dependencies>

    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/libs</outputDirectory>
                            <useRepositoryLayout>true</useRepositoryLayout>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathLayoutType>repository</classpathLayoutType>
                            <classpathPrefix>libs/</classpathPrefix>
                            <mainClass>MyMainClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2 个答案:

答案 0 :(得分:2)

通常,如果要为dl4j或基于javacpp的任何内容捆绑特定于平台的二进制文件,可以执行以下操作: https://github.com/bytedeco/javacpp-presets/wiki/Reducing-the-Number-of-Dependencies

具体来说,您只需为所有javacpp依赖项指定-Djavacpp.platform = linux-x86_64。

答案 1 :(得分:0)

您可以在依赖项上设置<exclusions>

https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html

一个例子是:

   <dependency>
      <groupId>com.something</groupId>
      <artifactId>some-jar</artifactId>
      <version>1.0.0</version>
      <scope>compile</scope>
      <exclusions>
        <exclusion>  
          <groupId>de.dumm</groupId>
          <artifactId>nicht-benutzen</artifactId>
        </exclusion>
      </exclusions> 
    </dependency>