Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包

时间:2022-04-16 09:08:29

有关MAVEN仓库的理解参见:http://blog.csdn.net/wanghantong/article/details/36427433

MAVEN依赖关系中Scope的作用

Java代码   Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
  1. Dependency Scope 在POM 4中,<dependency>中还引入了<scope>,它主要管理依赖的部署。目前依赖项的作用域<scope>可以使用5个值:    
  2. 在定义项目的依赖项的时候,我们可以通过scope来指定该依赖项的作用范围。scope的取值有compile、runtime、test、provided、system和import。  
  3. compile:这是依赖项的默认作用范围,即当没有指定依赖项的scope时默认使用compile。compile范围内的依赖项在所有情况下都是有效的,包括运行、测试和编译时。  
  4. runtime:表示该依赖项只有在运行时才是需要的,在编译的时候不需要。这种类型的依赖项将在运行和test的类路径下可以访问。  
  5. test:表示该依赖项只对测试时有用,包括测试代码的编译和运行,对于正常的项目运行是没有影响的。  
  6. provided:表示该依赖项将由JDK或者运行容器在运行时提供,也就是说由Maven提供的该依赖项我们只有在编译和测试时才会用到,而在运行时将由JDK或者运行容器提供。  
  7. system:当scope为system时,表示该依赖项是我们自己提供的,不需要Maven到仓库里面去找。指定scope为system需要与另一个属性元素systemPath一起使用,它表示该依赖项在当前系统的位置,使用的是绝对路径。  

 

Java代码   Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
  1. POM文件里面可以引用一些内置属性(Maven预定义可以直接使用)  
  2.   
  3. ${basedir} 项目根目录   
  4. ${version}表示项目版本;  
  5. ${project.basedir}同${basedir};  
  6. ${project.version}表示项目版本,与${version}相同;  
  7. ${project.build.directory} 构建目录,缺省为target  
  8. ${project.build.sourceEncoding}表示主源码的编码格式;  
  9. ${project.build.sourceDirectory}表示主源码路径;  
  10. ${project.build.finalName}表示输出文件名称;  
  11. ${project.build.outputDirectory} 构建过程输出目录,缺省为target/classes  

 

如何在Maven项目中引入本地包呢?

比如我从其它项目打一个jar包,引入到现有项目中。

方法一:将待引入的包放在目录下如lib目录下,修改pom文件,加入依赖并且scope要设置为system

 

Java代码   Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
  1. <dependencies>  
  2.     <dependency>  
  3.         <groupId>com.fbcds</groupId>  
  4.         <artifactId>fbcds</artifactId>  
  5.         <version>1.0</version>  
  6.         <scope>system</scope>  
  7.         <systemPath>${project.basedir}/lib/fbcds.jar</systemPath>  
  8.     </dependency>  
  9. </dependencies>  

 
Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包


 上面设置完成后,运行mvn package命令执行成功。但打出来的包里面不包含lib目录和fbcds.jar这个引用的包,即打出来的包不是可执行的jar。所以个人开发的话可以使用这种方式,如果团队开发请使用方法二。

 

方法二:将待引入的jar包安装到本地repository中

1、先把待引入的jar包放在一个目录下,需要改一下包名,如fbcds.jar修改成fbcds-1.0.jar,如F:\lib目录,在命令行CD到lib目录,执行以下命令:

Java代码   Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
  1. mvn install:install-file -Dfile=fbcds-1.0.jar -DgroupId=fbcds -DartifactId=fbcds -Dversion=1.0 -Dpackaging=jar  
  2. mvn install:install-file -Dfile=ojdbc7-1.0.jar -DgroupId=ojdbc7 -DartifactId=ojdbc7 -Dversion=1.0 -Dpackaging=jar  

 2、修改项目pom文件加入包对应的依赖

 

