Vcenter数据采集之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>
时间: 2024-12-24 16:44:49

Vcenter数据采集之maven打包可执行jar血历史的相关文章

maven 打包可执行jar 问题小结

最近项目用到maven,完成后要将项目打包成可执行的jar,在网上搜了很多打包方法,但总有个个别问题在那里 pom配置文件增加下面配置,执行mvn clean package <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configur

Maven打包可执行Jar的几种方法

一.无依赖其他任何jar <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifest&g

maven打包可执行jar包

创建一个文件夹haha,里面包含三项 Haha.java Main.java pom.xml pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:sch

Apache Maven 打包可执行jar

在本文的 参考资料 部分,您将发现大量介绍 Maven 的入门教程.本文的 5 个技巧目的是帮助您解决即将出现的一些问题:使用 Maven 管理您的应用程序的生命周期时,将会出现的编程场景. 1. 可执行的 JAR 文件 使用 Maven 构建一个 JAR 文件比较容易:只要定义项目包装为 “jar”,然后执行包装生命周期阶段即可.但是定义一个可执行 JAR 文件却比较麻烦.采取以下步骤可以更高效: 在您定义可执行类的 JAR 的 MANIFEST.MF 文件中定义一个 main 类.(MANI

Spring Boot Maven 打包可执行Jar文件!

Maven pom.xml 必须包含 [plain] view plain copy <packaging>jar</packaging> [html] view plain copy <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</ar

maven 打包可执行jar的方法

转自:http://blog.csdn.net/johnnywww/article/details/7964326 1.修改pom.xml增加如下内容 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration&g

Maven打包可执行jar

参考文献:http://blog.csdn.net/xiao__gui/article/details/47341385 方法:使用assembly插件,生成的jar包名为xxx-jar-with-dependencies 指定mainClass会把主类写到MANIFEST.MF文件里 指定jar-with-dependencies会把依赖考到jar包里. 1 <build> 2 <plugins> 3 <plugin> 4 <groupId>org.apa

Spring Boot Maven 打包可执行Jar文件

本文转载自:http://blog.csdn.net/smilecall/article/details/56288972 Maven pom.xml 必须包含 <packaging>jar</packaging> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-mave

maven 构建可执行jar文件

第一部分:认识jar中的MANIFEST.MF文件 一.MANIFEST.MF文件的基本配置信息: 1.一般属性: Manifest-Version:定义manifest文件的版本 Created-By:定义该文件的生成者,一般是由jar命令行工具生成 Signature-Version:定义jar文件的签名版本(关于jar文件的签名以后详谈) Class-Path:(具体作用在下面讲) 2.应用程序相关属性: Main-Class:定义jar文件的入口类(该类必须是一个可执行的类):定义该属性