1. 程式人生 > >Maven 操作指南

Maven 操作指南

println 依賴 class 文件的 順序 控制 spring mls rac

1.解壓部署Maven核心程序
①檢查JAVA_HOME環境變量
C:\Windows\System32>echo %JAVA_HOME%
D:\DevInstall\jdk1.7.0_07
②解壓Maven的核心程序
將apache-maven-3.2.2-bin.zip解壓到一個非中文無空格的目錄下。例如:D:\DevInstall\apache-maven-3.2.2
③配置環境變量
M2_HOME D:\DevInstall\apache-maven-3.2.2
path D:\DevInstall\apache-maven-3.2.2\bin
④查看Maven版本信息驗證安裝是否正確
C:\Windows\System32>mvn -v
Apache Maven 3.2.2 (45f7c06d68e745d05611f7fd14efb6594181933e; 2014-06-17T21:51:42+08:00)
Maven home: D:\DevInstall\apache-maven-3.2.2\bin\..
Java version: 1.7.0_07, vendor: Oracle Corporation
Java home: D:\DevInstall\jdk1.7.0_07\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
2.修改本地倉庫
①默認本地倉庫位置:~\.m2\repository
~表示當前用戶的家目錄,例如:C:\Users\[你當前登錄系統的用戶名]
②指定本地倉庫位置的配置信息文件:apache-maven-3.2.2\conf\settings.xml
③在根標簽settings下添加如下內容:<localRepository>[本地倉庫路徑,也就是RepMaven.zip的解壓目錄]</localRepository>
3.第一個Maven工程
①目錄結構
Hello
|---src
|---|---main
|---|---|---java
|---|---|---resources
|---|---test
|---|---|---java
|---|---|---resources
|---pom.xml

②POM文件內容
<?xml version="1.0" ?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.atguigu.maven</groupId>
<artifactId>Hello</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>Hello</name>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
③編寫主程序代碼
在src/main/java/com/atguigu/maven目錄下新建文件Hello.java,內容如下
package com.atguigu.maven;
public class Hello {
public String sayHello(String name){
return "Hello "+name+"!";
}
}
④編寫測試代碼
在/src/test/java/com/atguigu/maven目錄下新建測試文件HelloTest.java
package com.atguigu.maven;
import org.junit.Test;
import static junit.framework.Assert.*;
public class HelloTest {
@Test
public void testHello(){
Hello hello = new Hello();
String results = hello.sayHello("litingwei");
assertEquals("Hello litingwei!",results);
}
}
⑤運行幾個基本的Maven命令
mvn compile 編譯
mvn clean 清理
mvn test 測試
mvn package 打包
※註意:運行Maven命令時一定要進入pom.xml文件所在的目錄!
4.第二個Maven工程
①工程名:HelloFriend
②目錄結構與第一個Maven工程相同
③POM文件
<?xml version="1.0" ?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atguigu.maven</groupId>
<artifactId>HelloFriend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>HelloFriend</name>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.atguigu.maven</groupId>
<artifactId>Hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>

</dependencies>
</project>
④主程序:在src/main/java/com/atguigu/maven目錄下新建文件HelloFriend.java
package com.atguigu.maven;
import com.atguigu.maven.Hello;
public class HelloFriend {
public String sayHelloToFriend(String name){
Hello hello = new Hello();
String str = hello.sayHello(name)+" I am "+this.getMyName();
System.out.println(str);
return str;
}
public String getMyName(){
return "John";
}
}
⑤測試程序:在/src/test/java/com/atguigu/maven目錄下新建測試文件HelloFriendTest.java
package com.atguigu.maven;
import static junit.framework.Assert.assertEquals;
import org.junit.Test;
import com.atguigu.maven.Hello;

