将Spring应用打包为可执行的JAR文件会导致’Failed to read schema document’错误。

huangapple go评论70阅读模式
英文:

Packaging spring application in executable jar causes 'Failed to read schema document'

问题

我已将Spring应用程序打包成可执行的JAR,以下是相应的pom.xml/Maven配置:

<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打包时,同样的应用程序可以正常执行。我确信这不是依赖问题,我已经提供了适当的依赖项。问题是由于打包成JAR文件导致的。请帮忙解决,非常感谢您的时间。

英文:

I have packaged a spring application into executable jar with following pom.xml/maven configuration

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

Now when I execute this generated jar it gives following WARN

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

and execution fails with exception

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

And same application executes well when run without jar packaging in eclipse. I am sure it is not dependency issue, I have provided appropriate dependencies.
Issue happens due to packaging into jar.
Please help, thanks for you time.

答案1

得分: 1

为什么要使用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>
英文:

Why do you use spring-boot-maven-plugin to build jar? The below is sample.

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

			&lt;plugin&gt;
				&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
				&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
				&lt;configuration&gt;
					&lt;mainClass&gt; com.some.Main&lt;/mainClass&gt;
				&lt;/configuration&gt;
			&lt;/plugin&gt;
		&lt;/plugins&gt;
	&lt;/build&gt;

huangapple
  • 本文由 发表于 2020年10月12日 22:23:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64319754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定