如何在构建的jar中包含构建(在构建期间)的文件?

时间:2017-05-12 10:51:28

标签: maven jinput

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

依赖项是jinput,二进制文件是"解压缩"在建设期间。由于二进制文件在构建之后被解压缩,因此它们不会包含在我的.jar中,而是使用包含文件的标准资源" -way。如果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>

1 个答案:

答案 0 :(得分: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文件。

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