【问题标题】:Use external dll library in Jenkins plugin在 Jenkins 插件中使用外部 dll 库
【发布时间】:2013-10-23 01:58:49
【问题描述】:

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

<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文件,为什么?我可以在java.library.path中添加target/APPNAME/WEB-INF/lib不使用上面的参数吗?

【问题讨论】:

    标签: maven tomcat jenkins jenkins-plugins jacob


    【解决方案1】:

    一般来说,您需要做的是掌握 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 插件中加载 DLL。

    【讨论】:

    • 我使用了代码 sn-p(并将 getServletContext() 更改为 Jenkins.getInstance().servletContext)但它抛出了 FATAL: ...\target\work\webapp\WEB-INF\lib: Can't find dependent libraries java.lang.UnsatisfiedLinkError: ...\target\work\webapp\WEB-INF\lib: Can't find dependent libraries
    • 有什么解决办法吗?
    猜你喜欢
    • 2011-04-20
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多