maven设置打jar包并引入依赖包

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包。 

Xml代码  收藏代码
  1. <plugin>  
  2.   <artifactId>maven-assembly-plugin</artifactId>  
  3.   <configuration>  
  4.     <descriptorRefs>  
  5.       <descriptorRef>jar-with-dependencies</descriptorRef>  
  6.     </descriptorRefs>  
  7.   <archive>  
  8.     <manifest>  
  9.       <mainClass></mainClass>  
  10.     </manifest>  
  11.   </archive>  
  12.   </configuration>  
  13.   <executions>  
  14.     <execution>  
  15.       <id>make-assembly</id>  
  16.       <phase>package</phase>  
  17.       <goals>  
  18.         <goal>single</goal>  
  19.       </goals>  
  20.    </execution>  
  21.   </executions>  
  22. </plugin>  

posted on 2017-09-22 09:37  silyvin  阅读(869)  评论(0编辑  收藏  举报