Java代码   Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
  1. <dependencies>  
  2.     <dependency>  
  3.       <groupId>log4j</groupId>  
  4.       <artifactId>log4j</artifactId>  
  5.       <version>1.2.17</version>  
  6.     </dependency>  
  7.     <dependency>  
  8.       <groupId>fbcds</groupId>  
  9.       <artifactId>fbcds</artifactId>  
  10.       <version>1.0</version>  
  11.     </dependency>  
  12.     <dependency>   
  13.       <groupId>ojdbc7</groupId>  
  14.       <artifactId>ojdbc7</artifactId>  
  15.       <version>1.0</version>   
  16.     </dependency>  
  17.   </dependencies>  

 上面的fbcds和ojdbc7就是新加的引用包的依赖。

 完成后,在本地仓库可看到对应的文件夹内容:


Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
 

MAVEN如何打可执行的JAR包

前提条件:已成功将待引入的jar包安装到本地repository中

方法一、使用maven-shade-plugin插件打可执行的jar包

插件查找链接:http://maven.apache.org/plugins/

1、测试类代码

 

Java代码   Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
  1. package com.lwf.test;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7. import java.sql.Statement;  
  8.   
  9. import com.eclink.fbcis.store.StoreDao;  
  10.   
  11. public class TestClass {  
  12.     public static void main(String[] args) {  
  13.         StoreDao a = new StoreDao();  
  14.         System.out.println("------" + a.toString());  
  15.         Connection con = null;  
  16.         Statement st = null;  
  17.         ResultSet rs = null;  
  18.         try {  
  19.             String sql = "select * from temp_head where temp_no='C530015I19008015'";  
  20.             Class.forName("oracle.jdbc.driver.OracleDriver");  
  21.             con = DriverManager.getConnection("jdbc:oracle:thin:@//10.101.2.19:1521/pdbqmytcis","qmytcis","qmytcis123");  
  22.             st = con.createStatement();  
  23.             rs = st.executeQuery(sql);  
  24.             if(rs.next()){  
  25.                 System.out.println(rs.getString("temp_no"));  
  26.             }  
  27.               
  28.         } catch (Exception e) {  
  29.             e.printStackTrace();  
  30.         } finally{  
  31.             try {  
  32.                 rs.close();  
  33.                 st.close();  
  34.                 con.close();  
  35.             } catch (SQLException e) {  
  36.                 e.printStackTrace();  
  37.             }  
  38.         }  
  39.     }  
  40. }  

 上面类中引用到了fbcds和ojdbc7包的内容。

2、对应pom文件

Java代码   Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
  1. <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">  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <groupId>222</groupId>  
  4.   <artifactId>222</artifactId>  
  5.   <version>0.0.1-SNAPSHOT</version>  
  6.   <name>222</name>  
  7.   <dependencies>  
  8.     <dependency>  
  9.       <groupId>log4j</groupId>  
  10.       <artifactId>log4j</artifactId>  
  11.       <version>1.2.17</version>  
  12.     </dependency>  
  13.     <dependency>  
  14.       <groupId>fbcds</groupId>  
  15.       <artifactId>fbcds</artifactId>  
  16.       <version>1.0</version>  
  17.     </dependency>  
  18.     <dependency>   
  19.       <groupId>ojdbc7</groupId>  
  20.       <artifactId>ojdbc7</artifactId>  
  21.       <version>1.0</version>   
  22.     </dependency>  
  23.   </dependencies>  
  24.     <build>  
  25.         <plugins>  
  26.             <plugin>  
  27.                 <groupId>org.apache.maven.plugins</groupId>  
  28.                 <artifactId>maven-shade-plugin</artifactId>  
  29.                 <version>2.4.3</version>  
  30.                 <executions>  
  31.                     <execution>  
  32.                         <phase>package</phase>  
  33.                         <goals>  
  34.                             <goal>shade</goal>  
  35.                         </goals>  
  36.                         <configuration>  
  37.                             <transformers>  
  38.                                 <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
  39.                                     <mainClass>com.lwf.test.TestClass</mainClass>  
  40.                                 </transformer>  
  41.                             </transformers>  
  42.                         </configuration>  
  43.                     </execution>  
  44.                 </executions>  
  45.             </plugin>  
  46.         </plugins>  
  47.     </build>  
  48.   
  49. </project>  

 在eclipse中右键项目run as 选择Maven package,可看打包的target目录内容:


Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
 比较两个包内容:


Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
 执行包:cmd下


Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
 original-MavenPackage-0.0.1-SNAPSHOT.jar中没有主清单属性是执行不了的。

 参见:http://www.mkyong.com/maven/create-a-fat-jar-file-maven-shade-plugin/

