1. 程式人生 > >Maven倉庫理解、如何引入本地包、Maven多種方式打可執行jar包

Maven倉庫理解、如何引入本地包、Maven多種方式打可執行jar包

依賴 tro 個人 部署 格式 多種方式 ava null 路徑

轉載博客:http://quicker.iteye.com/blog/2319947

有關MAVEN倉庫的理解參見:http://blog.csdn.net/wanghantong/article/details/36427433

MAVEN依賴關系中Scope的作用

Java代碼 技術分享
  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代碼 技術分享
  1. POM文件裏面可以引用一些內置屬性(Maven預定義可以直接使用)
  2. ${basedir} 項目根目錄
  3. ${version}表示項目版本;
  4. ${project.basedir}同${basedir};
  5. ${project.version}表示項目版本,與${version}相同;
  6. ${project.build.directory} 構建目錄,缺省為target
  7. ${project.build.sourceEncoding}表示主源碼的編碼格式;
  8. ${project.build.sourceDirectory}表示主源碼路徑;
  9. ${project.build.finalName}表示輸出文件名稱;
  10. ${project.build.outputDirectory} 構建過程輸出目錄,缺省為target/classes

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

比如我從其它項目打一個jar包,引入到現有項目中。

方法一:將待引入的包放在目錄下如lib目錄下,修改pom文件,加入依賴並且scope要設置為system

Java代碼 技術分享
  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>


技術分享


上面設置完成後,運行mvn package命令執行成功。但打出來的包裏面不包含lib目錄和fbcds.jar這個引用的包,即打出來的包不是可執行的jar。所以個人開發的話可以使用這種方式,如果團隊開發請使用方法二。

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

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

Java代碼 技術分享
  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代碼 技術分享
  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如何打可執行的JAR包

前提條件:已成功將待引入的jar包安裝到本地repository中

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

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

1、測試類代碼

