标签:maven jar
技术点:spring加载xsd过程:
http://blog.csdn.net/bluishglc/article/details/7596118
案发背景:
1、一个使用maven管理的普通项目(vcenter-collector :关于Vcenter数据采集)
2、项目主要技术: java + mybatis(基于接口形式)+其他
3、项目中有一个非maven仓库的jar包:vijava(做vcenter数据采集的),在项目中以system方式引用
(该jar放在${project.basedir}/lib下)
需求: 将该项目打包为一个可执行jar
案发经过:
1、引用maven的插件(maven-assembly-plugin)打有依赖的jar包
<plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>com.main.VcenterCollectMain</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin>
利用命令: mvn clean assembly:assembly 进行打包,
打好包后运行:java -jar Vcenter-Collector.jar
运行报错:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/context]
然后开始在网上搜罗答案,结果有惊喜啊,发现spring配置文件在加载的时候需要相关的xsd配合,只管自己没了解spring配置文件加载细节,在iteye上有一篇帖子:
http://chenzhou123520.iteye.com/blog/1971322 说明了原因,并给出了解决方案
好吧,按照帖子方案选择了maven-shade-plugin 插件,修改配置如下:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version> 1.7.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.chenzhou.test.Main</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin>
主要是靠下面配置(打包时分别合并所有的spring.schemas与spring.handlers文件,这样就不会丢失):
<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>com.main.VcenterCollectMain</mainClass> </transformer> </transformers>
这段配置意思是把spring.handlers和spring.schemas文件以append方式加入到构建的jar包中。
好了,接着打包:mvn clean package ,这下前面的命名空间问题解决了,但虎去狼来啊,又有新问题:由于项目采用mybatis,而且采用接口形式开发,将mapper文件统统放到了java类目录,而不是资源目录,截图如下:
结果,导致maven打包时,这些xml文件打不进去,没办法,接着引入<resource>配置:
<resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource>
好了,这下mapper打进去了,运行时又出错了,找不到类,这些类正好是通过system方式管理的本地jar:
<dependency> <groupId>vijava</groupId> <artifactId>vijava</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/vijava55b20130927.jar</systemPath> </dependency>
又开始百度,,找到答案,说是要利用<resource>配置指定jar位置,加入如下配置:
<resource> <targetPath>lib/</targetPath> <directory>lib/</directory> <includes> <include>**/vijava55b20130927.jar</include> </includes> </resource>
好了,打包mvn clean package ,打包正常,这个jar在jar包中在单独的lib目录中,好了准备运行jar试试,结果,报错,说找不到类,而该类就是lib中的类,看来这样虽然可以将system管理的包打到jar中,但运行时却引用不到,看来不行啊,继续找,找。。
有人说,将jar安装到本地仓库,我也想啊,但是,别人拿到项目还不是得在自己的本地再次安装一遍,太费事,采取终极方案:
添加 in project repository,在新机器上执行时就不用运行mvn install:install-file命令了
配置如下:
<repositories> <repository> <id>in-project</id> <name>In Project Repo</name> <url>file://${project.basedir}/lib</url> </repository> </repositories> <dependency> <groupId>vijava</groupId> <artifactId>vijava</artifactId> <version>1.0</version> </dependency>
本项目配置如下:
注意:
你的jar包及路径必须严格遵循格式: /groupId/artifactId/version/artifactId-verion.jar 本例中: lib/org/richard/my-jar/1.0/my-jar-1.0.ja maven-shade-plugin插件有个配置属性:createDependencyReducedPom,默认值为true. 注意这个属性,如果你用这个插件来deploy,或者发布到中央仓库 这个属性会缩减你的pom文件,会把你依赖的<dependency>干掉 正确的做法是把这个值改成false,如果为true,则会生成一个裁剪的pom文件: dependency-reduced-pom.xml 应该将该值改为false: <configuration> <createDependencyReducedPom>false</createDependencyReducedPom> </configuration>
最后给出本项目完整配置的pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.huhu</groupId> <artifactId>VcenterCollector</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>VcenterCollector</name> <url>http://maven.apache.org</url> <repositories> <repository> <id>in-project</id> <name>In Project Repo</name> <url>file://${project.basedir}/lib</url> </repository> </repositories> <properties> <spring.version>4.1.4.RELEASE</spring.version> <mybatis.version>3.2.6</mybatis.version> <slf4j.version>1.7.7</slf4j.version> <log4j.version>1.2.17</log4j.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8</version> <scope>test</scope> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.5</version> </dependency> <!-- end spring --> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.30</version> </dependency> <!-- 添加druid连接池包 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.12</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>vijava</groupId> <artifactId>vijava</artifactId> <version>1.0</version> </dependency> </dependencies> <build> <finalName>VcenterCollector</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version> 1.7.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <createDependencyReducedPom>false</createDependencyReducedPom> <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>com.main.VcenterCollectMain</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
标签:maven jar
原文地址:http://langlichong.blog.51cto.com/8846944/1754239