maven---3手寫一個helloWord

1.編寫POM

Maven項目的核心文件是pom.xml寇损,POM(Project Objcet Model)項目對象模型,該文件中定義了項目基本信息包括如何構(gòu)建羹呵、項目依賴、打包類型琢融、項目繼承等等。

為helloword項目編寫一個pom.xml文件

在I:/目錄下新建一個helloword文件夾簿寂,在文件夾中建一個pom.xml文件漾抬,內(nèi)容如下:

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
  <groupId>com.zlcook.studymvn</groupId>
  <artifactId>helloword</artifactId>
  <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
  <name>maven helloword project</name>
</project>

解釋

  • modelVersion指定了當前Pom模型的版本,對于maven2常遂、maven3寫4.0.0
  • groupId 纳令、artifactId、version三個元素定義了一個項目或者構(gòu)件(artifact )坐標克胳,在maven中任何jar平绩、pom、war都是以基于這些基本的坐標進行區(qū)分的漠另。
  • groupId定義了項目屬于哪個組捏雌,這個組一般和項目所在公司存在聯(lián)系。如baidu建立了一個名為weixin的項目笆搓,那么groupId=com.baidu.weixin性湿。
  • artifactId定義了當前maven項目在組中的唯一ID,我們?yōu)檫@個hello world項目定義artifactId為helloword.
  • packaging 指定了項目的打包類型满败,默認是jar肤频,打包類型有jar、war算墨、pom
  • version指定了helloword項目當前版本宵荒,SNAPSHOT為快照版,說明項目處于開發(fā)期净嘀。
    版本號解釋
    x.x.x-里程碑--->
    第一個x表示項目版本有大變動如架構(gòu)變化1.0->2.0报咳,第二個x表示項目分支變化,第三個x表示項目分支的變化次數(shù)面粮。
    里程碑:SNAPSHOT開發(fā)版本少孝,alpha項目組內(nèi)部測試版本,beta公開使用測試版本熬苍,Release(RC)穩(wěn)定版稍走,GA可靠版本
  • name項目名稱,方便用戶可以看的懂柴底。

2.編寫主代碼

  • 項目主代碼和測試代碼不同婿脸,項目主代碼會被打包到最終的構(gòu)件中(如jar),而測試代碼值在運行測試時用到柄驻,不會打包狐树。
  • 那么主代碼要寫在哪里才會被maven識別呢?默認maven假設(shè)項目主代碼位于src/main/java目錄下鸿脓,所以我們遵循約定在該目錄下創(chuàng)建文件com/zlcook/studymvn/helloword/HelloWord.java
    內(nèi)容如下:
package com.zlcook.studymvn.helloword;

public class HelloWord
{
    public String say(){
        return "hello maven";
    }
    public static void main(String[] args){
        System.out.print(new HelloWord().say());
    }
}

一個簡單的java類

java代碼注意

  • 在絕大多數(shù)情況下抑钟,應(yīng)該把項目主代碼放到src/main/java目錄下(遵循maven約定)涯曲,無須額外配置,maven會自動搜尋該目錄找到項目主代碼
  • java類的包名com.zlcook.studymvn.helloword在塔,這與之前在POM中定義的groupId和artifactId相吻合幻件。建議都這么寫,這樣更加清晰符合邏輯蛔溃,也方便搜索構(gòu)建或java類绰沥。
一個簡單的maven項目

2.1編譯主代碼

打開命令行,進入到項目根目錄下運行mvn clean compile得到如下結(jié)果

I:\helloword>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven helloword project 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ helloword ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ helloword
---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory I:\helloword\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ helloword ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to I:\helloword\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.383 s
[INFO] Finished at: 2016-11-15T16:30:35+08:00
[INFO] Final Memory: 14M/220M

編譯完后文件夾

編譯后多出target文件夾

解釋mvn clean compile命令

  • clean告訴maven清理輸出目錄 target/
  • compile告訴maven編譯項目主代碼贺待。

