>我正在尝试通过 mvn 包创建一个 jar,然后运行 Java -jar/target/test.jar
Caused by: org.apache.commons.vfs2.FileSystemException: Could not replicate "file:///C:/Users/user/workspace/testProject/target/test.jar!/BOOT-INF/lib/myJar.jar" as it does not exist.
at org.apache.commons.vfs2.provider.AbstractFileSystem.replicateFile(AbstractFileSystem.java:418) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.provider.zip.ZipFileSystem.<init>(ZipFileSystem.java:61) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.provider.jar.JarFileSystem.<init>(JarFileSystem.java:50) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.provider.jar.JarFileProvider.doCreateFileSystem(JarFileProvider.java:82) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.provider.AbstractLayeredFileProvider.createFileSystem(AbstractLayeredFileProvider.java:89) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.provider.AbstractLayeredFileProvider.findFile(AbstractLayeredFileProvider.java:63) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:693) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:649) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:605) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.provider.res.ResourceFileProvider.findFile(ResourceFileProvider.java:81) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:693) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:649) ~[commons-vfs2-2.0.jar!/:2.0]
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:605) ~[commons-vfs2-2.0.jar!/:2.0]
... 52 common frames omitted
当我做mvn spring-boot:run
时,它工作正常,但是当我打包并运行它时,我得到了上述异常。
在绒球中.xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<mainClass>
com.client.test.Application
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
可执行 jar 中的大多数嵌套库不需要解压缩即可运行。但是,某些库可能会出现问题。有关更多详细信息,请参阅此 Spring 启动文档 https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-extract-specific-libraries-when-an-executable-jar-runs
或
使用 maven-dependency-plugin 和 maven-jar-plugin。 尽管需要在.jar的同一位置复制"目标/库"文件夹。就像它在目标文件夹中一样。 尝试使用这个:
<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>
<outputDirectory>
${project.build.directory}/libs
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>
<package>.<MainClassName>
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Commons-VFS不知道如何处理Spring Boot的嵌套Jar路径。你必须通过扩展它的 ResourceFileProvider 来教它。事实上,Spring Boot 的类加载器以jar:file:foo.jar!bar.jar!baz.jar
的形式返回资源 URL(一个 jar:前缀(,而 Commons-VFS 需要多个 "jar:" 前缀,如下所示:jar:jar:file:foo.jar!bar.jar!baz.jar
(每个 bang 字符 "!" 返回一个 "jar:"(。
请参阅 LGPL 许可的 Portofino (https://github.com/ManyDesigns/Portofino/blob/02550f5789c307ff668b4a778328c027ec2d7fcc/microservices/spring-boot/src/main/java/com/manydesigns/portofino/microservices/boot/PortofinoBootApplication.java#L56-L59,https://github.com/ManyDesigns/Portofino/blob/02550f5789c307ff668b4a778328c027ec2d7fcc/microservices/spring-boot/src/main/java/com/manydesigns/portofino/microservices/boot/SpringBootResourceFileProvider.java(和GPL许可的Renjin (https://github.com/bedatadriven/renjin/blob/cac412d232ad66d4ee8e37cfc8cb70a45e676e19/core/src/main/java/org/renjin/util/ClasspathFileProvider.java#L88-L126(。