Maven项目编译工具的使用
生活随笔
收集整理的這篇文章主要介紹了
Maven项目编译工具的使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Java項目中最常用的編譯工具是Ant,Ant解析XML配置文件,即build.xml,執(zhí)行Target來實現(xiàn)用戶目標(biāo)的編譯工作。Maven也是一個非常好用的編譯工具,Maven類似于Ant也是有一個配置文件來配置編譯信息,即pom.xml. 本文簡要描述Maven的簡單實用。 1,Maven的安裝 Maven安裝很簡單,下載包下來,加壓到目錄,例如D:\maven。 將Maven bin目錄加到path環(huán)境變量: path=%path%;d:\maven\bin 這樣打開CMD就可以在任意位置執(zhí)行 mvn 命令了。 檢驗安裝是否成功的命令: D:\Nokia\SVN> mvn -version
Apache Maven 2.2.1 (r801777; 2009-08-07 03:16:01+0800)
Java version: 1.6.0_27
Java home: C:\Program Files\Java\jdk1.6.0_27\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows xp" version: "5.1" arch: "x86" Family: "windows" 2, Maven 生成項目目錄結(jié)構(gòu) 這個功能可以自動為我們生成標(biāo)準(zhǔn)的項目目錄結(jié)構(gòu),不過經(jīng)驗上看,其實這個功能用的少,自己在開發(fā)工具上創(chuàng)建也花不了多少時間。 mvn archetype:create -DgroupId=com.nokia -DartifactId=testApp 生成的目錄結(jié)構(gòu)為: testApp
----src
??? ----main
??????? ----java
????????? ----com
????????????? ----nokia
??? ----test
??????? ----java
??????????? ----com
??????????????? ----nokia 3,創(chuàng)建Maven 配置文件pom.xml pom.xml是Maven的配置文件,在這里可以配置項目名稱,項目依賴等等 示例: <?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">
????<parent>
????????<artifactId>testApp</artifactId>
????????<groupId>com.testApp</groupId>
????????<version>1.6.11</version>
????</parent>
????<modelVersion>4.0.0</modelVersion>
????<artifactId>testApp-config-utils</artifactId>
????<name>testApp Config Utilities</name>
????<dependencies>
????????<dependency>
????????????<groupId>com.testApp2</groupId>
????????????<artifactId>testApp-util</artifactId>
????????????<scope>compile</scope>
????????</dependency>
????????<dependency>
????????????<groupId>junit</groupId>
????????????<artifactId>junit</artifactId>
????????????<version>3.8.1</version>
????????????<scope>test</scope>
????????</dependency>
????</dependencies>
</project> 4, Maven編譯命令 創(chuàng)建好項目和配置文件后,我們就可以進(jìn)入pom.xml文件所在目錄執(zhí)行以下命令進(jìn)行編譯工作: --mvn test:運行應(yīng)用程序中的單元測試
--mvn package:依據(jù)項目生成jar文件
--mvn install:將項目的jar文件添加到庫中,以備依賴此項目時使用
--mvn site:生成項目相關(guān)信息的網(wǎng)站
--mvn clean:清除目標(biāo)目錄中的生成結(jié)果
--mvn eclipse:eclipse:生成Eclipse項目文件 5, 有用的一些Tips 5.1 忽略失敗的單元測試,讓編譯繼續(xù)執(zhí)行: <plugin>????????????????????
??<groupId> org.apache.maven.plugins </groupId>????????
??<artifactId> maven-surefire-plugin </artifactId>????
??<configuration>????
????<testFailureIgnore> true </testFailureIgnore>????
??</configuration>????????????
</plugin> 5.2 不執(zhí)行單元測試 <plugin>????????????????????
??<groupId> org.apache.maven.plugins </groupId>????
??<artifactId> maven-surefire-plugin </artifactId>????
??<configuration>????
????<skip> true </skip>????
??</configuration>????
</plugin> 5.3 配置存儲庫 要求項目的每個開發(fā)者必須在conf目錄中配置存儲庫是不方便的,所以Maven可以同時查看多個存儲庫并且將它們?nèi)颗渲迷趐om.xml文件中。
<repositories>
????????<repository>
????????????????????<id>Ibiblio</id>
????????????????????<name>Ibiblio</name>
????????????????????<url>http://www.ibiblio.org/maven/</url>
????????</repository>
????????<repository>
????????????????????<id>PlanetMirror</id>
????????????????????<name>Planet Mirror</name>
????????????????????<url>http://public.planetmirror.com/pub/maven/</url>
????????</repository>
</repositories>
Apache Maven 2.2.1 (r801777; 2009-08-07 03:16:01+0800)
Java version: 1.6.0_27
Java home: C:\Program Files\Java\jdk1.6.0_27\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows xp" version: "5.1" arch: "x86" Family: "windows" 2, Maven 生成項目目錄結(jié)構(gòu) 這個功能可以自動為我們生成標(biāo)準(zhǔn)的項目目錄結(jié)構(gòu),不過經(jīng)驗上看,其實這個功能用的少,自己在開發(fā)工具上創(chuàng)建也花不了多少時間。 mvn archetype:create -DgroupId=com.nokia -DartifactId=testApp 生成的目錄結(jié)構(gòu)為: testApp
----src
??? ----main
??????? ----java
????????? ----com
????????????? ----nokia
??? ----test
??????? ----java
??????????? ----com
??????????????? ----nokia 3,創(chuàng)建Maven 配置文件pom.xml pom.xml是Maven的配置文件,在這里可以配置項目名稱,項目依賴等等 示例: <?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">
????<parent>
????????<artifactId>testApp</artifactId>
????????<groupId>com.testApp</groupId>
????????<version>1.6.11</version>
????</parent>
????<modelVersion>4.0.0</modelVersion>
????<artifactId>testApp-config-utils</artifactId>
????<name>testApp Config Utilities</name>
????<dependencies>
????????<dependency>
????????????<groupId>com.testApp2</groupId>
????????????<artifactId>testApp-util</artifactId>
????????????<scope>compile</scope>
????????</dependency>
????????<dependency>
????????????<groupId>junit</groupId>
????????????<artifactId>junit</artifactId>
????????????<version>3.8.1</version>
????????????<scope>test</scope>
????????</dependency>
????</dependencies>
</project> 4, Maven編譯命令 創(chuàng)建好項目和配置文件后,我們就可以進(jìn)入pom.xml文件所在目錄執(zhí)行以下命令進(jìn)行編譯工作: --mvn test:運行應(yīng)用程序中的單元測試
--mvn package:依據(jù)項目生成jar文件
--mvn install:將項目的jar文件添加到庫中,以備依賴此項目時使用
--mvn site:生成項目相關(guān)信息的網(wǎng)站
--mvn clean:清除目標(biāo)目錄中的生成結(jié)果
--mvn eclipse:eclipse:生成Eclipse項目文件 5, 有用的一些Tips 5.1 忽略失敗的單元測試,讓編譯繼續(xù)執(zhí)行: <plugin>????????????????????
??<groupId> org.apache.maven.plugins </groupId>????????
??<artifactId> maven-surefire-plugin </artifactId>????
??<configuration>????
????<testFailureIgnore> true </testFailureIgnore>????
??</configuration>????????????
</plugin> 5.2 不執(zhí)行單元測試 <plugin>????????????????????
??<groupId> org.apache.maven.plugins </groupId>????
??<artifactId> maven-surefire-plugin </artifactId>????
??<configuration>????
????<skip> true </skip>????
??</configuration>????
</plugin> 5.3 配置存儲庫 要求項目的每個開發(fā)者必須在conf目錄中配置存儲庫是不方便的,所以Maven可以同時查看多個存儲庫并且將它們?nèi)颗渲迷趐om.xml文件中。
<repositories>
????????<repository>
????????????????????<id>Ibiblio</id>
????????????????????<name>Ibiblio</name>
????????????????????<url>http://www.ibiblio.org/maven/</url>
????????</repository>
????????<repository>
????????????????????<id>PlanetMirror</id>
????????????????????<name>Planet Mirror</name>
????????????????????<url>http://public.planetmirror.com/pub/maven/</url>
????????</repository>
</repositories>
轉(zhuǎn)載于:https://blog.51cto.com/babyhe/716869
總結(jié)
以上是生活随笔為你收集整理的Maven项目编译工具的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浅谈工业4.0时代,深信服adesk桌面
- 下一篇: Gitlab的develop角色的人没有