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

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

MAVEN依賴關係中Scope的作用

Dependency Scope 在POM 4中,<dependency>中還引入了<scope>,它主要管理依賴的部署。目前依賴項的作用域<scope>可以使用5個值:  
在定義項目的依賴項的時候,我們可以通過scope來指定該依賴項的作用範圍。scope的取值有compile、runtime、test、provided、system和import。
compile:這是依賴項的默認作用範圍,即當沒有指定依賴項的scope時默認使用compile。compile範圍內的依賴項在所有情況下都是有效的,包括運行、測試和編譯時。
runtime:表示該依賴項只有在運行時纔是需要的,在編譯的時候不需要。這種類型的依賴項將在運行和test的類路徑下可以訪問。
test:表示該依賴項只對測試時有用,包括測試代碼的編譯和運行,對於正常的項目運行是沒有影響的。
provided:表示該依賴項將由JDK或者運行容器在運行時提供,也就是說由Maven提供的該依賴項我們只有在編譯和測試時纔會用到,而在運行時將由JDK或者運行容器提供。
system:當scope爲system時,表示該依賴項是我們自己提供的,不需要Maven到倉庫裏面去找。指定scope爲system需要與另一個屬性元素systemPath一起使用,它表示該依賴項在當前系統的位置,使用的是絕對路徑。

 

POM文件裏面可以引用一些內置屬性(Maven預定義可以直接使用)

${basedir} 項目根目錄 
${version}表示項目版本;
${project.basedir}同${basedir};
${project.version}表示項目版本,與${version}相同;
${project.build.directory} 構建目錄,缺省爲target
${project.build.sourceEncoding}表示主源碼的編碼格式;
${project.build.sourceDirectory}表示主源碼路徑;
${project.build.finalName}表示輸出文件名稱;
${project.build.outputDirectory} 構建過程輸出目錄,缺省爲target/classes

 

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

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

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

 

  <dependencies>
  	<dependency>
  		<groupId>com.fbcds</groupId>
  		<artifactId>fbcds</artifactId>
  		<version>1.0</version>
  		<scope>system</scope>
  		<systemPath>${project.basedir}/lib/fbcds.jar</systemPath>
  	</dependency>
  </dependencies>

 


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

 

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

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

	mvn install:install-file -Dfile=fbcds-1.0.jar -DgroupId=fbcds -DartifactId=fbcds -Dversion=1.0 -Dpackaging=jar
	mvn install:install-file -Dfile=ojdbc7-1.0.jar -DgroupId=ojdbc7 -DartifactId=ojdbc7 -Dversion=1.0 -Dpackaging=jar

 2、修改項目pom文件加入包對應的依賴

 

<dependencies>
  	<dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
      <groupId>fbcds</groupId>
      <artifactId>fbcds</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency> 
      <groupId>ojdbc7</groupId>
      <artifactId>ojdbc7</artifactId>
      <version>1.0</version> 
    </dependency>
  </dependencies>

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

 完成後,在本地倉庫可看到對應的文件夾內容:



 

MAVEN如何打可執行的JAR包

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

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

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

1、測試類代碼

 

package com.lwf.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.eclink.fbcis.store.StoreDao;