依次執(zhí)行的目標任務(wù)有

  • maven-clean-plugin:2.5:clean 刪除target文件夾徽曲,默認maven構(gòu)建的所有輸出都在target/目錄中
  • maven-resources-plugin:2.6:resources 項目主資源處理,未定義項目資源麸塞,會跳過秃臣,skip non existing resourceDirectory;
  • maven-compiler-plugin:3.1:compile 編譯主代碼喘垂,將項目主代碼編譯到target/classes目錄甜刻。
  • maven-clean-plugin:2.5:clean 绍撞、maven-resources-plugin:2.6:resources正勒、maven-compiler-plugin:3.1:compile對應(yīng)了一些Maven插件及插件目標,maven-clean-plugin:2.5:clean是maven-clean-plugin插件的clean目標傻铣。

3.編寫測試代碼

  • 為了是項目結(jié)構(gòu)清晰章贞,maven的測試代碼默認目錄是src/test/java。所以在helloword文件夾下創(chuàng)建該目錄非洲。

3.1添加測試依賴

  • 編寫的測試代碼需要依賴JUnit鸭限,所以修改pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
  <groupId>com.zlcook.studymvn</groupId>
  <artifactId>helloword</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>maven helloword project</name>
  <dependencies>
   <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency> 
</dependencies>
</project>
  • dependencies元素下可以包含多個dependency元素以聲明項目的依賴,每個依賴都是由groudId两踏,artifactId败京,version組成了構(gòu)件的唯一性。有了這段配置信息maven就會自動下載junit-4.7.jar梦染。那么maven會去哪里下載呢赡麦?答:maven會自動訪問中央倉庫(http://repol.maven.org/maven2/)下載需要文件。
  • dependency元素中還有scope配置帕识,scope為依賴范圍泛粹。scope的取值如下:
    1.test范圍指的是測試范圍有效,在編譯和打包時都不會使用這個依賴肮疗,換句話說在測試代碼中導入import JUnit代碼沒問題晶姊,但是在主代碼中使用import JUnit代碼會造成編譯錯誤,同時在主代碼中導入了測試代碼中的類伪货,在編譯時也會出錯们衙,比如“符號找不到”钾怔。因為在編譯主代碼時測試代碼包括scope為test的依賴都不會使用。 如:junit
    2.compile范圍指的是編譯范圍有效蒙挑,在編譯和打包時都會將依賴存儲進去蒂教。(默認方式)
    3.provided依賴,在編譯和測試過程有效脆荷,最后生成war包時不會加入凝垛,諸如:servlet-api,因為servlet-api,tomcat等服務(wù)器已經(jīng)存在了,如果再打包會沖突
    4.runtime在運行時依賴蜓谋,在編譯的時候不會依賴梦皮,測試運行時才依賴,如mysql-connector-java

注:在項目之間只有complie范圍的依賴才會傳遞

3.2編寫測試代碼及測試

在src/test/java目錄下寫測試代碼文件
src\test\java\com\zlcook\studymvn\helloword\HelloWordTest.java

package com.zlcook.studymvn.helloword;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class HelloWordTest
{
    @Test
    public void testSay(){
        HelloWord helloWord = new HelloWord();
        String result = helloWord.say();
        assertEquals("hello maven",result); 
        
    }
}

使用JUnit4桃焕,需要執(zhí)行測試的方法都需要加上@Test注解剑肯。
測試案例寫完,執(zhí)行maven測試命令观堂,在helloword文件下執(zhí)行命令:mvn clean test
得到結(jié)果如下:

I:\helloword>mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven helloword project 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ helloword ---
[INFO] Deleting I:\helloword\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ helloword
---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory I:\helloword\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ helloword ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to I:\helloword\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ he
lloword ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory I:\helloword\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hellowo
rd ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to I:\helloword\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ helloword ---
[INFO] Surefire report directory: I:\helloword\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.zlcook.studymvn.helloword.HelloWordTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.035 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.699 s
[INFO] Finished at: 2016-11-16T13:49:29+08:00
[INFO] Final Memory: 15M/191M
[INFO] ------------------------------------------------------------------------

依次執(zhí)行的目標有

  • maven-clean-plugin:2.5:clean 刪除target文件夾
  • maven-resources-plugin:2.6:resources 項目主資源處理
  • maven-compiler-plugin:3.1:compile 編譯主代碼
  • maven-resources-plugin:2.6:testResources 測試資源處理
  • maven-compiler-plugin:3.1:testCompile 編譯測試代碼让网,編譯通過在target/test-classes下生成了二進制文件
  • maven-surefire-plugin:2.12.4:test 運行測試代碼,surefire是maven中負責執(zhí)行測試的插件师痕,并生成測試報告surefire-reports,顯示共運行了多少測試溃睹,失敗了多少,出錯了多少胰坟,跳過了多少因篇。

由此可見,在執(zhí)行test之前會先執(zhí)行compile笔横。

4.打包和運行

4.1打包

項目編譯竞滓、測試之后,然后就可以打包(package)吹缔,Hello Word的打包指定了打包類型為jar包商佑。
maven的打包命令:mvn clean package

執(zhí)行結(jié)果:

I:\helloword>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven helloword project 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ helloword ---
[INFO] Deleting I:\helloword\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ helloword
---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory I:\helloword\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ helloword ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to I:\helloword\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ he
lloword ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory I:\helloword\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ hellowo
rd ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to I:\helloword\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ helloword ---
[INFO] Surefire report directory: I:\helloword\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.zlcook.studymvn.helloword.HelloWordTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.034 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ helloword ---
[INFO] Building jar: I:\helloword\target\helloword-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.159 s
[INFO] Finished at: 2016-11-16T14:17:36+08:00
[INFO] Final Memory: 16M/187M
[INFO] ------------------------------------------------------------------------

依次執(zhí)行的目標有

  • maven-clean-plugin:2.5:clean 刪除target文件夾
  • maven-resources-plugin:2.6:resources 項目主資源處理
  • maven-compiler-plugin:3.1:compile 編譯主代碼
  • maven-resources-plugin:2.6:testResources 測試資源處理
  • maven-compiler-plugin:3.1:testCompile 編譯測試代碼,編譯通過在target/test-classes下生成了二進制文件
  • maven-surefire-plugin:2.12.4:test
  • maven-jar-plugin:2.4:jar 打包插件厢塘,將項目主代碼打包成一個helloword-0.0.1-SNAPSHOT.jar文件到target/目錄下茶没。名稱是根據(jù)artifact-version.jar規(guī)則進行命名的, 如果不想使用該名稱俗冻,可以在pom.xml文件中使用finalName來定義文件名稱礁叔,位置
<project>
 <build>
       <finalName>項目名稱</finalName>
 </build>
</project>

由此可見在執(zhí)行打包前,執(zhí)行了編譯迄薄、測試琅关。

安裝到本地倉庫

...
[INFO] --- maven-install-plugin:2.4:install (default-install) @ helloword ---
[INFO] Installing I:\helloword\target\helloword-0.0.1-SNAPSHOT.jar to D:\Soft\ma
ven\maven_jar\repository\com\zlcook\studymvn\helloword\0.0.1-SNAPSHOT\helloword-
0.0.1-SNAPSHOT.jar
[INFO] Installing I:\helloword\pom.xml to D:\Soft\maven\maven_jar\repository\com
\zlcook\studymvn\helloword\0.0.1-SNAPSHOT\helloword-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
...

在執(zhí)行打包之前新症,依次執(zhí)行了編譯步氏、測試、打包徒爹。

  • maven-install-plugin:2.4:install 安裝荚醒,把jar包安裝到本地倉庫中,helloword項目在本體倉庫文件如圖:
HelloWord項目的pom和jar文件

4.2運行

  • helloword項目中的HelloWord類有一個main方法隆嗅,默認打包生成的jar是不能直接運行的界阁,因為帶有main方法的類信息不會添加到manifest中(解壓jar文件后,其中的META-INF/MANIFEST.MF文件沒有Main-Class一行)胖喳。

MANIFEST.MF文件信息如下:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: dell
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_65

為了生成可執(zhí)行的jar文件泡躯,需要借助maven-shade-plugin插件,在pom.xml中配置插件如下:
了解更多插件使用

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
  <groupId>com.zlcook.studymvn</groupId>
  <artifactId>helloword</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>maven helloword project</name>
<dependencies>
 <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
    <scope>test</scope>
  </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.zlcook.studymvn.helloword.HelloWord</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
  • 我們配置了mainClass為com.zlcook.studymvn.helloword.HelloWord丽焊,項目打包時會將該信息放到MANIFEST中较剃。

在執(zhí)行mvn clean install命令
執(zhí)行完后在target/目錄中可以出現(xiàn)兩個文件

Paste_Image.png

original-helloword-0.0.1-SNAPSHOT.jar 是原始的jar。
helloword-0.0.1-SNAPSHOT.jar 是帶有Main-Class信息的可運行的jar

解壓helloword-0.0.1-SNAPSHOT.jar 在META-INF/MANIFEST.MF中包含信息:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: dell
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_65
Main-Class: com.zlcook.studymvn.helloword.HelloWord

執(zhí)行jar文件

在控制臺輸入命令如下技健,可以看到輸出了hello maven結(jié)果写穴。


執(zhí)行jar文件

4.2.*生成可運行的jar文件(補充,可以忽略)

  • 一個javase項目最終通過java命令會打包生成一個jar包凫乖,一個javase項目通常會引用第三方j(luò)ar包确垫,而通過java提供的打包命令不會將第三方j(luò)ar打包到最終的jar包中弓颈。所以最終的jar因為缺少依賴而無法運行帽芽。那么如何才能讓一個普通的javase項目生成一個可執(zhí)行的jar包呢?

  • 下面一段話是在spring-boot上面看到的翔冀,提到了一種常用的方法是shaded jars导街,以及spring-boot采用的有別于shaded jars的方法,spring-boot的方法請看spring boot開發(fā)文檔的第Appendix E. The executable jar format章節(jié)纤子,上面使用的maven-shade-plugin插件生成的可運行jar就是使用shaded jars方法搬瑰。


Java does not provide any standard way to load nested jar files (i.e. jar files that are themselves contained within a jar). This can be problematic if you are looking to distribute a self-contained application that you can just run from the command line without unpacking.

To solve this problem, many developers use “shaded” jars. A shaded jar simply packages all classes, from all jars, into a single 'uber jar'. The problem with shaded jars is that it becomes hard to see which libraries you are actually using in your application. It can also be problematic if the same filename is used (but with different content) in multiple jars. Spring Boot takes a different approach and allows you to actually nest jars directly.


  • 為了驗證shade jar的原理,給HelloWorld類的內(nèi)容做了改變控硼,依賴了 org.junit.Test等類,這樣就加入了第三方依賴泽论。然后使用maven-shade-plugin打包生成的jar包和沒使用生成的jar包內(nèi)容的對比。
package com.zlcook.studymvn.helloword;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import java.util.Date;
public class HelloWord
{
    public String say(){
        return "hello maven";
    }
    public static void main(String[] args){
        HelloWord helloWord = new HelloWord();
        String result = helloWord.say();
        System.out.print(result);

        assertEquals("hello maven",result); 
    }
}
沒使用maven-shade-plugin插件生成的jar內(nèi)容
使用maven-shade-plugin插件生成的jar內(nèi)容

com卡乾、junit翼悴、org文件里是編譯后的.class文件,其中shade打包的META-INF/MANIFEST.MF中添加了Main-Class信息幔妨,正如上面一樣鹦赎。

5使用Archetype創(chuàng)建maven項目

5.1需求背景

  • 在寫helloword項目中我們遵循了maven的約定谍椅,比如主代碼寫在src/main/java,測試代碼寫在src/test/java下面古话,pom.xml文件放置項目更目錄下等雏吭。這些目錄結(jié)構(gòu)和pom.xml文件內(nèi)容稱為項目的骨架,當然java項目和javaEE項目的項目骨架是有不同的地方陪踩。當我們用maven創(chuàng)建java項目時就必須遵循這些約定杖们,也就是每次都要創(chuàng)建這些目錄和pom.xml文件。

  • 那么問題來了肩狂?每次都自己創(chuàng)建不是挺麻煩的嗎胀莹?maven不是有好多命令嗎可不可以執(zhí)行某一個命令就可以幫我們自動創(chuàng)建創(chuàng)建好目錄結(jié)構(gòu)和pom.xml文件。
    答:Maven提供了maven-archetype-plugin插件以幫助我們快速勾勒出項目骨架(當然你也可以自己開發(fā)一個)婚温。

5.2案例演示

  • 以study-archetype項目為例使用maven archetype在D:\soft盤創(chuàng)建study-archetype項目骨架描焰。

在命令行輸入mvn archetype:generate命令。
結(jié)果如下:

D:\Soft>mvn archetype:generate
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generate-sources
@ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generate-sources
@ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom --
-
[INFO] Generating project in Interactive mode
.......
1717: remote -> tr.com.obss.sdlc.archetype:obss-archetype-java (This archetype p
rovides a common skelton for the Java packages.)
1718: remote -> tr.com.obss.sdlc.archetype:obss-archetype-webapp (This archetype
 provides a skelton for the Java Web Application packages.)
1719: remote -> uk.ac.ebi.gxa:atlas-archetype (Archetype for generating a custom
 Atlas webapp)
1720: remote -> uk.ac.rdg.resc:edal-ncwms-based-webapp (-)
1721: remote -> uk.co.nemstix:basic-javaee7-archetype (A basic Java EE7 Maven ar
chetype)
1722: remote -> uk.co.solong:angular-spring-archetype (So Long archetype for RES
Tful spring services with an AngularJS frontend. Includes debian deployment)
1723: remote -> us.fatehi:schemacrawler-archetype-maven-project (-)
1724: remote -> us.fatehi:schemacrawler-archetype-plugin-command (-)
1725: remote -> us.fatehi:schemacrawler-archetype-plugin-dbconnector (-)
1726: remote -> us.fatehi:schemacrawler-archetype-plugin-lint (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive co
ntains): 888:
  • 會出現(xiàn)很多可用的archetype供選擇栅螟。
    正如在eclipse中可視化創(chuàng)建maven項目時會出現(xiàn)選擇框讓你選擇合適的archetype荆秦。


    eclipse中創(chuàng)建maven程序選擇合適archetype
  • 每個archetype前面都會對應(yīng)一個編號,同時命令行會提示一個默認的編號888,其對應(yīng)的archetype是maven-archetype-quickstart力图,該archetype適合用來創(chuàng)建普通java項目的maven結(jié)構(gòu)步绸,如果是javaEE項目可以選擇maven-archetype-webapp。

  • 直接回車選擇888吃媒,緊接著會讓你選擇maven-archetype-quickstart插件的版本瓤介,該骨架插件的版本有好幾個我們選擇編號6。

Choose org.apache.maven.archetypes:maven-archetype-quickstart version:
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6:
  • 接下來就會提示你輸入要創(chuàng)建的項目的groupId赘那、artifactId刑桑、version以及包名package
    確定項目的信息
Define value for property 'groupId': : com.zlcook.studymvn
Define value for property 'artifactId': : study-archetype
Define value for property 'version':  1.0-SNAPSHOT: :
Define value for property 'package':  com.zlcook.studymvn: : com.zlcook.studymvn
.helloword
Confirm properties configuration:
groupId: com.zlcook.studymvn
artifactId: study-archetype
version: 1.0-SNAPSHOT
package: com.zlcook.studymvn.helloword
 Y: : Y
  • 最后輸出創(chuàng)建完成信息


    創(chuàng)建完成

查看study-archetype項目

Paste_Image.png

主代碼目錄src/main/java已經(jīng)被創(chuàng)建,在該目錄下還有一個Java類 com.zlcook.studymvn.helloword.App
測試代碼目錄也被創(chuàng)建好了募舟,包含一個測試用例 com.zlcook.studymvn.helloword.AppTest祠斧。

  • pom.xml文件
    里面添加了一個junit依賴,和項目文件編碼屬性配置
<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.zlcook.studymvn</groupId>
  <artifactId>study-archetype</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>study-archetype</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Archetype可以迅速構(gòu)建一個項目骨架拱礁,如果很多項目擁有許多自定義的項目結(jié)構(gòu)以及配置文件琢锋,比如A公司要求開放的每個maven項目的目錄下都要有readme.txt文件等,則完全可以開發(fā)自己的Archetype呢灶,然后在這些項目中使用自定義的Archetype來快速生成項目骨架吴超。

6.補充:Maven打包的JAR文件目錄內(nèi)容

通過maven打包好的jar包中包含的目錄結(jié)構(gòu)分析:
hibernate-ehcache-3.6.10.Final.jar中的文件


hibernate-ehcache-3.6.10.Final.jar
  • org中時該包中說有源代碼被編譯后的字節(jié)碼文件class,這個內(nèi)容是固定的鸯乃,不管是使用maven還ant還是gradle構(gòu)建內(nèi)容都是一樣的鲸阻。hibernate-ehcache所依賴的包不會被打包進來,他的依賴信息如果是使用maven構(gòu)建的話會在META-INF中的pom.xml文件中指定。
  • META-INF:打包元數(shù)據(jù),里面的內(nèi)容赘娄,和構(gòu)建工具有一定關(guān)系仆潮。如果你不是使用maven進行打包的話,那么肯定不會有maven這個文件夾遣臼。目錄內(nèi)容如下:
META-INF中文件

MANIFEST.MF文件中內(nèi)容:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: gbadner
Build-Jdk: 1.5.0_16
Specification-Title: Hibernate Ehcache Integration
Specification-Version: 3.6.10.Final
Specification-Vendor: Hibernate.org
Implementation-Title: Hibernate Ehcache Integration
Implementation-Version: 3.6.10.Final
Implementation-Vendor-Id: org.hibernate
Implementation-Vendor: Hibernate.org
Implementation-URL: http://hibernate.org

maven目錄中包含兩個文件
org.hibernate/hibernate-ehcache/pom.properties

#Generated by Maven
#Wed Feb 08 19:23:01 CST 2012
version=3.6.10.Final
groupId=org.hibernate
artifactId=hibernate-ehcache

org.hibernate/hibernate-ehcache/pom.xml

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

    <parent>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-parent</artifactId>
        <version>3.6.10.Final</version>
        <relativePath>../hibernate-parent/pom.xml</relativePath>
    </parent>
    
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-ehcache</artifactId>
    <packaging>jar</packaging>

    <name>Hibernate Ehcache Integration</name>
    <description>Integration of Hibernate with Ehcache</description>

    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.4.3</version>
        </dependency>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>hibernate-testing</artifactId>
            <version>${project.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>1.8.0.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末性置,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子揍堰,更是在濱河造成了極大的恐慌鹏浅,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,682評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件屏歹,死亡現(xiàn)場離奇詭異隐砸,居然都是意外死亡,警方通過查閱死者的電腦和手機蝙眶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評論 3 395
  • 文/潘曉璐 我一進店門季希,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人幽纷,你說我怎么就攤上這事式塌。” “怎么了友浸?”我有些...
    開封第一講書人閱讀 165,083評論 0 355
  • 文/不壞的土叔 我叫張陵峰尝,是天一觀的道長。 經(jīng)常有香客問我收恢,道長武学,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,763評論 1 295
  • 正文 為了忘掉前任伦意,我火速辦了婚禮火窒,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘默赂。我一直安慰自己沛鸵,他們只是感情好,可當我...
    茶點故事閱讀 67,785評論 6 392
  • 文/花漫 我一把揭開白布缆八。 她就那樣靜靜地躺著,像睡著了一般疾捍。 火紅的嫁衣襯著肌膚如雪奈辰。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,624評論 1 305
  • 那天乱豆,我揣著相機與錄音奖恰,去河邊找鬼。 笑死,一個胖子當著我的面吹牛瑟啃,可吹牛的內(nèi)容都是我干的论泛。 我是一名探鬼主播,決...
    沈念sama閱讀 40,358評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼蛹屿,長吁一口氣:“原來是場噩夢啊……” “哼屁奏!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起错负,我...
    開封第一講書人閱讀 39,261評論 0 276
  • 序言:老撾萬榮一對情侶失蹤坟瓢,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后犹撒,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體折联,經(jīng)...
    沈念sama閱讀 45,722評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年识颊,在試婚紗的時候發(fā)現(xiàn)自己被綠了诚镰。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,030評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡祥款,死狀恐怖怕享,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情镰踏,我是刑警寧澤函筋,帶...
    沈念sama閱讀 35,737評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站奠伪,受9級特大地震影響跌帐,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜绊率,卻給世界環(huán)境...
    茶點故事閱讀 41,360評論 3 330
  • 文/蒙蒙 一谨敛、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧滤否,春花似錦脸狸、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至欲芹,卻和暖如春卿啡,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背菱父。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評論 1 270
  • 我被黑心中介騙來泰國打工颈娜, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留剑逃,地道東北人。 一個月前我還...
    沈念sama閱讀 48,237評論 3 371
  • 正文 我出身青樓官辽,卻偏偏與公主長得像蛹磺,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子同仆,可洞房花燭夜當晚...
    茶點故事閱讀 44,976評論 2 355

推薦閱讀更多精彩內(nèi)容