public class HelloFriendTest {
@Test
public void testHelloFriend(){
HelloFriend helloFriend = new HelloFriend();
String results = helloFriend.sayHelloToFriend("litingwei");
assertEquals("Hello litingwei! I am John",results);
}
}
⑥運行Maven命令
mvn install 安裝
5.第三個Maven工程
①設置通過Maven創建的工程的JDK版本——一勞永逸
[1]打開settings.xml文件
[2]找到profiles標簽
[3]加入如下配置
<profile>
<id>jdk-1.7</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.7</jdk>
</activation>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
</properties>
</profile>
②工程坐標
groupId:com.atguigu.maven
ArtifactId:MakeFriends
Package:com.atguigu.maven
③在src/main/java中新建類com.atguigu.maven.MakeFriends
public String makeFriends(String name){
HelloFriend friend = new HelloFriend();
friend.sayHelloToFriend("litingwei");
String str = "Hey,"+friend.getMyName()+" make a friend please.";
System.out.println(str);
return str;
}
④在src/test/java中新建類com.atguigu.maven.MakeFriendsTest
package com.atguigu.maven;
import static junit.framework.Assert.assertEquals;
import org.junit.Test;
public class MakeFriendsTest {
@Test
public void testMakeFriends(){
MakeFriends makeFriend = new MakeFriends();
String str = makeFriend.makeFriends("litingwei");
assertEquals("Hey,John make a friend please.",str);
}
}
⑤添加依賴信息
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.atguigu.maven</groupId>
<artifactId>HelloFriend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
⑥在Eclipse環境下執行Maven命令:右擊pom.xml選擇run as 中的命令執行即可
6.測試依賴的範圍對傳遞性的影響
①在Hello中添加對spring-core的依賴
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.0.RELEASE</version>
<scope>compile</scope>
</dependency>
②在HelloFriend中查看spring-core是否被加入了運行時環境
③將Hello中對spring-core的依賴範圍修改為test,再到HelloFriend中檢查
④將Hello中對spring-core的依賴範圍修改為provided,再到HelloFriend中檢查
⑤結論:非compile範圍的依賴不能傳遞,必須在有需要的工程中單獨加入
7.測試依賴原則
①路徑最短者優先
[1]在Hello中依賴log4j-1.2.17
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

[2]在HelloFriend中依賴log4j-1.2.14
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
[3]查看MakeFriends中自動引入的log4j是哪個版本

②路徑相同時先聲明者優先
[1]創建OurFriends工程,依賴log4j-1.2.17
[2]讓MakeFriends依賴OurFriends
[3]測試MakeFriends中,HelloFriend和OurFriends依賴的先後順序和引入的log4j版本之間的關系
8.創建Web工程
①ServletAPI依賴
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
②JSPAPI依賴
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1.3-b06</version>
<scope>provided</scope>
</dependency>
9.Web工程自動部署
<build>
<finalName>AtguiguWeb</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.2.3</version>
<configuration>
<container>
<containerId>tomcat6x</containerId>
<home>D:\DevInstall\apache-tomcat-6.0.39</home>
</container>
<configuration>
<type>existing</type>
<home>D:\DevInstall\apache-tomcat-6.0.39</home>
<!-- 如果Tomcat端口為默認值8080則不必設置該屬性 -->
<properties>
<cargo.servlet.port>8989</cargo.servlet.port>
</properties>
</configuration>
</configuration>
<executions>
<execution>
<id>cargo-run</id>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
10.繼承
①創建Parent工程,打包方式為pom
②收集所有非compile範圍的依賴信息,使用dependencyManagement標簽統一管理
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
③在各個子工程中引用父工程
<parent>
<groupId>com.atguigu.maven</groupId>
<artifactId>Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>

<!-- 以當前文件為基準查找父工程中pom.xml文件的相對路徑 -->
<relativePath>../Parent/pom.xml</relativePath>
</parent>
④刪除子工程中的重復信息
groupId
artifactid
⑤在子工程中找到被父工程管理的依賴信息,刪除版本號部分
⑥在父工程中統一修改已管理的依賴信息的版本號,看是否能夠控制所有子工程
11.聚合
在總的聚合工程中加入如下信息
<modules>
<module>../Hello</module>
<module>../HelloFriend</module>
<module>../MakeFriends</module>
</modules>

Maven 操作指南