在Jenkins插件中使用外部dll库

时间:2013-10-15 09:57:43

标签: maven tomcat jenkins jenkins-plugins jacob

要使用外部com对象,我必须将Jacob jar和dll库包含到我的Jenkins插件中。我找到了使用雅各布的jacob_excel_reader plugin,但解决方案并不能让我满意。开发人员将jar依赖项添加到pom.xml文件:

<dependency>
  <groupId>com.jacob</groupId>
  <artifactId>jacob</artifactId>
  <version>1.17-M2</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/lib/jacob.jar</systemPath>
</dependency>

并通过参数

集成dll文件
-Djava.library.path=${workspace_loc:jacob_excel_reader}\lib

这是集成dll的唯一方法吗?我尝试了另一种方式,但我遇到了问题,我肯定需要你的帮助!

首先,我将jar和dll文件的依赖项添加到pom.xml

<dependency>
  <groupId>net.sf.jacob-project</groupId>
  <artifactId>jacob</artifactId>
  <version>1.14.3</version>
</dependency>
<dependency>
  <groupId>net.sf.jacob-project</groupId>
  <artifactId>jacob</artifactId>
  <version>1.14.3</version>
  <classifier>x86</classifier>
  <type>dll</type>
</dependency>

并配置将dll文件复制到target/APPNAME/WEB-INF/lib

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <executions>
        <execution>
          <id>copy-dependencies</id>
          <phase>prepare-package</phase>
          <goals>
            <goal>copy-dependencies</goal>
          </goals>
          <configuration>
            <excludeTransitive>true</excludeTransitive>
            <includeArtifactIds>jacob</includeArtifactIds>
            <includeTypes>dll</includeTypes>
            <failOnMissingClassifierArtifact>true</failOnMissingClassifierArtifact>
            <silent>false</silent>
            <outputDirectory>target/APPNAME/WEB-INF/lib</outputDirectory>
            <overWriteReleases>true</overWriteReleases>
            <overWriteSnapshots>true</overWriteSnapshots>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

当我运行mvn package时,默认测试会发出警告:

SEVERE: Failed Inspecting plugin C:\...\AppData\Local\Temp\hudson7697228528744044800tmp\the.jpl
java.util.zip.ZipException: error in opening zip file

但它将dll和jar文件复制到target/APPNAME/WEB-INF/lib为什么它也会复制jar文件以及导致ZipException的原因?

在插件执行期间插件抛出:

FATAL: no jacob-1.14.3-x86 in java.library.path
java.lang.UnsatisfiedLinkError: no jacob-1.14.3-x86 in java.library.path

无法找到dll文件,为什么?我可以在不使用上述参数的情况下将target/APPNAME/WEB-INF/lib添加到java.library.path 吗?

1 个答案:

答案 0 :(得分:2)

一般来说,你需要做的是抓住DLL所在的路径,然后给Jacob提示找到路径的位置。

关于第一步,这可能有效:

String relativeWebPath = "/WEB-INF/lib/";
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);

或者,如果您的DLL文件打包在JAR中,则必须先将其解压缩到已知位置。

InputStream in = SomeClassInTheJAR.class.getResourceAsStream("/jacob-1.17-x86.dll");
File fileOut = new File(tempDir + "/jacob-1.17-x86.dll");
String absoluteDiskPath = tempDir;

OutputStream out = FileUtils.openOutputStream(fileOut);
IOUtils.copy(in, out);
in.close();
out.close();

关于第二步,为了让Jacob知道DLL路径,请执行以下操作:

System.setProperty(com.jacob.com.LibraryLoader.JACOB_DLL_PATH, absoluteDiskPath);

然而,它并不比命令行标志好多少。只是不同 ... 似乎没有“简单”(如开箱即用)的方式从WebApp中加载DLL,特别是在Jenkins插件中。

相关问题