2017-05-12 54 views
1

这可能是愚蠢的(请指导我),但我想使用Maven在我的包中包含来自依赖项的一些二进制文件。如何在构建的jar中包含构建过程中的文件?

依赖关系是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> 

回答

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>标记。

相关问题