我正在尝试使用maven shade插件构建一个具有依赖关系的单个可执行jar文件。在我的pom.xml中,我添加了一个依赖于我本地编译的包(mvn install),这也是一个基于spring框架的包。但是当我运行我的应用程序时,我得到以下错误。
线程"main"异常org.springframework.beans.factory. parse . beandefinitionparsingexception:配置问题:无法定位XML模式命名空间的Spring NamespaceHandler [http://www.springframework.org/schema/context]违规资源:类路径资源[applicationContext.xml]
今天花了一半的时间搜索答案,我发现不同的springframework模块的manifest文件可以相互覆盖,因此我必须使用AppendingTransformer,这样它们就可以被附加而不是被覆盖。所以我添加了这些线,但它仍然失败。会出什么问题呢?
是我的插件定义。
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>my-spring-app</finalName>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>jar-with-dependencies</shadedClassifierName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.mycomp.App</mainClass>
</transformer>
<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>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.tooling</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
这是我的applicationContext.xml (partial)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath*:test.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- C3P0 properties -->
<property name="acquireIncrement" value="${acquireIncrement}" />
<property name="minPoolSize" value="${minPoolSize}" />
<property name="maxPoolSize" value="${maxPoolSize}" />
<property name="maxIdleTime" value="${maxIdleTime}" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="default_schema">test</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.mycomp.database.myVO</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<tx:annotation-driven transaction-manager="transactionManager" />
<task:annotation-driven />
</beans>
Maven-shade-plugin是喜怒无常的,根据我的经验,一旦你有了几个类和几个库jar,从它那里得到一个好的jar可能会很麻烦。Capsule是一个有趣的替代方案,它使用启动器来设置类路径,如果您愿意,甚至可以从Maven Central下载依赖项!有一个maven插件,但它仍处于早期开发阶段。
也就是说,您需要mvn clean package
来强制重新构建阴影jar
我已经通过为我的春季项目进行无xml设置解决了这个问题。我在@Configurations类中定义了我的spring bean,并为我解决了这个问题。
好的。我终于找到了问题所在。我的电脑配置不正确。
我有阴影插件条目如下。
<plugins>
<pluginManagement>
<plugin>
.... shade stuff
</plugin>
</pluginManagement>
</plugins>
解决方案是将它放在pluginManagement之外,如下所示。
<plugins>
<plugin>
... shade stuff
</plugin>
<pluginManagement/>
</plugins>