隨筆 - 3  文章 - 8  trackbacks - 0
          <2006年1月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          2006年

          常用鏈接

          留言簿(1)

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          最新隨筆

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          開(kāi)始一個(gè)新的項(xiàng)目并編譯和測(cè)試

                這里將要?jiǎng)?chuàng)建第一個(gè)Maven項(xiàng)目。我們將要用到的是Maven的原型機(jī)制(archetype mechanism)原型被定義為一個(gè)在所有其他性質(zhì)相同的事物的原始式樣或模型(archetype is defined as an original pattern or model from which all other things of the same kind are made)在Maven中,一個(gè)原型是一個(gè)已經(jīng)被使用者按需剪裁組合的Maven項(xiàng)目模板,下面我們就來(lái)看一下Maven的原型機(jī)制是如何工作的。

           ps:如果想了解什么是Archetypes請(qǐng)?jiān)L問(wèn):
           
          http://maven.apache.org/guides/introduction/introduction-to-archetypes.html

          下面來(lái)建立一個(gè)最簡(jiǎn)單的項(xiàng)目、執(zhí)行下面的語(yǔ)句:

          mvn archetype:create -DgroupId=com.maventest.pro -DartifactId=my

          執(zhí)行上面的語(yǔ)句之前請(qǐng)保證你已經(jīng)聯(lián)入Internet
          C:\>md MavenTest
          C:\>cd MavenTest
          C:\MavenTest>mvn archetype:create -DgroupId=com.maventest.pro -DartifactId=my
          [INFO] Scanning for projects...
          [INFO] Searching repository for plugin with prefix: 'archetype'.
          [INFO] org.codehaus.mojo: checking for updates from central
          [INFO] artifact org.apache.maven.plugins:maven-archetype-plugin: checking for up
          dates from central
          Downloading:
          http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-archet
          ype-plugin/1.0-alpha-3/maven-archetype-plugin-1.0-alpha-3.pom
          1K downloaded
          Downloading:
          http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-plugin
          -parent/2.0-beta-1/maven-plugin-parent-2.0-beta-1.pom
          6K downloaded
          Downloading:
          http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-archet
          ype-plugin/1.0-alpha-3/maven-archetype-plugin-1.0-alpha-3.jar
          6K downloaded
          [INFO] -------------------------------------------------------------------------
          ---
          [INFO] Building Maven Default Project
          [INFO]    task-segment: [archetype:create] (aggregator-style)
          [INFO] -------------------------------------------------------------------------
          ---
          Downloading:
          http://repo1.maven.org/maven2/org/apache/maven/maven-archetype-core
          /1.0-alpha-3/maven-archetype-core-1.0-alpha-3.pom
          1K downloaded
          Downloading:
          http://repo1.maven.org/maven2/org/apache/maven/maven-archetype/1.0-
          alpha-3/maven-archetype-1.0-alpha-3.pom
          638b downloaded
          Downloading:
          http://repo1.maven.org/maven2/org/apache/maven/maven/2.0-beta-1/mav
          en-2.0-beta-1.pom
          6K downloaded
          Downloading:
          http://repo1.maven.org/maven2/plexus/plexus-utils/1.0.3/plexus-util
          s-1.0.3.pom
          970b downloaded
          Downloading:
          http://repo1.maven.org/maven2/plexus/plexus-root/1.0.3/plexus-root-
          1.0.3.pom
          5K downloaded
          Downloading:
          http://repo1.maven.org/maven2/plexus/plexus-container-default/1.0-a
          lpha-6/plexus-container-default-1.0-alpha-6.pom
          916b downloaded
          Downloading:
          http://repo1.maven.org/maven2/plexus/plexus-containers/1.0.2/plexus
          -containers-1.0.2.pom
          471b downloaded
          Downloading:
          http://repo1.maven.org/maven2/classworlds/classworlds/1.1-alpha-2/c
          lassworlds-1.1-alpha-2.pom
          3K downloaded
          Downloading:
          http://repo1.maven.org/maven2/junit/junit/3.8.1/junit-3.8.1.pom
          145b downloaded
          Downloading:
          http://repo1.maven.org/maven2/plexus/plexus-utils/1.0.2/plexus-util
          s-1.0.2.pom
          740b downloaded
          Downloading:
          http://repo1.maven.org/maven2/org/apache/maven/maven-model/2.0-beta
          -1/maven-model-2.0-beta-1.pom
          1K downloaded
          Downloading:
          http://repo1.maven.org/maven2/plexus/plexus-velocity/1.0/plexus-vel
          ocity-1.0.pom
          943b downloaded
          Downloading:
          http://repo1.maven.org/maven2/plexus/plexus-components/1.0/plexus-c
          omponents-1.0.pom
          1K downloaded
          Downloading:
          http://repo1.maven.org/maven2/plexus/plexus-root/1.0/plexus-root-1.
          0.pom
          4K downloaded
          Downloading:
          http://repo1.maven.org/maven2/commons-logging/commons-logging/1.0.2
          /commons-logging-1.0.2.pom
          810b downloaded
          Downloading:
          http://repo1.maven.org/maven2/commons-collections/commons-collectio
          ns/2.0/commons-collections-2.0.pom
          171b downloaded
          Downloading:
          http://repo1.maven.org/maven2/velocity/velocity/1.4/velocity-1.4.po
          m
          2K downloaded
          Downloading:
          http://repo1.maven.org/maven2/velocity/velocity-dep/1.4/velocity-de
          p-1.4.pom
          1K downloaded
          Downloading:
          http://repo1.maven.org/maven2/log4j/log4j/1.2.8/log4j-1.2.8.pom
          145b downloaded
          Downloading:
          http://repo1.maven.org/maven2/plexus/plexus-container-default/1.0-a
          lpha-2/plexus-container-default-1.0-alpha-2.pom
          170b downloaded
          Downloading:
          http://repo1.maven.org/maven2/org/apache/maven/maven-artifact/2.0-b
          eta-1/maven-artifact-2.0-beta-1.pom
          711b downloaded
          Downloading:
          http://repo1.maven.org/maven2/org/apache/maven/maven-plugin-api/2.0
          -beta-1/maven-plugin-api-2.0-beta-1.pom
          615b downloaded
          Downloading:
          http://repo1.maven.org/maven2/commons-logging/commons-logging/1.0.2
          /commons-logging-1.0.2.jar
          25K downloaded
          Downloading:
          http://repo1.maven.org/maven2/org/apache/maven/maven-archetype-core
          /1.0-alpha-3/maven-archetype-core-1.0-alpha-3.jar
          14K downloaded
          Downloading:
          http://repo1.maven.org/maven2/plexus/plexus-velocity/1.0/plexus-velocity-1.0.jar
          4K downloaded
          Downloading:
          http://repo1.maven.org/maven2/velocity/velocity-dep/1.4/velocity-dep-1.4.jar
          505K downloaded
          Downloading:
          http://repo1.maven.org/maven2/velocity/velocity/1.4/velocity-1.4.jar
          352K downloaded
          Downloading:
          http://repo1.maven.org/maven2/commons-collections/commons-collectio
          ns/2.0/commons-collections-2.0.jar
          88K downloaded
          Downloading:
          http://repo1.maven.org/maven2/log4j/log4j/1.2.8/log4j-1.2.8.jar
          344K downloaded
          [INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus
          .velocity.ContextClassLoaderResourceLoader'.
          [INFO] Setting property: resource.loader => 'classpath'.
          [INFO] **************************************************************
          [INFO] Starting Jakarta Velocity v1.4
          [INFO] RuntimeInstance initializing.
          [INFO] Default Properties File: org\apache\velocity\runtime\defaults\velocity.pr
          operties
          [INFO] Default ResourceManager initializing. (class org.apache.velocity.runtime.
          resource.ResourceManagerImpl)
          [INFO] Resource Loader Instantiated: org.codehaus.plexus.velocity.ContextClassLo
          aderResourceLoader
          [INFO] ClasspathResourceLoader : initialization starting.
          [INFO] ClasspathResourceLoader : initialization complete.
          [INFO] ResourceCache : initialized. (class org.apache.velocity.runtime.resource.
          ResourceCacheImpl)
          [INFO] Default ResourceManager initialization complete.
          [INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Literal
          [INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Macro
          [INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Parse
          [INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Include
          [INFO] Loaded System Directive: org.apache.velocity.runtime.directive.Foreach
          [INFO] Created: 20 parsers.
          [INFO] Velocimacro : initialization starting.
          [INFO] Velocimacro : adding VMs from VM library template : VM_global_library.vm
          [ERROR] ResourceManager : unable to find resource 'VM_global_library.vm' in any
          resource loader.
          [INFO] Velocimacro : error using  VM library template VM_global_library.vm : org
          .apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'V
          M_global_library.vm'
          [INFO] Velocimacro :  VM library template macro registration complete.
          [INFO] Velocimacro : allowInline = true : VMs can be defined inline in templates

          [INFO] Velocimacro : allowInlineToOverride = false : VMs defined inline may NOT
          replace previous VM definitions
          [INFO] Velocimacro : allowInlineLocal = false : VMs defined inline will be  glob
          al in scope if allowed.
          [INFO] Velocimacro : messages on  : VM system will output logging messages
          [INFO] Velocimacro : autoload off  : VM system will not automatically reload glo
          bal library macros
          [INFO] Velocimacro : initialization complete.
          [INFO] Velocity successfully started.
          [INFO] [archetype:create]
          [INFO] Defaulting package to group ID: com.maventest.pro
          [INFO] artifact org.apache.maven.archetypes:maven-archetype-quickstart: checking
           for updates from central
          Downloading:
          http://repo1.maven.org/maven2/org/apache/maven/archetypes/maven-arc
          hetype-quickstart/1.0-alpha-3/maven-archetype-quickstart-1.0-alpha-3.jar
          5K downloaded
          [INFO] ----------------------------------------------------------------------------
          [INFO] Using following parameters for creating Archetype: maven-archetype-quicks
          tart:RELEASE
          [INFO] ----------------------------------------------------------------------------
          [INFO] Parameter: groupId, Value: com.maventest.pro
          [INFO] Parameter: outputDirectory, Value: C:\MavenTest\my
          [INFO] Parameter: packageName, Value: com.maventest.pro
          [INFO] Parameter: package, Value: com.maventest.pro
          [INFO] Parameter: version, Value: 1.0-SNAPSHOT
          [INFO] Parameter: artifactId, Value: my
          [INFO] ResourceManager : found archetype-resources/pom.xml with loader org.codeh
          aus.plexus.velocity.ContextClassLoaderResourceLoader
          [INFO] ********************* End of debug info from resources from generated POM
           ***********************
          [INFO] ResourceManager : found archetype-resources/src/main/java/App.java with l
          oader org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader
          [INFO] ResourceManager : found archetype-resources/src/test/java/AppTest.java wi
          th loader org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader
          [INFO] Archetype created in dir: C:\MavenTest\my
          [INFO] ----------------------------------------------------------------------------
          [INFO] BUILD SUCCESSFUL
          [INFO] ----------------------------------------------------------------------------
          [INFO] Total time: 4 minutes 39 seconds
          [INFO] Finished at: Fri Jan 06 07:29:27 CST 2006
          [INFO] Final Memory: 4M/7M
          [INFO] ----------------------------------------------------------------------------
          C:\MavenTest>

              一旦你執(zhí)行后、會(huì)注意到發(fā)生了幾件事情
          1、在MavenTest目錄下出現(xiàn)了一個(gè)名為my的新項(xiàng)目工程。而且里面包含一個(gè)名字為pom.xml
          的文件。pom.xml和src是同級(jí)目錄。
          my
          |-- pom.xml
          `-- src
              |-- main
              |   `-- java
              |       `-- com
              |           `-- maventest
              |               `-- pro
              |                   `-- App.java
              `-- test
                  `-- java
                      `-- com
                          `-- maventest
                              `-- pro
                                  `-- AppTest.java

          這個(gè)pom.xml文件內(nèi)容如下:
          <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.maventest.pro</groupId>
            <artifactId>my</artifactId>
            <packaging>jar</packaging>
            <version>1.0-SNAPSHOT</version>
            <name>Maven Quick Start Archetype</name>
            <url>http://maven.apache.org</url>
            <dependencies>
              <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
              </dependency>
            </dependencies>
          </project>

          這里我們看到兩個(gè)目錄分別為應(yīng)用程序源和測(cè)試源,這是個(gè)標(biāo)準(zhǔn)的Maven項(xiàng)目布局。而這個(gè)結(jié)構(gòu)布局通常都是手工創(chuàng)建的。

          [faq]
          Q:什么是POM?
          A:Project Object Model 項(xiàng)目對(duì)象模型、Maven的基本單元。詳細(xì)了解訪問(wèn):
                   http://maven.apache.org/guides/introduction/introduction-to-the-pom.html

          2、就是Maven在首次運(yùn)行的時(shí)候要連接Internet獲取相關(guān)的文件。

          pom.xml文件中的標(biāo)簽元素說(shuō)明

          ·project                這是所有pom.xml的頂層
          ·modelVersion   這個(gè)元素指出POM現(xiàn)在使用的對(duì)象模型版本,除非開(kāi)發(fā)者強(qiáng)制性的為了
                                           確保穩(wěn)定性而進(jìn)行的必要改變、一般都是不發(fā)生變化的。
          ·groupId               這個(gè)就是指出被被創(chuàng)建項(xiàng)目的唯一標(biāo)識(shí)、通常使用域名。
          ·artifactId            這個(gè)元素指出了生成項(xiàng)目時(shí)的唯一主要的的名字、通常這個(gè)名字被作為
                                            Jar的名字。
          ·packaging          打包類型 (e.g. JAR, WAR, EAR, etc.). 這個(gè)不僅僅意味著打包的產(chǎn)品類型
                                            也指出了一個(gè)建造過(guò)程中的詳細(xì)生命周期。
          ·version                這個(gè)元素指出在項(xiàng)目被生成的時(shí)候的版本,通常你所看到的
                                            SNAPSHOT指的是開(kāi)發(fā)進(jìn)行的狀態(tài)
          ·name                   這個(gè)元素指出項(xiàng)目相識(shí)的名字、通常被用在Maven生成的文檔中
          ·url                        這個(gè)元素指出在哪里可以找到這個(gè)項(xiàng)目的站點(diǎn)、通常也被用到Maven的
                                            文檔中。
          ·description          這個(gè)元素為你的項(xiàng)目提供一個(gè)基本的描述,通常被用在Maven生成的文
                                            檔中。

          下面我們就項(xiàng)目進(jìn)行測(cè)試和編譯
          編譯
           執(zhí)行: 
          mvn compile

          C:\MavenTest\my>mvn compile
          [INFO] Scanning for projects...
          [INFO] ----------------------------------------------------------------------------
          [INFO] Building Maven Quick Start Archetype
          [INFO]    task-segment: [compile]
          [INFO] ----------------------------------------------------------------------------
          [INFO] [resources:resources]
          [INFO] Using default encoding to copy filtered resources.
          [INFO] [compiler:compile]
          [INFO] Nothing to compile - all classes are up to date
          [INFO] ----------------------------------------------------------------------------
          [INFO] BUILD SUCCESSFUL
          [INFO] ----------------------------------------------------------------------------
          [INFO] Total time: 1 second
          [INFO] Finished at: Fri Jan 06 09:08:13 CST 2006
          [INFO] Final Memory: 2M/5M
          [INFO] ----------------------------------------------------------------------------
          [INFO] Total time: 4 minutes 39 seconds 
          [INFO] Finished at: Fri Jan 06 07:29:27 CST 2006
          [INFO] Final Memory: 4M/7M  
          [INFO] ---------------------------------------------------------------------------

          測(cè)試
           執(zhí)行:
          mvn test

          C:\MavenTest\my>mvn test
          [INFO] Scanning for projects...
          [INFO] ----------------------------------------------------------------------------
          [INFO] Building Maven Quick Start Archetype
          [INFO]    task-segment: [test]
          [INFO] ----------------------------------------------------------------------------
          [INFO] [resources:resources]
          [INFO] Using default encoding to copy filtered resources.
          [INFO] [compiler:compile]
          [INFO] Nothing to compile - all classes are up to date
          [INFO] [resources:testResources]
          [INFO] Using default encoding to copy filtered resources.
          [INFO] [compiler:testCompile]
          [INFO] Nothing to compile - all classes are up to date
          [INFO] [surefire:test]
          [INFO] Setting reports dir: C:\MavenTest\my\target/surefire-reports

          -------------------------------------------------------
           T E S T S
          -------------------------------------------------------
          [surefire] Running com.maventest.pro.AppTest
          [surefire] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.015 sec

          Results :
          [surefire] Tests run: 1, Failures: 0, Errors: 0

          [INFO] ----------------------------------------------------------------------------
          [INFO] BUILD SUCCESSFUL
          [INFO] ----------------------------------------------------------------------------
          [INFO] Total time: 2 seconds
          [INFO] Finished at: Fri Jan 06 09:07:45 CST 2006
          [INFO] Final Memory: 3M/6M
          [INFO] ----------------------------------------------------------------------------

          這個(gè)時(shí)候觀察一下my 下的目錄結(jié)構(gòu)。會(huì)發(fā)現(xiàn)生成了target 目錄,這個(gè)就是Maven對(duì)程序進(jìn)行編譯和測(cè)試而生成的目標(biāo)文件夾。

          請(qǐng)注意這里compile和test是分別對(duì)兩個(gè)類進(jìn)行編譯的。compile是對(duì)App.java進(jìn)行編譯,而test是對(duì)AppTest.java進(jìn)行編譯和測(cè)試。
          如果單單是想編譯AppTest.java的話可以使用下面的命令:
                             mvn test-compile



          后面我講下如何在eclipse導(dǎo)入Maven構(gòu)建的項(xiàng)目,待續(xù)…………

          前篇:Maven 安裝

          posted on 2006-01-06 09:39 水狐 閱讀(20948) 評(píng)論(4)  編輯  收藏 所屬分類: Maven

          FeedBack:
          # re: Maven 試用手記----開(kāi)始一個(gè)新的項(xiàng)目并編譯和測(cè)試 2006-01-06 10:33 胡子魚(yú)
          暈哦,日志都粘上來(lái)了~~
          言簡(jiǎn)意賅~~  回復(fù)  更多評(píng)論
            
          # re: Maven 試用手記----開(kāi)始一個(gè)新的項(xiàng)目并編譯和測(cè)試 2006-01-06 14:48 Jet Geng
          配置文件可以說(shuō)一下。不再配置文件中設(shè)一下網(wǎng)絡(luò)代理的話會(huì)聯(lián)網(wǎng)絡(luò)有時(shí)會(huì)有問(wèn)題。找不到相應(yīng)的package且無(wú)法download。執(zhí)行會(huì)失敗。:(  回復(fù)  更多評(píng)論
            
          # re: Maven 試用手記----開(kāi)始一個(gè)新的項(xiàng)目并編譯和測(cè)試 2006-01-10 17:49 foxgem
          如果出現(xiàn)找不到相應(yīng)的package,需要在pom.xml中添加一個(gè)關(guān)于這個(gè)package的dependency;
          如果出現(xiàn)無(wú)法download,就要確認(rèn)你寫的dependency是否正確
          例如:
          <dependency>
          <groupId>springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>1.1.4</version>
          </dependency>

          如果local的repository中沒(méi)有相關(guān)的jar包,就會(huì)到如下的url中找:
          [REPO_ROOT]/<groupId>/<type>s/<artifactId>-<currentVersion>.<type>
          repo_root指的是http://www.ibiblio.org/maven/
            回復(fù)  更多評(píng)論
            
          # re: Maven 試用手記----開(kāi)始一個(gè)新的項(xiàng)目并編譯和測(cè)試 2008-07-17 10:50 beyondcf
          請(qǐng)教一個(gè)問(wèn)題,用MAVEN建立了一個(gè)工程,但是在編譯的時(shí)候提示編譯失敗日志如下:
          D:\my>mvn compile
          [INFO] Scanning for projects...
          [INFO] Building my
          [INFO] task-segment: [compile] INFO] [resources:resources]
          [INFO] Using default encoding to copy filtered resources.
          [INFO] [compiler:compile] [ERROR] BUILD ERROR
          [INFO] No such compiler 'javac'.
          [INFO] For more information, run Maven with the -e switch [INFO] Total time: 1 second
          [INFO] Finished at: Thu Jul 17 10:39:20 CST 2008
          [INFO] Final Memory: 2M/6M

          但是我的jdk可以用的,也能夠編譯java 文件 ,在edlipse 中也能對(duì)eclipse的工程文件進(jìn)行編譯和運(yùn)行.執(zhí)行javac -version 也能夠看到 Java的版本

          請(qǐng)教樓主和各位MAVEN高手,誰(shuí)能告訴我錯(cuò)誤出在哪里了啊,謝謝了。  回復(fù)  更多評(píng)論
            

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 田阳县| 墨江| 镇赉县| 天等县| 永清县| 汉阴县| 达尔| 前郭尔| 林甸县| 宣城市| 伽师县| 饶河县| 阜宁县| 连南| 梁平县| 鹤壁市| 安平县| 洛隆县| 黄梅县| 图片| 蓬溪县| 进贤县| 合水县| 宾阳县| 牙克石市| 福泉市| 兖州市| 石景山区| 永新县| 理塘县| 新竹县| 泗洪县| 茂名市| 永仁县| 紫云| 塘沽区| 同德县| 营山县| 栾川县| 商丘市| 密山市|