Mar*_*rda 16 java spring maven-2
我正在使用Spring创建一个独立的Sava应用程序来处理JDBC访问.该应用程序在每次测试中都能正常工作,我决定需要一个jar来部署我们的客户端.
他们的类路径中可能没有spring,所以我使用maven-assembly-plugin来处理带有依赖项的jar创建.
但是,当我尝试运行该应用程序时:
java -jar target/myproject-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Run Code Online (Sandbox Code Playgroud)
这会引发以下错误:
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/p]
Offending resource: class path resource [applicationContext.xml]
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)
...and so on to the database access class of this project.
Run Code Online (Sandbox Code Playgroud)
applicationContext.xml文件位于projectbase/src/main/resources中.它放在目标/包名基础上.
applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="dataSourceDesenv" class="org.apache.commons.dbcp.BasicDataSource"... />
<bean id="simpleJdbcDaoSupport" class="org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport"
p:dataSource-ref="dataSourceDesenv" />
<bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="dataSourceDesenv" />
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
Gra*_*ohn 33
今天我用maven-assembly-plugin遇到了这个问题.搜索引出了我这个问题,然后看一下bug报告提示我可能使用了错误的插件.所以我切换到了maven-shade-plugin.据我所知,它完美无缺.我有一个可执行jar,它包含许多Spring模块以及Apache MQ.这是我的pom.xml中的相关位:
<build>
<finalName>sample-broker</finalName>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<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>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.XYZ</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
我发现错误,错误在于maven-assembly插件中一个未修复的bug.我使用了以下解决方法:
首先在我的pom中注释掉了maven-assembly代码.然后我使用maben-dependency-plugin将依赖项复制到目标的lib文件夹:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
然后我使用maven-jar-plugin来设置我的可执行jar:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<index>true</index>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>org.foo.myproject.App</mainClass>
</manifest>
<manifestEntries>
<mode>development</mode>
<url>${pom.url}</url>
<key>value</key>
</manifestEntries>
</archive>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
最后,我创建了一个bash脚本,该脚本与运行我的应用程序及其库和任何提供的参数的应用程序一起部署:
java -cp lib/*:myproject-0.0.1-SNAPSHOT.jar org.foo.myproject.App $@
Run Code Online (Sandbox Code Playgroud)
我应该在python中构建应用程序= /
看起来像在Maven Assembly插件错误- MASSEMBLY-360,并在此博客条目讨论在这里。
简而言之,处理 Spring 命名空间的 Spring JAR 中的元数据文件正在被 maven 破坏。
归档时间: |
|
查看次数: |
19234 次 |
最近记录: |