java Spring Build问题-找不到Bean架构

qzwqbdag  于 2023-06-04  发布在  Java
关注(0)|答案(4)|浏览(245)

我使用的是Maven 2和Spring 3,当我在Eclipse中运行我的项目时,一切正常,但是当我使用assembly:assembly时,结果jar抛出以下Exception:

Exception in thread "main" 
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 4 
in XML document from class path resource [beans.xml] is invalid; nested exception 
is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of 
element 'beans'.

我的beans文件看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
    <!-- Beans Here -->
</beans>

此文件存储在src/main/resources中
my pom.xml对spring有以下依赖:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>3.0.3.RELEASE</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>

知道为什么会这样吗如何修复?
更新:
进一步的探索Google发现Spring和Maven在我的pom.xml中对以下内容处理得不太好,尽管没有解决方案:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2-beta-5</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>org.robert.xclades.Main</mainClass>
            </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>
3vpjnl9f

3vpjnl9f1#

我也有同样的问题。使用shade插件有所帮助,但我必须添加一个转换器,如http://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html中所述,以concat Spring的处理程序和模式文件。

rekjcdws

rekjcdws2#

我发现的一个适用于Spring 2.5.6的解决方案是包含一个Spring jar而不是单独的jar。我不确定是否有一个用于Spring 3.x的单个jar。

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring</artifactId>
        <version>2.5.6</version>
</dependency>
xqkwcwgp

xqkwcwgp3#

问题似乎是,在各种spring依赖项的META-INF文件夹中,每个不同的spring.handlers和spring.schemas之间存在冲突。
我发现的技巧是通过在src/main/resources/META-INF中创建这些文件并将这些单独文件的内容复制和粘贴到这些文件中来覆盖这些文件。其次,使用maven-jar-plugin设置清单的mainClass;和maven-shade-plugin,而不是maven-assembly-plugin。因此,更新后的pom.xml看起来像:

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
              <mainClass>org.robert.xclades.Main</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
        </execution>
    </executions>
</plugin>

所以现在需要的是maven package

w41d8nur

w41d8nur4#

使用这个:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.4.1</version>
            <configuration>
                <createDependencyReducedPom>false</createDependencyReducedPom>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>

相关问题