方法二、使用maven-assembly-plugin插件打可执行的jar包

测试类与方法一中一样,只是pom不一样,pom文件如下:

Java代码   Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
  1. <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">  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <groupId>com.lwf.MavenPackage</groupId>  
  4.   <artifactId>MavenPackage</artifactId>  
  5.   <version>0.0.1-SNAPSHOT</version>  
  6.   <name>MavenPackage</name>  
  7.   <dependencies>  
  8.     <dependency>  
  9.       <groupId>log4j</groupId>  
  10.       <artifactId>log4j</artifactId>  
  11.       <version>1.2.17</version>  
  12.     </dependency>  
  13.     <dependency>  
  14.       <groupId>fbcds</groupId>  
  15.       <artifactId>fbcds</artifactId>  
  16.       <version>1.0</version>  
  17.     </dependency>  
  18.     <dependency>   
  19.       <groupId>ojdbc7</groupId>  
  20.       <artifactId>ojdbc7</artifactId>  
  21.       <version>1.0</version>   
  22.     </dependency>  
  23.   </dependencies>  
  24.     <build>  
  25.         <plugins>  
  26. <!-- 使用 maven-shade-plugin插件打可执行包-->  
  27. <!--   
  28.             <plugin>  
  29.                 <groupId>org.apache.maven.plugins</groupId>  
  30.                 <artifactId>maven-shade-plugin</artifactId>  
  31.                 <version>2.4.3</version>  
  32.                 <executions>  
  33.                     <execution>  
  34.                         <phase>package</phase>  
  35.                         <goals>  
  36.                             <goal>shade</goal>  
  37.                         </goals>  
  38.                         <configuration>  
  39.                             <transformers>  
  40.                                 <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
  41.                                     <mainClass>com.lwf.test.TestClass</mainClass>  
  42.                                 </transformer>  
  43.                             </transformers>  
  44.                         </configuration>  
  45.                     </execution>  
  46.                 </executions>  
  47.             </plugin>  
  48. -->               
  49.   
  50. <!-- 使用 maven-Assembly-plugin插件打可执行包-->  
  51.             <plugin>  
  52.                 <groupId>org.apache.maven.plugins</groupId>  
  53.                 <artifactId>maven-assembly-plugin</artifactId>  
  54.                 <version>2.6</version>  
  55.                 <configuration>  
  56.                     <!-- get all project dependencies -->  
  57.                     <descriptorRefs>  
  58.                         <descriptorRef>jar-with-dependencies</descriptorRef>  
  59.                     </descriptorRefs>  
  60.                     <!-- MainClass in mainfest make a executable jar -->  
  61.                     <archive>  
  62.                       <manifest>  
  63.                         <mainClass>com.lwf.test.TestClass</mainClass>  
  64.                       </manifest>  
  65.                     </archive>  
  66.                 </configuration>  
  67.                 <executions>  
  68.                   <execution>  
  69.                     <id>make-assembly</id>  
  70.                     <phase>package</phase>   
  71.                     <goals>  
  72.                         <goal>single</goal>  
  73.                     </goals>  
  74.                   </execution>  
  75.                 </executions>  
  76.             </plugin>  
  77.         </plugins>  
  78.     </build>  
  79.   
  80. </project>  

 修改完pom后,在eclipse中右键项目run as 选择Maven package,可看打包的target目录内容:


Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
 两个jar文件比较:


Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
 


Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
 

 参见:http://www.mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/

