Spring Boot-引发异常jar不存在

时间:2018-08-17 17:37:42

标签: maven spring-boot jar apache-commons-vfs

我正在尝试通过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时,它可以正常工作,但是当我打包并运行它时,我遇到了异常。

在pom.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>

2 个答案:

答案 0 :(得分:2)

可执行jar中的大多数嵌套库无需解压即可运行。但是,某些库可能会有问题。有关更多详细信息,请参阅此春季启动文档https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-extract-specific-libraries-when-an-executable-jar-runs

OR

使用maven-dependency-plugin和maven-jar-plugin。 尽管需要在.jar的相同位置复制“ target / 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>
                        <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>

答案 1 :(得分:0)

Commons-VFS 不知道如何处理 Spring Boot 的嵌套 Jar 路径。您必须通过扩展其 ResourceFileProvider 来教授它。实际上,Spring Boot 的类加载器以 jar:file:foo.jar!bar.jar!baz.jar(一个 jar: 前缀)的形式返回资源 URL,而 Commons-VFS 需要多个“jar:”前缀,例如:jar:jar:file:foo.jar!bar.jar!baz.jar(每个“jar:”爆炸字符“!”)。

查看 LGPL 许可的 Portofino(https://github.com/ManyDesigns/Portofino/blob/02550f5789c307ff668b4a778328c027ec2d7fcc/microservices/spring-boot/src/main/java/com/manydesigns/portofino/microservices/boot/PortofinoBootApplication.java#L56-L59https://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)中的示例修复。