java - 是否可以使用 org.codehaus.mojo :build-helper-maven-plugin to bundle native dlls into a war file?

标签 java maven package bundle native

我有一组用maven构建的java项目。

现在,其中一个依赖于 native DLL,而 native DLL 又依赖于其他几个,所有这些都是我构建的。构建的最终产品是一个 war 文件。我需要将这些 dll 放在该 war 文件中。

我知道一种方法是将这些 dll 作为各自 java 项目的资源包含在内。这样做意味着我必须在构建该项目之前将这些 dll 复制到相应 java 项目的资源目录中。

但我正在尝试探索另一种方法,不确定是否只有我做错了,或者根本不可能。我正在谈论 org.codehaus.mojo:build-helper-maven-pluginattach-artifact 目标。

这是我尝试的方法:

根级别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>com.shunra</groupId>
  <name>VCat-build</name>
  <version>1.0.0.SNAPSHOT</version>
  <artifactId>VCat-build</artifactId>
  <packaging>pom</packaging>
  <modules>
    <module>../../../../DriverProxy</module>
    <module>../../../VCat</module>
    <!-- More modules follow -->
  </modules>
  <build>
    <plugins>
    </plugins>
  </build>
</project>

native DLL 的 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.shunra.localDriverProxy</groupId>
  <version>0.0.1</version>
  <artifactId>local-driver-proxy</artifactId>
  <packaging>pom</packaging>
  <name>Local Driver Proxy</name>
  <properties>
    <msbuild.exe>C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe</msbuild.exe>
    <msbuild.configuration>StratusRelease</msbuild.configuration>
  </properties>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.2.1</version>
        <configuration>
          <executable>${msbuild.exe}</executable>
          <workingDirectory>${basedir}</workingDirectory>
        </configuration>
        <executions>
          <execution>
            <id>clean-with-msbuild</id>
            <phase>clean</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <arguments>
                <argument>/t:Clean</argument>
                <argument>/p:Configuration=${msbuild.configuration}</argument>
                <argument>LocalDriverProxy.sln</argument>
              </arguments>
            </configuration>
          </execution>
          <execution>
            <id>build-with-msbuild</id>
            <phase>compile</phase>
            <goals>
              <goal>exec</goal>
            </goals>
            <configuration>
              <arguments>
                <argument>/t:Build</argument>
                <argument>/p:Configuration=${msbuild.configuration}</argument>
                <argument>LocalDriverProxy.sln</argument>
              </arguments>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.7</version>
        <executions>
          <execution>
            <id>attach-local-driver-proxy</id>
            <phase>package</phase>
            <goals>
              <goal>attach-artifact</goal>
            </goals>
            <configuration>
              <artifacts>
                <artifact>
                  <file>${basedir}/../Distribution/DataStruct.dll</file>
                  <classifier>DataStruct</classifier>
                  <type>dll</type>
                </artifact>
                <artifact>
                  <file>${basedir}/../Distribution/GraphSetup.dll</file>
                  <classifier>GraphSetup</classifier>
                  <type>dll</type>
                </artifact>
                <!-- more artifacts follow -->
              </artifacts>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Java项目的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>com.shunra</groupId>
  <artifactId>vcat</artifactId>
  <version>0.0.1</version>
  <packaging>war</packaging>

  <properties>
    <java-version>1.6</java-version>
    <org.springframework-version>3.1.0.RELEASE</org.springframework-version>
    <org.aspectj-version>1.6.9</org.aspectj-version>
    <org.slf4j-version>1.5.10</org.slf4j-version>
  </properties>
  <dependencies>
    <!-- Quite a few dependencies follow -->
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source/>
          <target/>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.0.1</version>
        <configuration>
          <webappDirectory>target/exploded</webappDirectory>
          <archive>
            <!-- <manifest>
               <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
               <addClasspath>true</addClasspath>
           </manifest> -->
          </archive>
        </configuration>

        <executions>
          <execution>
            <id>prepare-war</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>exploded</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

