首页 > 解决方案 > 在可执行 jar 中打包 spring 应用程序导致“无法读取架构文档”

问题描述

我已经使用以下 pom.xml/maven 配置将 spring 应用程序打包到可执行 jar 中

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>
                            com.some.Main
                        </mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </execution>
    </executions>
</plugin>

现在,当我执行这个生成的 jar 时,它会给出以下警告

Failed to read schema document 'http://www.springframework.org/schema/beans/spring-beans.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

并且执行失败并出现异常

Caused by: org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 70; cvc-elt.1.a: Cannot find the declaration of element 'beans:beans'.

同样的应用程序在 Eclipse 中没有 jar 打包运行时也能很好地执行。我确定这不是依赖问题,我提供了适当的依赖。由于包装成罐子而发生问题。请帮忙,谢谢你的时间。

标签: javaspringmavenjar

解决方案


为什么要使用 spring-boot-maven-plugin 来构建 jar?下面是示例。

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass> com.some.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

推荐阅读