2017-05-12 99 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>標記。

相關問題