运行mvn package成功,创建了war文件,但它不包含任何 native DLL。这些 DLL 构建得很好,但它们永远不会被复制到任何地方。

我运行了mvn -X。以下是我认为相关的几行:

Apache Maven 3.0.4 (r1232337; 2012-01-17 03:44:56-0500)
Maven home: o:\java\apache-maven-3.0.4
Java version: 1.6.0_27, vendor: Sun Microsystems Inc.
Java home: c:\Program Files\Java\jdk1.6.0_27\jre
.
.
.
[DEBUG] -----------------------------------------------------------------------
[DEBUG] Goal:          org.codehaus.mojo:build-helper-maven-plugin:1.7:attach-artifact (attach-local-driver-proxy)
[DEBUG] Style:         Regular
[DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <artifacts>
    <artifact>
      <file>C:\dev\shunra\DriverProxy/../Distribution/DataStruct.dll</file>
      <classifier>DataStruct</classifier>
      <type>dll</type>
    </artifact>
    <artifact>
      <file>C:\dev\shunra\DriverProxy/../Distribution/GraphSetup.dll</file>
      <classifier>GraphSetup</classifier>
      <type>dll</type>
    </artifact>
    .
    .
    .
  </artifacts>
  <basedir>${basedir}</basedir>
  <mavenSession>${session}</mavenSession>
  <project>${project}</project>
  <runOnlyAtExecutionRoot default-value="false">${buildhelper.runOnlyAtExecutionRoot}</runOnlyAtExecutionRoot>
  <skipAttach default-value="false">${buildhelper.skipAttach}</skipAttach>
</configuration>
[DEBUG] =======================================================================
[DEBUG] com.shunra.localDriverProxy:local-driver-proxy:pom:0.0.1
.
.
.
[INFO] --- build-helper-maven-plugin:1.7:attach-artifact (attach-local-driver-proxy) @ local-driver-proxy ---
[DEBUG] org.codehaus.mojo:build-helper-maven-plugin:jar:1.7:
.
.
.
[DEBUG]    org.codehaus.plexus:plexus-utils:jar:1.5.8:compile
[DEBUG] Created new class realm plugin>org.codehaus.mojo:build-helper-maven-plugin:1.7
[DEBUG] Importing foreign packages into class realm plugin>org.codehaus.mojo:build-helper-maven-plugin:1.7
[DEBUG]   Imported:  < maven.api
[DEBUG] Populating class realm plugin>org.codehaus.mojo:build-helper-maven-plugin:1.7
[DEBUG]   Included: org.codehaus.mojo:build-helper-maven-plugin:jar:1.7
.
.
.
[DEBUG]   Excluded: org.apache.maven:maven-artifact:jar:2.0.6
[DEBUG] Configuring mojo org.codehaus.mojo:build-helper-maven-plugin:1.7:attach-artifact from plugin realm ClassRealm[plugin>org.codehaus.mojo:build-helper-maven-plugin:1.7, parent: sun.misc.Launcher$AppClassLoader@3326b249]
[DEBUG] Configuring mojo 'org.codehaus.mojo:build-helper-maven-plugin:1.7:attach-artifact' with basic configurator -->
[DEBUG]   (s) file = C:\dev\shunra\DriverProxy\..\Distribution\DataStruct.dll
[DEBUG]   (s) classifier = DataStruct
[DEBUG]   (s) type = dll
[DEBUG]   (s) file = C:\dev\shunra\DriverProxy\..\Distribution\GraphSetup.dll
[DEBUG]   (s) classifier = GraphSetup
[DEBUG]   (s) type = dll
.
.
.
[DEBUG]   (f) artifacts = [org.codehaus.mojo.buildhelper.Artifact@7e79b177, ...]
[DEBUG]   (f) basedir = C:\dev\shunra\DriverProxy
[DEBUG]   (f) mavenSession = org.apache.maven.execution.MavenSession@37567e6c
[DEBUG]   (f) project = MavenProject: com.shunra.localDriverProxy:local-driver-proxy:0.0.1 @ C:\dev\shunra\DriverProxy\pom.xml
[DEBUG]   (f) runOnlyAtExecutionRoot = false
[DEBUG]   (f) skipAttach = false
[DEBUG] -- end configuration --

我对maven不太熟悉,所以我可能省略了日志中的重要部分。完整的日志在这里 - http://pastebin.com/raw.php?i=fhZCmGxB (我删除了其他几个java项目的构建日志,它们看起来都一样,但占用了大量空间)

所以我的问题是 - 我可以使用 org.codehaus.mojo:build-helper-maven-plugin 来完成我需要的操作 - 将 native DLL bundle 到 war 中吗?

编辑

我已经尝试了SpaceTrucker建议的方法,没有产生任何错误,但由此产生的 war 仍然是一样的——没有打包DLL。 maven 日志包含以下条目:

[DEBUG] =======================================================================
[DEBUG] com.shunra:vcat:war:0.0.1
.
.
.
[DEBUG]    com.shunra.localDriverProxy:local-driver-proxy:dll:DataStruct:0.0.1:compile
.
.
.
[INFO] Exploding webapp...
[INFO] Copy webapp webResources to C:\dev\shunra\Application\VCat\target\exploded
[INFO] Assembling webapp vcat in C:\dev\shunra\Application\VCat\target\exploded
[DEBUG] Processing: spring-context-3.1.0.RELEASE.jar
.
.
.
[DEBUG] Processing: local-driver-proxy-0.0.1.dll
[DEBUG] Skipping artifact of type dll for WEB-INF/lib
[INFO] 
[INFO] --- maven-war-plugin:2.0.1:war (default-war) @ vcat ---
[DEBUG] Configuring mojo org.apache.maven.plugins:maven-war-plugin:2.0.1:war from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-war-plugin:2.0.1, parent: sun.misc.Launcher$AppClassLoader@3326b249]
.
.
.
[DEBUG] -- end configuration --
[INFO] Exploding webapp...
[INFO] Copy webapp webResources to C:\dev\shunra\Application\VCat\target\exploded
[INFO] Assembling webapp vcat in C:\dev\shunra\Application\VCat\target\exploded
[DEBUG] Processing: spring-context-3.1.0.RELEASE.jar
.
.
.
[DEBUG] Processing: local-driver-proxy-0.0.1.dll
[DEBUG] Skipping artifact of type dll for WEB-INF/lib
[INFO] Generating war C:\dev\shunra\Application\VCat\target\vcat-0.0.1.war

日志提到local-driver-proxy-0.0.1.dll,但该文件不存在!

最佳答案

从我在你的pom中看到的是,没有从Java项目声明依赖关系,该项目构建了对包含 native dll的项目的 war 。声明此依赖项时应该没问题。

编辑:
将其添加到 war 文件 pom 的 dependency 部分:

<dependency>
  <groupId>com.shunra.localDriverProxy</groupId>
  <version>0.0.1</version>
  <artifactId>local-driver-proxy</artifactId>
  <classifier>DataStruct</classifier>
  <type>dll</type>
</dependency>

关于java - 是否可以使用 org.codehaus.mojo :build-helper-maven-plugin to bundle native dlls into a war file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14233787/

相关文章:

java - java中如何阻止一条语句执行,直到前一条语句完成?

java - nexus-staging-maven-plugin如何使用<scm>信息?

java - Maven-dependency-plugin 和带有 SOURCE RetentionPolicy 的注释

java - 如何将我们的包注入(inject)到java中

java - JCodeModel 和 elseif

java - JTextField 不更新 for 循环中的用户输入

java - 从另一个包访问文件

java - 如何修复 Eclipse 项目的结构?

linux - 如何查找包含某个包的存储库

symfony - Composer 需要版本,但也允许开发分支