一.常用配置
1.安裝並替換Eclipse中的插件
preferences -> maven ->Installions->add 已經安裝好的mavenpreferences -> maven ->User Settings->Globle Settings,User Settings maven文件夾下的setting.xml
爲防止eclipse運行時找不到maven工作目錄,需要在選擇的jre中配置 -Dmaven.multiModuleProjectDirectory=$M2_HOME
2.maven 本地倉庫配置
在setting.xml中<localRepository>d:\m2\repo</localRepository> Default: ${user.home}/.m2/repository3.遠程倉庫鏡像
全局配置:修改setting.xml文件中的mirrors節點 。url指向遠程倉庫的地址。 這樣設置之後你的所有項目都有作用。<mirrors>
<mirror>
<id>central_mirror</id>
<name>internal central_mirror epository</name>
<url>http://repo1.maven.org/maven2/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
單一工程修改:在pom.xml文件中加入
<repositories>
<repository>
<id>oschinaRepository</id>
<name>local private nexus</name>
<url>http://maven.oschina.net/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
4.配置網絡代理
setting.xml文件中的proxy節點<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<host>xxx.com.cn</host>
<port>xxx</port>
<nonProxyHosts>11.xxx.xxx.xxx</nonProxyHosts>
</proxy>
5.指定jdk版本
全局配置 setting.xml<profiles>
<profile>
<id>jdk18</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
局部配置 pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
二.基本概念
1.maven座標,唯一標示(GAV)
pom.xml<groupId>com.companyname.project-group</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
2.三套生命週期
1)Clean Lifecycle 在進行真正的構建之前進行一些清理工作。clean
2)Default Lifecycle 構建的核心部分,編譯,測試,打包,部署等等。
compile->test->package->install
3)Site Lifecycle 生成項目報告,站點,發佈站點。
site
3.profile
src/main/resources 目錄下有兩套環境配置文件--serverConfig-loc.properties 內容:profile=dev
--serverConfig-test.properties 內容:profile=test
--serverConfig.properties 內容:profile=${profile}
pom.xml中配置
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>serverConfig.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>loc</id>
<build>
<filters>
<filter>src/main/resources/serverConfig-loc.properties</filter>
</filters>
</build>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<build>
<filters>
<filter>src/main/resources/serverConfig-test.properties</filter>
</filters>
</build>
</profile>
</profiles>
創建Demo.class
public class Demo {
public static void say() throws Exception {
String path = "serverConfig";
ResourceBundle resource = ResourceBundle.getBundle(path);
String profile = resource.getString("profile");
System.out.println(profile);
}
}
mvn clean package install 發佈到本地環境Demo.say()結果:loc
mvn clean package -Ptest install 發佈到測試環境 Demo.say()結果:test
4.模塊(module):高內聚,低耦合
例子:---- app-parent
|-- pom.xml (pom)
|
|-- app-util
| |-- pom.xml (jar)
|
|-- app-dao
| |-- pom.xml (jar)
|
|-- app-service
| |-- pom.xml (jar)
|
|-- app-web
|-- pom.xml (war)
這些模塊的依賴關係如下:(在Maven build app-parent的時候,它會根據子模塊的相互依賴關係整理一個build順序,然後依次build。)
app-dao --> app-util
app-service --> app-dao
app-web --> app-service
app-parent的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.myorg.myapp</groupId>
<artifactId>app-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>app-util</module>
<module>app-dao</module>
<module>app-service</module>
<module>app-web</module>
</modules>
</project>
app-util的pom.xml(app-util模塊繼承了app-parent父模塊,因此這個POM的一開始就聲明瞭對app-parent的引用,該引用是通過Maven座標GAV實現的。
而關於項目app-util本身,它卻沒有聲明完整GAV,這裏我們只看到了artifactId。這個POM並沒有錯,groupId和version默認從父模塊繼承了。
實際上子模塊從父模塊繼承一切東西,包括依賴,插件配置等等。)
<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>app-parent</artifactId>
<groupId>org.myorg.myapp</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>app-util</artifactId>
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
</project>
app-dao的pom.xml(app-dao依賴於app-util。這裏要注意的是version的值爲${project.version},這個值是一個屬性引用,指向了POM的project/version的值,也就是這個POM對應的version。由於app-dao的version繼承於app-parent,因此它的值就是1.0-SNAPSHOT。而app-util也繼承了這個值,因此在所有這些項目中,我們做到了保持版本一致。這裏還需要注意的是,app-dao依賴於app-util,而app-util又依賴於commons-lang,根據傳遞性,app-dao也擁有了對於commons-lang的依賴。)
<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>app-parent</artifactId>
<groupId>org.myorg.myapp</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>app-dao</artifactId>
<dependencies>
<dependency>
<groupId>org.myorg.myapp</groupId>
<artifactId>app-util</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
web.xml is missing and <failOnMissingWebXml> is set to true。
這時候需要右擊項目——>Java EE Tools——>Generate Deployment Descriptor Stub.然後系統會在src/main/webapp/WEB_INF文件加下創建web.xml文件
5.plugins
第一,編寫插件pom.xml文件;<groupId>com.maven</groupId>
<artifactId>myPlugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.0.3</version>
</dependency>
</dependencies>
第二,編寫真正做事情的插件Java類。
/**
* @goal info
* @phase compile
* @requiresProject false
*/
public class HelloWorldMojo extends AbstractMojo {
/**
* @parameter expression="${name}"
* @required
*/
String name;
public void execute() throws MojoExecutionException, MojoFailureException {
System.out.println("hello " + name);
}
}
然後install,執行mvn com.maven:myPlugin:info -Dname=tim 然後可以看到控制檯輸出 hello tim。
擴展:通過自定義插件可以一個指令快速搭建項目結構。
源碼鏈接:https://github.com/wangjianyangchn/maven
參考:http://juvenshun.iteye.com/blog,http://baike.baidu.com/view/336103.htm,http://maven.apache.org/guides/index.html