要使用外部 com 对象,我必须将 Jacob jar 和 dll 库包含到我的 Jenkins 插件中。我找到了 jacob_excel_reader plugin它使用 Jacob,但解决方案并不令我满意。开发人员将 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
不使用上面的参数?
最佳答案
一般来说,您需要做的是掌握 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。
关于maven - 在 Jenkins 插件中使用外部 dll 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19378259/