Maven快速入門
創(chuàng)建快速啟動項(xiàng)目
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app
編譯
mvn compile
測試
mvn test
如果只是編譯測試源文件,而不啟動測試:
mvn test-compile
打包
mvn package
安裝到本地Repository
mvn install
Maven會自動查找測試文件,尋找的模式為:
默認(rèn)包括的測試文件有:
默認(rèn)排除的測試文件有:
創(chuàng)建項(xiàng)目網(wǎng)站
mvn site
清理
mvn clean
為項(xiàng)目生成IntelliJ IDEA描述符,可以在一個(gè)已經(jīng)存在的IDEA項(xiàng)目上進(jìn)行,會更新設(shè)置而不是從零開始。
mvn idea:idea
如何使用插件
示例如下:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
如何在Jar中包含資源
把資源放置在${basedir}/src/main/resources目錄中即可。測試用例所需資源的路徑是${basedir}/src/test/resources。
如何過濾資源文件
有時(shí)候資源文件需要構(gòu)建時(shí)刻才能提供的值,為了達(dá)到這個(gè)目的,在資源文件中添加${<property name>}這樣的引用。這些屬性可以來自pom.xml,settings.xml,其它的屬性文件或是系統(tǒng)屬性。
按照如下方式修改pom.xml:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
按如下使用pom.xml中的值:
# application.properties
application.name=${pom.name}
application.version=${pom.version}
如果是使用其它的屬性文件:
<build>
<filters>
<filter>src/main/filters/filter.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
也可以把這些屬性寫在pom.xml中:
<properties>
<my.filter.value>hello</my.filter.value>
</properties>
同樣可以是Java的系統(tǒng)屬性,或是通過-D傳入命令行參數(shù)。
如何使用外部依賴
在pom.xml中的dependencies一節(jié)中列出了所需的全部外部依賴。為了定義外部依賴,需要定義至少4個(gè)內(nèi)容:groupId, artifactId, version, 和 scope。Scope可以是test,compile或runtime。Maven會自動從一個(gè)遠(yuǎn)程的Repository下載所需的依賴。
如何部署jar到自己的遠(yuǎn)程repository
在pom.xml中添加如下內(nèi)容:
<distributionManagement>
<repository>
<id>mycompany-repository</id>
<name>MyCompany Repository</name>
<url>scp://repository.mycompany.com/repository/maven2</url>
</repository>
</distributionManagement>
同樣在用戶的settings.xml中也要定義服務(wù)器:
<settings>
<servers>
<server>
<id>mycompany-repository</id>
<username>jvanzyl</username>
<!-- Default value is ~/.ssh/id_dsa -->
<privateKey>/path/to/identity</privateKey> (default is ~/.ssh/id_dsa)
<passphrase>my_key_passphrase</passphrase>
</server>
</servers>
</settings>