方法三、使用onejar-maven-plugin插件打可执行的jar包

测试类与方法一中一样,只是pom不一样,pom文件如下:

Java代码   Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
  1. <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">  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <groupId>com.lwf.MavenPackage</groupId>  
  4.   <artifactId>MavenPackage</artifactId>  
  5.   <version>0.0.1-SNAPSHOT</version>  
  6.   <name>MavenPackage</name>  
  7.   <dependencies>  
  8.     <dependency>  
  9.       <groupId>log4j</groupId>  
  10.       <artifactId>log4j</artifactId>  
  11.       <version>1.2.17</version>  
  12.     </dependency>  
  13.     <dependency>  
  14.       <groupId>fbcds</groupId>  
  15.       <artifactId>fbcds</artifactId>  
  16.       <version>1.0</version>  
  17.     </dependency>  
  18.     <dependency>   
  19.       <groupId>ojdbc7</groupId>  
  20.       <artifactId>ojdbc7</artifactId>  
  21.       <version>1.0</version>   
  22.     </dependency>  
  23.   </dependencies>  
  24.     <build>  
  25.         <plugins>  
  26. <!-- 使用 maven-shade-plugin插件打可执行包-->  
  27. <!--   
  28.             <plugin>  
  29.                 <groupId>org.apache.maven.plugins</groupId>  
  30.                 <artifactId>maven-shade-plugin</artifactId>  
  31.                 <version>2.4.3</version>  
  32.                 <executions>  
  33.                     <execution>  
  34.                         <phase>package</phase>  
  35.                         <goals>  
  36.                             <goal>shade</goal>  
  37.                         </goals>  
  38.                         <configuration>  
  39.                             <transformers>  
  40.                                 <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
  41.                                     <mainClass>com.lwf.test.TestClass</mainClass>  
  42.                                 </transformer>  
  43.                             </transformers>  
  44.                         </configuration>  
  45.                     </execution>  
  46.                 </executions>  
  47.             </plugin>  
  48. -->               
  49.   
  50. <!-- 使用 maven-Assembly-plugin插件打可执行包-->  
  51. <!--   
  52.             <plugin>  
  53.                 <groupId>org.apache.maven.plugins</groupId>  
  54.                 <artifactId>maven-assembly-plugin</artifactId>  
  55.                 <version>2.6</version>  
  56.                 <configuration>  
  57.                     <descriptorRefs>  
  58.                         <descriptorRef>jar-with-dependencies</descriptorRef>  
  59.                     </descriptorRefs>  
  60.                     <archive>  
  61.                       <manifest>  
  62.                         <mainClass>com.lwf.test.TestClass</mainClass>  
  63.                       </manifest>  
  64.                     </archive>  
  65.                 </configuration>  
  66.                 <executions>  
  67.                   <execution>  
  68.                     <id>make-assembly</id>  
  69.                     <phase>package</phase>   
  70.                     <goals>  
  71.                         <goal>single</goal>  
  72.                     </goals>  
  73.                   </execution>  
  74.                 </executions>  
  75.             </plugin>  
  76. -->                   
  77.             <!-- 使用 onejar-maven-plugin插件打可执行包-->  
  78.             <plugin>  
  79.                 <groupId>org.apache.maven.plugins</groupId>  
  80.                 <artifactId>maven-jar-plugin</artifactId>  
  81.                 <configuration>  
  82.                     <archive>  
  83.                         <manifest>  
  84.                             <mainClass>com.lwf.test.TestClass</mainClass>  
  85.                         </manifest>  
  86.                     </archive>  
  87.                 </configuration>  
  88.             </plugin>  
  89.             <plugin>  
  90.                 <groupId>com.jolira</groupId>  
  91.                 <artifactId>onejar-maven-plugin</artifactId>  
  92.                 <version>1.4.4</version>  
  93.                 <executions>  
  94.                     <execution>  
  95.                         <configuration>  
  96.                             <attachToBuild>true</attachToBuild>  
  97.                             <classifier>onejar</classifier>  
  98.                         </configuration>  
  99.                         <goals>  
  100.                             <goal>one-jar</goal>  
  101.                         </goals>  
  102.                     </execution>  
  103.                 </executions>  
  104.             </plugin>  
  105.         </plugins>  
  106.     </build>  
  107. </project>  