Java代碼 技術分享
  1. package com.lwf.test;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import com.eclink.fbcis.store.StoreDao;
  8. public class TestClass {
  9. public static void main(String[] args) {
  10. StoreDao a = new StoreDao();
  11. System.out.println("------" + a.toString());
  12. Connection con = null;
  13. Statement st = null;
  14. ResultSet rs = null;
  15. try {
  16. String sql = "select * from temp_head where temp_no=‘C530015I19008015‘";
  17. Class.forName("oracle.jdbc.driver.OracleDriver");
  18. con = DriverManager.getConnection("jdbc:oracle:thin:@//10.101.2.19:1521/pdbqmytcis","qmytcis","qmytcis123");
  19. st = con.createStatement();
  20. rs = st.executeQuery(sql);
  21. if(rs.next()){
  22. System.out.println(rs.getString("temp_no"));
  23. }
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. } finally{
  27. try {
  28. rs.close();
  29. st.close();
  30. con.close();
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. }
  36. }

上面類中引用到了fbcds和ojdbc7包的內容。

2、對應pom文件

Java代碼 技術分享
  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. </project>

在eclipse中右鍵項目run as 選擇Maven package,可看打包的target目錄內容:


技術分享
比較兩個包內容:


技術分享
執行包:cmd下


技術分享
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代碼 技術分享
  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. <!-- 使用 maven-Assembly-plugin插件打可執行包-->
  50. <plugin>
  51. <groupId>org.apache.maven.plugins</groupId>
  52. <artifactId>maven-assembly-plugin</artifactId>
  53. <version>2.6</version>
  54. <configuration>
  55. <!-- get all project dependencies -->
  56. <descriptorRefs>
  57. <descriptorRef>jar-with-dependencies</descriptorRef>
  58. </descriptorRefs>
  59. <!-- MainClass in mainfest make a executable jar -->
  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. </plugins>
  77. </build>
  78. </project>

修改完pom後,在eclipse中右鍵項目run as 選擇Maven package,可看打包的target目錄內容:


技術分享
兩個jar文件比較:


技術分享


技術分享

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

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

測試類與方法一中一樣,只是pom不一樣,pom文件如下:

Java代碼 技術分享
  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. <!-- 使用 maven-Assembly-plugin插件打可執行包-->
  50. <!--
  51. <plugin>
  52. <groupId>org.apache.maven.plugins</groupId>
  53. <artifactId>maven-assembly-plugin</artifactId>
  54. <version>2.6</version>
  55. <configuration>
  56. <descriptorRefs>
  57. <descriptorRef>jar-with-dependencies</descriptorRef>
  58. </descriptorRefs>
  59. <archive>
  60. <manifest>
  61. <mainClass>com.lwf.test.TestClass</mainClass>
  62. </manifest>
  63. </archive>
  64. </configuration>
  65. <executions>
  66. <execution>
  67. <id>make-assembly</id>
  68. <phase>package</phase>
  69. <goals>
  70. <goal>single</goal>
  71. </goals>
  72. </execution>
  73. </executions>
  74. </plugin>
  75. -->
  76. <!-- 使用 onejar-maven-plugin插件打可執行包-->
  77. <plugin>
  78. <groupId>org.apache.maven.plugins</groupId>
  79. <artifactId>maven-jar-plugin</artifactId>
  80. <configuration>
  81. <archive>
  82. <manifest>
  83. <mainClass>com.lwf.test.TestClass</mainClass>
  84. </manifest>
  85. </archive>
  86. </configuration>
  87. </plugin>
  88. <plugin>
  89. <groupId>com.jolira</groupId>
  90. <artifactId>onejar-maven-plugin</artifactId>
  91. <version>1.4.4</version>
  92. <executions>
  93. <execution>
  94. <configuration>
  95. <attachToBuild>true</attachToBuild>
  96. <classifier>onejar</classifier>
  97. </configuration>
  98. <goals>
  99. <goal>one-jar</goal>
  100. </goals>
  101. </execution>
  102. </executions>
  103. </plugin>
  104. </plugins>
  105. </build>
  106. </project>

打包截圖如下:


技術分享


技術分享

技術分享

參見: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代碼 技術分享
  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. <!-- 方法二:使用 maven-Assembly-plugin插件打可執行包-->
  50. <!--
  51. <plugin>
  52. <groupId>org.apache.maven.plugins</groupId>
  53. <artifactId>maven-assembly-plugin</artifactId>
  54. <version>2.6</version>
  55. <configuration>
  56. <descriptorRefs>
  57. <descriptorRef>jar-with-dependencies</descriptorRef>
  58. </descriptorRefs>
  59. <archive>
  60. <manifest>
  61. <mainClass>com.lwf.test.TestClass</mainClass>
  62. </manifest>
  63. </archive>
  64. </configuration>
  65. <executions>
  66. <execution>
  67. <id>make-assembly</id>
  68. <phase>package</phase>
  69. <goals>
  70. <goal>single</goal>
  71. </goals>
  72. </execution>
  73. </executions>
  74. </plugin>
  75. -->
  76. <!-- 方法三:使用 onejar-maven-plugin插件打可執行包-->
  77. <!--
  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. -->
  106. <!-- 方法四:使用maven-jar-plugin和maven-dependency-plugin打可執行包,引用的包放包外面文件夾下 -->
  107. <plugin>
  108. <groupId>org.apache.maven.plugins</groupId>
  109. <artifactId>maven-jar-plugin</artifactId>
  110. <configuration>
  111. <excludes>
  112. <exclude>**/log4j.properties</exclude>
  113. </excludes>
  114. <archive>
  115. <manifest>
  116. <addClasspath>true</addClasspath>
  117. <mainClass>com.lwf.test.TestClass</mainClass>
  118. <classpathPrefix>lib/</classpathPrefix>
  119. </manifest>
  120. </archive>
  121. </configuration>
  122. </plugin>
  123. <!-- Copy project dependency -->
  124. <plugin>
  125. <groupId>org.apache.maven.plugins</groupId>
  126. <artifactId>maven-dependency-plugin</artifactId>
  127. <version>2.5.1</version>
  128. <executions>
  129. <execution>
  130. <id>copy-dependencies</id>
  131. <phase>package</phase>
  132. <goals>
  133. <goal>copy-dependencies</goal>
  134. </goals>
  135. <configuration>
  136. <!-- exclude junit, we need runtime dependency only -->
  137. <includeScope>runtime</includeScope>
  138. <outputDirectory>${project.build.directory}/lib/</outputDirectory>
  139. </configuration>
  140. </execution>
  141. </executions>
  142. </plugin>
  143. </plugins>
  144. </build>
  145. </project>


技術分享


技術分享


技術分享

可以看到依賴的包拷貝到了lib目錄下,打的包裏沒有依賴包的信息,只是簡單的包,不過Manifest文件class-path要包含引用名的路徑

Java代碼 技術分享
  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打包所使用的方法。

    • MavenPackage.zip (5.2 KB)
    • 下載次數: 17

Maven倉庫理解、如何引入本地包、Maven多種方式打可執行jar包