public class TestClass {
	public static void main(String[] args) {
		StoreDao a = new StoreDao();
		System.out.println("------" + a.toString());
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			String sql = "select * from temp_head where temp_no='C530015I19008015'";
			Class.forName("oracle.jdbc.driver.OracleDriver");
			con = DriverManager.getConnection("jdbc:oracle:thin:@//10.101.2.19:1521/pdbqmytcis","qmytcis","qmytcis123");
			st = con.createStatement();
			rs = st.executeQuery(sql);
			if(rs.next()){
				System.out.println(rs.getString("temp_no"));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				rs.close();
				st.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

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

2、對應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>222</groupId>
  <artifactId>222</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>222</name>
  <dependencies>
  	<dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
      <groupId>fbcds</groupId>
      <artifactId>fbcds</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency> 
      <groupId>ojdbc7</groupId>
      <artifactId>ojdbc7</artifactId>
      <version>1.0</version> 
    </dependency>
  </dependencies>
	<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.lwf.test.TestClass</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</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文件如下:

<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.lwf.MavenPackage</groupId>
  <artifactId>MavenPackage</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>MavenPackage</name>
  <dependencies>
  	<dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
      <groupId>fbcds</groupId>
      <artifactId>fbcds</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency> 
      <groupId>ojdbc7</groupId>
      <artifactId>ojdbc7</artifactId>
      <version>1.0</version> 
    </dependency>
  </dependencies>
	<build>
        <plugins>
<!-- 使用 maven-shade-plugin插件打可執行包-->
<!-- 
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.lwf.test.TestClass</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
-->             

<!-- 使用 maven-Assembly-plugin插件打可執行包-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <!-- get all project dependencies -->
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <!-- MainClass in mainfest make a executable jar -->
                    <archive>
                      <manifest>
                        <mainClass>com.lwf.test.TestClass</mainClass>
                      </manifest>
                    </archive>
                </configuration>
                <executions>
                  <execution>
                    <id>make-assembly</id>
                    <phase>package</phase> 
                    <goals>
                        <goal>single</goal>
                    </goals>
                  </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</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文件如下:

<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.lwf.MavenPackage</groupId>
  <artifactId>MavenPackage</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>MavenPackage</name>
  <dependencies>
  	<dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
      <groupId>fbcds</groupId>
      <artifactId>fbcds</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency> 
      <groupId>ojdbc7</groupId>
      <artifactId>ojdbc7</artifactId>
      <version>1.0</version> 
    </dependency>
  </dependencies>
	<build>
        <plugins>
<!-- 使用 maven-shade-plugin插件打可執行包-->
<!-- 
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.lwf.test.TestClass</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
-->             

<!-- 使用 maven-Assembly-plugin插件打可執行包-->
<!-- 
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                      <manifest>
                        <mainClass>com.lwf.test.TestClass</mainClass>
                      </manifest>
                    </archive>
                </configuration>
                <executions>
                  <execution>
                    <id>make-assembly</id>
                    <phase>package</phase> 
                    <goals>
                        <goal>single</goal>
                    </goals>
                  </execution>
                </executions>
            </plugin>
-->                 
			<!-- 使用 onejar-maven-plugin插件打可執行包-->
			<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.lwf.test.TestClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.jolira</groupId>
                <artifactId>onejar-maven-plugin</artifactId>
                <version>1.4.4</version>
                <executions>
                    <execution>
                        <configuration>
                            <attachToBuild>true</attachToBuild>
                            <classifier>onejar</classifier>
                        </configuration>
                        <goals>
                            <goal>one-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</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文件如下

<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.lwf.MavenPackage</groupId>
  <artifactId>MavenPackage</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>MavenPackage</name>
  <dependencies>
  	<dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>
    <dependency>
      <groupId>fbcds</groupId>
      <artifactId>fbcds</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency> 
      <groupId>ojdbc7</groupId>
      <artifactId>ojdbc7</artifactId>
      <version>1.0</version> 
    </dependency>
  </dependencies>
	<build>
        <plugins>
<!-- 方法一:使用 maven-shade-plugin插件打可執行包-->
<!-- 
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.lwf.test.TestClass</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
-->             

<!-- 方法二:使用 maven-Assembly-plugin插件打可執行包-->
<!-- 
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                      <manifest>
                        <mainClass>com.lwf.test.TestClass</mainClass>
                      </manifest>
                    </archive>
                </configuration>
                <executions>
                  <execution>
                    <id>make-assembly</id>
                    <phase>package</phase> 
                    <goals>
                        <goal>single</goal>
                    </goals>
                  </execution>
                </executions>
            </plugin>
-->      
           
<!-- 方法三:使用 onejar-maven-plugin插件打可執行包-->
<!--  		
			<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.lwf.test.TestClass</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.jolira</groupId>
                <artifactId>onejar-maven-plugin</artifactId>
                <version>1.4.4</version>
                <executions>
                    <execution>
                        <configuration>
                            <attachToBuild>true</attachToBuild>
                            <classifier>onejar</classifier>
                        </configuration>
                        <goals>
                            <goal>one-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            
	 -->        
	    
<!-- 方法四:使用maven-jar-plugin和maven-dependency-plugin打可執行包,引用的包放包外面文件夾下 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
				<configuration>
				  <excludes>
					<exclude>**/log4j.properties</exclude>
				  </excludes>
				  <archive>
				    <manifest>
					<addClasspath>true</addClasspath>
					<mainClass>com.lwf.test.TestClass</mainClass>
					<classpathPrefix>lib/</classpathPrefix>
				    </manifest>
				  </archive>
				</configuration>
			</plugin>

			<!-- Copy project dependency -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-dependency-plugin</artifactId>
				<version>2.5.1</version>
				<executions>
				  <execution>
					<id>copy-dependencies</id>
					<phase>package</phase>
					<goals>
						<goal>copy-dependencies</goal>
					</goals>
					<configuration>
					  <!-- exclude junit, we need runtime dependency only -->
					  <includeScope>runtime</includeScope>
					  <outputDirectory>${project.build.directory}/lib/</outputDirectory>
					</configuration>
				  </execution>
				</executions>
			</plugin>            
        </plugins>
    </build>
</project>

 



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

Manifest-Version: 1.0
Built-By: lweifeng
Build-Jdk: 1.7.0_17
Class-Path: lib/log4j-1.2.17.jar lib/fbcds-1.0.jar lib/ojdbc7-1.0.jar
Created-By: Apache Maven 3.3.9
Main-Class: com.lwf.test.TestClass
Archiver-Version: Plexus Archiver

 
 在以上前三種插件打包方式中,maven-shade-plugin和maven-assembly-plugin採取的是將依賴包解壓再一併打到新包中,這樣依賴包可能存在衝突的時候,導致運行時可能出現未知問題,而onejar-maven-plugin打包是將依賴包自動歸入lib目錄,不解壓原包,相當於在原包基礎上加殼,這樣可以避免衝突的發生。第四種方法即是我們原來ant打包所使用的方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章