打包截图如下:


Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包


Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
 
Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
 
 

参见:http://www.mkyong.com/maven/maven-create-a-fat-jar-file-one-jar-example/

上文中因googlecode中已没有onejar-maven-plugin所以另请参见下文:

http://my.oschina.net/noahxiao/blog/78241

方法四:使用maven-jar-plugin和maven-dependency-plugin打可执行包,引用的包放包外面文件夹下

其他不变,pom文件如下

Java代码   Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
  1. <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">  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <groupId>com.lwf.MavenPackage</groupId>  
  4.   <artifactId>MavenPackage</artifactId>  
  5.   <version>0.0.1-SNAPSHOT</version>  
  6.   <name>MavenPackage</name>  
  7.   <dependencies>  
  8.     <dependency>  
  9.       <groupId>log4j</groupId>  
  10.       <artifactId>log4j</artifactId>  
  11.       <version>1.2.17</version>  
  12.     </dependency>  
  13.     <dependency>  
  14.       <groupId>fbcds</groupId>  
  15.       <artifactId>fbcds</artifactId>  
  16.       <version>1.0</version>  
  17.     </dependency>  
  18.     <dependency>   
  19.       <groupId>ojdbc7</groupId>  
  20.       <artifactId>ojdbc7</artifactId>  
  21.       <version>1.0</version>   
  22.     </dependency>  
  23.   </dependencies>  
  24.     <build>  
  25.         <plugins>  
  26. <!-- 方法一:使用 maven-shade-plugin插件打可执行包-->  
  27. <!--   
  28.             <plugin>  
  29.                 <groupId>org.apache.maven.plugins</groupId>  
  30.                 <artifactId>maven-shade-plugin</artifactId>  
  31.                 <version>2.4.3</version>  
  32.                 <executions>  
  33.                     <execution>  
  34.                         <phase>package</phase>  
  35.                         <goals>  
  36.                             <goal>shade</goal>  
  37.                         </goals>  
  38.                         <configuration>  
  39.                             <transformers>  
  40.                                 <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
  41.                                     <mainClass>com.lwf.test.TestClass</mainClass>  
  42.                                 </transformer>  
  43.                             </transformers>  
  44.                         </configuration>  
  45.                     </execution>  
  46.                 </executions>  
  47.             </plugin>  
  48. -->               
  49.   
  50. <!-- 方法二:使用 maven-Assembly-plugin插件打可执行包-->  
  51. <!--   
  52.             <plugin>  
  53.                 <groupId>org.apache.maven.plugins</groupId>  
  54.                 <artifactId>maven-assembly-plugin</artifactId>  
  55.                 <version>2.6</version>  
  56.                 <configuration>  
  57.                     <descriptorRefs>  
  58.                         <descriptorRef>jar-with-dependencies</descriptorRef>  
  59.                     </descriptorRefs>  
  60.                     <archive>  
  61.                       <manifest>  
  62.                         <mainClass>com.lwf.test.TestClass</mainClass>  
  63.                       </manifest>  
  64.                     </archive>  
  65.                 </configuration>  
  66.                 <executions>  
  67.                   <execution>  
  68.                     <id>make-assembly</id>  
  69.                     <phase>package</phase>   
  70.                     <goals>  
  71.                         <goal>single</goal>  
  72.                     </goals>  
  73.                   </execution>  
  74.                 </executions>  
  75.             </plugin>  
  76. -->        
  77.              
  78. <!-- 方法三:使用 onejar-maven-plugin插件打可执行包-->  
  79. <!--           
  80.             <plugin>  
  81.                 <groupId>org.apache.maven.plugins</groupId>  
  82.                 <artifactId>maven-jar-plugin</artifactId>  
  83.                 <configuration>  
  84.                     <archive>  
  85.                         <manifest>  
  86.                             <mainClass>com.lwf.test.TestClass</mainClass>  
  87.                         </manifest>  
  88.                     </archive>  
  89.                 </configuration>  
  90.             </plugin>  
  91.             <plugin>  
  92.                 <groupId>com.jolira</groupId>  
  93.                 <artifactId>onejar-maven-plugin</artifactId>  
  94.                 <version>1.4.4</version>  
  95.                 <executions>  
  96.                     <execution>  
  97.                         <configuration>  
  98.                             <attachToBuild>true</attachToBuild>  
  99.                             <classifier>onejar</classifier>  
  100.                         </configuration>  
  101.                         <goals>  
  102.                             <goal>one-jar</goal>  
  103.                         </goals>  
  104.                     </execution>  
  105.                 </executions>  
  106.             </plugin>  
  107.               
  108.      -->          
  109.           
  110. <!-- 方法四:使用maven-jar-plugin和maven-dependency-plugin打可执行包,引用的包放包外面文件夹下 -->  
  111.             <plugin>  
  112.                 <groupId>org.apache.maven.plugins</groupId>  
  113.                 <artifactId>maven-jar-plugin</artifactId>  
  114.                 <configuration>  
  115.                   <excludes>  
  116.                     <exclude>**/log4j.properties</exclude>  
  117.                   </excludes>  
  118.                   <archive>  
  119.                     <manifest>  
  120.                     <addClasspath>true</addClasspath>  
  121.                     <mainClass>com.lwf.test.TestClass</mainClass>  
  122.                     <classpathPrefix>lib/</classpathPrefix>  
  123.                     </manifest>  
  124.                   </archive>  
  125.                 </configuration>  
  126.             </plugin>  
  127.   
  128.             <!-- Copy project dependency -->  
  129.             <plugin>  
  130.                 <groupId>org.apache.maven.plugins</groupId>  
  131.                 <artifactId>maven-dependency-plugin</artifactId>  
  132.                 <version>2.5.1</version>  
  133.                 <executions>  
  134.                   <execution>  
  135.                     <id>copy-dependencies</id>  
  136.                     <phase>package</phase>  
  137.                     <goals>  
  138.                         <goal>copy-dependencies</goal>  
  139.                     </goals>  
  140.                     <configuration>  
  141.                       <!-- exclude junit, we need runtime dependency only -->  
  142.                       <includeScope>runtime</includeScope>  
  143.                       <outputDirectory>${project.build.directory}/lib/</outputDirectory>  
  144.                     </configuration>  
  145.                   </execution>  
  146.                 </executions>  
  147.             </plugin>              
  148.         </plugins>  
  149.     </build>  
  150. </project>  

 
Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包


Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包


Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包

可以看到依赖的包拷贝到了lib目录下,打的包里没有依赖包的信息,只是简单的包,不过Manifest文件class-path要包含引用名的路径

Java代码   Maven仓库理解、如何引入本地包、Maven多种方式打可执行jar包
  1. Manifest-Version: 1.0  
  2. Built-By: lweifeng  
  3. Build-Jdk: 1.7.0_17  
  4. Class-Path: lib/log4j-1.2.17.jar lib/fbcds-1.0.jar lib/ojdbc7-1.0.jar  
  5. Created-By: Apache Maven 3.3.9  
  6. Main-Class: com.lwf.test.TestClass  
  7. Archiver-Version: Plexus Archiver  

 
 在以上前三种插件打包方式中,maven-shade-plugin和maven-assembly-plugin采取的是将依赖包解压再一并打到新包中,这样依赖包可能存在冲突的时候,导致运行时可能出现未知问题,而onejar-maven-plugin打包是将依赖包自动归入lib目录,不解压原包,相当于在原包基础上加壳,这样可以避免冲突的发生。第四种方法即是我们原来ant打包所使用的方法。