maven是一個項目自動化管理與構建工具,簡單地來講,我拿它幹兩件事:html
作本地的jar包管理java
用來打包apache
至於打成jar包仍是zip包,能夠在pom.xml文件中指定。eclipse
在eclipse中安裝maven插件後,新建maven工程,能夠發如今根目錄下有一個pom.xml。pom文件是整個項目的核心,能夠添加遠程倉庫地址:maven
<repositories> <repository> <id>maven repo</id> <url>http://mvnrepository.com/artifact/</url> </repository> </repositories>
我通常是用mvnrepository做爲中央repository。工具
也能夠指定項目build時encoding方式爲UTF-8,在本地下載jar包時同時下載source、javaDoc的jar包:oop
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <downloadSources>true</downloadSources> <downloadJavadocs>true</downloadJavadocs> </properties>
能夠添加jar包依賴:ui
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-core</artifactId> <version>1.2.1</version> </dependency> </dependencies>
能夠指定項目build方式:url
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <executions> <execution> <id>attach-sources</id> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
上面的build方式爲在項目打包時,同時也會打sourcejar包。我平時經常使用build方式都會採用assembly,會打一個純項目包與with-dependencies的包(即加上了全部的包依賴))。.net
maven提供了豐富的插件,能夠用以下的命令執行:
mvn <plugin-name>:<goal-name>
好比mvn eclipse:eclipse
,表示使用maven-eclipse-plugin的eclipse-goal生成eclipse項目。
下載依賴包的source與javadoc,有兩種方法:
方法一
可輸入下面兩條命令,參看這裏
mvn dependency:sources mvn dependency:resolve -Dclassifier=javadoc
方法二
在pom中添加
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <configuration> <downloadSources>true</downloadSources> <downloadJavadocs>true</downloadJavadocs> </configuration> </plugin> </plugins> </build>
而後輸入mvn eclipse:eclipse
,便可。
除了在pom.xml能夠指定中央repository的URL地址,其次也能夠在settting.xml指定中央repo的鏡像地址,好比這樣:
<mirrors> <mirror> <id>internal-repository</id> <name>Maven Repository Manager</name> <url>http://repo.mycompany.com/proxy</url> <mirrorOf>*</mirrorOf> </mirror> </mirrors>
注意
:setting.xml要放在~/.m2/
目錄下。
oschina提供maven鏡像,不再用擔憂下載慢的問題了。
<mirror> <id>nexus-osc</id> <mirrorOf>*</mirrorOf> <name>Nexus osc</name> <url>http://maven.oschina.net/content/groups/public/</url> </mirror> <mirror> <id>nexus-osc</id> <mirrorOf>central</mirrorOf> <name>Nexus osc</name> <url>http://maven.oschina.net/content/groups/public/</url> </mirror> <mirror> <id>nexus-osc-thirdparty</id> <mirrorOf>thirdparty</mirrorOf> <name>Nexus osc thirdparty</name> <url>http://maven.oschina.net/content/repositories/thirdparty/</url> </mirror> <profile> <id>jdk-1.4</id> <activation> <jdk>1.4</jdk> </activation> <repositories> <repository> <id>nexus</id> <name>local private nexus</name> <url>http://maven.oschina.net/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>nexus</id> <name>local private nexus</name> <url>http://maven.oschina.net/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile>