http://www.cnblogs.com/sheeva/p/5032706.html
--------------------------------------------------------方法一:将jar包和项目打在一起---------------------------------
1.修改pom根节点下packaging属性为jar
2.修改pom根节点下build为:
<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.hbase2Redis.Hbase2Redis</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
3.使用maven install 打包,将在target下生成项目jar包xxx-with-dependencies.jar复制出来,执行java -jar xxx-with-dependencies.jar即可运行。
--------------------------------------------------------方法二:将jar包和项目分离---------------------------------------
1.修改pom根节点下packaging属性为jar
2.修改pom根节点下build为:
<build> <finalName>Hbase2Redis</finalName> <pluginManagement> <plugins> <plugin> <groupId>org.eclipse.m2e</groupId> <artifactId>lifecycle-mapping</artifactId> <version>1.0.0</version> <configuration> <lifecycleMappingMetadata> <pluginExecutions> <pluginExecution> <pluginExecutionFilter> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <versionRange>[2.0,)</versionRange> <goals> <goal>copy-dependencies</goal> </goals> </pluginExecutionFilter> <action> <ignore /> </action> </pluginExecution> </pluginExecutions> </lifecycleMappingMetadata> </configuration> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <!-- 主方法所在类名 --> <mainClass>com.hbase2Redis.Hbase2Redis</mainClass> </manifest> </archive> </configuration> </plugin> <!-- 将依赖包放到lib文件夹中 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>copy</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory> ${project.build.directory}/lib </outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build>
3.使用maven install 打包,将在target下生成项目jar包xxx.jar及lib文件夹,将这两者放在同一目录下,执行java -jar xxx.jar即可运行。
http://songpo-ath-taobao-com.iteye.com/blog/2018341
将依赖打入jar包,由于maven管理了所有的依赖,所以将项目的代码和依赖打成一个包对它来说是顺理成章的功能。maven的这个功能之前就用过,但这次使用时忘了细节,只记得用maven的assembly插件,但assembly插件功能强大,可以打zip、war各种包,所以一下子找不到如何将依赖打入jar包了。浪费了一点时间,所以一定要记录一下。
在pom.xml中加入如下配置即可,关键是configuration-descriptorRefs-descriptorRef,这个就表示要将依赖打入jar包。
在pom.xml中加入如下配置即可,关键是configuration-descriptorRefs-descriptorRef,这个就表示要将依赖打入jar包。
- <plugin>
- <artifactId>maven-assembly-plugin</artifactId>
- <configuration>
- <descriptorRefs>
- <descriptorRef>jar-with-dependencies</descriptorRef>
- </descriptorRefs>
- <archive>
- <manifest>
- <mainClass></mainClass>
- </manifest>
- </archive>
- </configuration>
- <executions>
- <execution>
- <id>make-assembly</id>
- <phase>package</phase>
- <goals>
- <goal>single</goal>
- </goals>
- </execution>
- </executions>
- </plugin>