1

这可能很愚蠢(请指导我),但我正在尝试使用 Maven 在我的包中包含一些依赖项中的二进制文件。

依赖项是 jinput,并且二进制文件在构建期间被“解包”。由于二进制文件在构建后被解包,因此它们不包含在我的 .jar 中,使用标准的“资源”方式来包含文件。当构建过程中不存在时,如何使 Maven 将二进制文件包含在我的包中?

<?xml version="1.0" encoding="UTF-8"?>
<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>com.mycompany</groupId>
    <artifactId>ProjectA</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>net.java.jinput</groupId>
            <artifactId>jinput</artifactId>
            <version>2.0.7</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <!-- Unpack the jinput binaries to "bin" -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>unpack jinput windows</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <skip>false</skip>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>net.java.jinput</groupId>
                                    <artifactId>jinput-platform</artifactId>
                                    <version>2.0.7</version>
                                    <classifier>natives-windows</classifier>
                                    <type>jar</type>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>/bin</outputDirectory>
                                    <includes>**/*.dll</includes>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>${project.builddir}</directory>
                <includes>
                    <include>bin/*.dll</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>
4

1 回答 1

1

您可以使用 maven-jar-plugin 代替资源。

我已经测试了下面的代码,它工作正常。

<properties>
    <my.dll.folder>${project.build.directory}/unpackedfiles</my.dll.folder>
</properties>

<dependencies>
    <dependency>
        <groupId>net.java.jinput</groupId>
        <artifactId>jinput</artifactId>
        <version>2.0.7</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <id>unpack</id>
                    <phase>package</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>net.java.jinput</groupId>
                                <artifactId>jinput-platform</artifactId>
                                <version>2.0.7</version>
                                <classifier>natives-windows</classifier>
                                <type>jar</type>
                                <overWrite>false</overWrite>
                                <outputDirectory>${my.dll.folder}/bin</outputDirectory>
                                <destFileName>optional-new-name.jar</destFileName>
                                <includes>**/*.dll</includes>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                        <classesDirectory>${my.dll.folder}</classesDirectory>
                        <classifier>sample</classifier>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

用上面的代码修改你的 pom 文件后,你可以执行下面的命令

mvn clean package

然后您可以验证生成的 jar 文件是否包含 bin 文件夹中所需的 *.dll 文件。

<classifier>如果要覆盖相同的输出 jar 文件,可以删除该 标记。

于 2017-05-12T13:05:11.910 回答