|
| 1 | +1.��ѹ����Maven���ij��� |
| 2 | + �ټ��JAVA_HOME�������� |
| 3 | + C:\Windows\System32>echo %JAVA_HOME% |
| 4 | + D:\DevInstall\jdk1.7.0_07 |
| 5 | + �ڽ�ѹMaven�ĺ��ij��� |
| 6 | + ��apache-maven-3.2.2-bin.zip��ѹ��һ���������ո��Ŀ¼�¡����磺D:\DevInstall\apache-maven-3.2.2 |
| 7 | + �����û������� |
| 8 | + M2_HOME D:\DevInstall\apache-maven-3.2.2 |
| 9 | + path D:\DevInstall\apache-maven-3.2.2\bin |
| 10 | + �ܲ鿴Maven�汾��Ϣ��֤��װ�Ƿ���ȷ |
| 11 | + C:\Windows\System32>mvn -v |
| 12 | + Apache Maven 3.2.2 (45f7c06d68e745d05611f7fd14efb6594181933e; 2014-06-17T21:51:42+08:00) |
| 13 | + Maven home: D:\DevInstall\apache-maven-3.2.2\bin\.. |
| 14 | + Java version: 1.7.0_07, vendor: Oracle Corporation |
| 15 | + Java home: D:\DevInstall\jdk1.7.0_07\jre |
| 16 | + Default locale: zh_CN, platform encoding: GBK |
| 17 | + OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows" |
| 18 | +2.�ı��زֿ� |
| 19 | + ��Ĭ�ϱ��زֿ�λ�ã�~\.m2\repository |
| 20 | + ~��ʾ��ǰ�û��ļ�Ŀ¼�����磺C:\Users\[�㵱ǰ��¼ϵͳ���û���] |
| 21 | + ��ָ�����زֿ�λ�õ�������Ϣ�ļ���apache-maven-3.2.2\conf\settings.xml |
| 22 | + ���ڸ���ǩsettings�������������ݣ�<localRepository>[���زֿ�·����Ҳ����RepMaven.zip�Ľ�ѹĿ¼]</localRepository> |
| 23 | +3.��һ��Maven���� |
| 24 | + ��Ŀ¼�ṹ |
| 25 | + Hello |
| 26 | + |---src |
| 27 | + |---|---main |
| 28 | + |---|---|---java |
| 29 | + |---|---|---resources |
| 30 | + |---|---test |
| 31 | + |---|---|---java |
| 32 | + |---|---|---resources |
| 33 | + |---pom.xml |
| 34 | + |
| 35 | + ��POM����� |
| 36 | +<?xml version="1.0" ?> |
| 37 | +<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"> |
| 38 | + <modelVersion>4.0.0</modelVersion> |
| 39 | + |
| 40 | + <groupId>com.atguigu.maven</groupId> |
| 41 | + <artifactId>Hello</artifactId> |
| 42 | + <version>0.0.1-SNAPSHOT</version> |
| 43 | + |
| 44 | + <name>Hello</name> |
| 45 | + |
| 46 | + <dependencies> |
| 47 | + <dependency> |
| 48 | + <groupId>junit</groupId> |
| 49 | + <artifactId>junit</artifactId> |
| 50 | + <version>4.0</version> |
| 51 | + <scope>test</scope> |
| 52 | + </dependency> |
| 53 | + </dependencies> |
| 54 | +</project> |
| 55 | + �۱�д��������� |
| 56 | + ��src/main/java/com/atguigu/mavenĿ¼���½��ļ�Hello.java���������� |
| 57 | + package com.atguigu.maven; |
| 58 | + public class Hello { |
| 59 | + public String sayHello(String name){ |
| 60 | + return "Hello "+name+"!"; |
| 61 | + } |
| 62 | + } |
| 63 | + �ܱ�д���Դ��� |
| 64 | + ��/src/test/java/com/atguigu/mavenĿ¼���½������ļ�HelloTest.java |
| 65 | + package com.atguigu.maven; |
| 66 | + import org.junit.Test; |
| 67 | + import static junit.framework.Assert.*; |
| 68 | + public class HelloTest { |
| 69 | + @Test |
| 70 | + public void testHello(){ |
| 71 | + Hello hello = new Hello(); |
| 72 | + String results = hello.sayHello("litingwei"); |
| 73 | + assertEquals("Hello litingwei!",results); |
| 74 | + } |
| 75 | + } |
| 76 | + �������������Maven���� |
| 77 | + mvn compile ���� |
| 78 | + mvn clean ���� |
| 79 | + mvn test ���� |
| 80 | + mvn package ��� |
| 81 | + ��ע�⣺����Maven����ʱһ��Ҫ����pom.xml�ļ����ڵ�Ŀ¼�� |
| 82 | +4.�ڶ���Maven���� |
| 83 | + �ٹ�������HelloFriend |
| 84 | + ��Ŀ¼�ṹ���һ��Maven������ͬ |
| 85 | + ��POM�ļ� |
| 86 | +<?xml version="1.0" ?> |
| 87 | +<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"> |
| 88 | + <modelVersion>4.0.0</modelVersion> |
| 89 | + <groupId>com.atguigu.maven</groupId> |
| 90 | + <artifactId>HelloFriend</artifactId> |
| 91 | + <version>0.0.1-SNAPSHOT</version> |
| 92 | + <name>HelloFriend</name> |
| 93 | + |
| 94 | + <dependencies> |
| 95 | + <dependency> |
| 96 | + <groupId>junit</groupId> |
| 97 | + <artifactId>junit</artifactId> |
| 98 | + <version>4.0</version> |
| 99 | + <scope>test</scope> |
| 100 | + </dependency> |
| 101 | + |
| 102 | + <dependency> |
| 103 | + <groupId>com.atguigu.maven</groupId> |
| 104 | + <artifactId>Hello</artifactId> |
| 105 | + <version>0.0.1-SNAPSHOT</version> |
| 106 | + <scope>compile</scope> |
| 107 | + </dependency> |
| 108 | + |
| 109 | + </dependencies> |
| 110 | +</project> |
| 111 | + ����������src/main/java/com/atguigu/mavenĿ¼���½��ļ�HelloFriend.java |
| 112 | + package com.atguigu.maven; |
| 113 | + import com.atguigu.maven.Hello; |
| 114 | + public class HelloFriend { |
| 115 | + public String sayHelloToFriend(String name){ |
| 116 | + Hello hello = new Hello(); |
| 117 | + String str = hello.sayHello(name)+" I am "+this.getMyName(); |
| 118 | + System.out.println(str); |
| 119 | + return str; |
| 120 | + } |
| 121 | + public String getMyName(){ |
| 122 | + return "John"; |
| 123 | + } |
| 124 | + } |
| 125 | + �ݲ��Գ�����/src/test/java/com/atguigu/mavenĿ¼���½������ļ�HelloFriendTest.java |
| 126 | + package com.atguigu.maven; |
| 127 | + import static junit.framework.Assert.assertEquals; |
| 128 | + import org.junit.Test; |
| 129 | + import com.atguigu.maven.Hello; |
| 130 | + |
| 131 | + public class HelloFriendTest { |
| 132 | + @Test |
| 133 | + public void testHelloFriend(){ |
| 134 | + HelloFriend helloFriend = new HelloFriend(); |
| 135 | + String results = helloFriend.sayHelloToFriend("litingwei"); |
| 136 | + assertEquals("Hello litingwei! I am John",results); |
| 137 | + } |
| 138 | + } |
| 139 | + ������Maven���� |
| 140 | + mvn install ��װ |
| 141 | +5.������Maven���� |
| 142 | + ������ͨ��Maven�����Ĺ��̵�JDK�汾����һ������ |
| 143 | + [1]��settings.xml�ļ� |
| 144 | + [2]�ҵ�profiles��ǩ |
| 145 | + [3]������������ |
| 146 | + <profile> |
| 147 | + <id>jdk-1.7</id> |
| 148 | + <activation> |
| 149 | + <activeByDefault>true</activeByDefault> |
| 150 | + <jdk>1.7</jdk> |
| 151 | + </activation> |
| 152 | + <properties> |
| 153 | + <maven.compiler.source>1.7</maven.compiler.source> |
| 154 | + <maven.compiler.target>1.7</maven.compiler.target> |
| 155 | + <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion> |
| 156 | + </properties> |
| 157 | + </profile> |
| 158 | + �ڹ������� |
| 159 | + groupId��com.atguigu.maven |
| 160 | + ArtifactId��MakeFriends |
| 161 | + Package��com.atguigu.maven |
| 162 | + ����src/main/java���½���com.atguigu.maven.MakeFriends |
| 163 | + public String makeFriends(String name){ |
| 164 | + HelloFriend friend = new HelloFriend(); |
| 165 | + friend.sayHelloToFriend("litingwei"); |
| 166 | + String str = "Hey,"+friend.getMyName()+" make a friend please."; |
| 167 | + System.out.println(str); |
| 168 | + return str; |
| 169 | + } |
| 170 | + ����src/test/java���½���com.atguigu.maven.MakeFriendsTest |
| 171 | + package com.atguigu.maven; |
| 172 | + import static junit.framework.Assert.assertEquals; |
| 173 | + import org.junit.Test; |
| 174 | + public class MakeFriendsTest { |
| 175 | + @Test |
| 176 | + public void testMakeFriends(){ |
| 177 | + MakeFriends makeFriend = new MakeFriends(); |
| 178 | + String str = makeFriend.makeFriends("litingwei"); |
| 179 | + assertEquals("Hey,John make a friend please.",str); |
| 180 | + } |
| 181 | + } |
| 182 | + ������������Ϣ |
| 183 | + <dependency> |
| 184 | + <groupId>junit</groupId> |
| 185 | + <artifactId>junit</artifactId> |
| 186 | + <version>4.9</version> |
| 187 | + <scope>test</scope> |
| 188 | + </dependency> |
| 189 | + <dependency> |
| 190 | + <groupId>com.atguigu.maven</groupId> |
| 191 | + <artifactId>HelloFriend</artifactId> |
| 192 | + <version>0.0.1-SNAPSHOT</version> |
| 193 | + <type>jar</type> |
| 194 | + <scope>compile</scope> |
| 195 | + </dependency> |
| 196 | + ����Eclipse������ִ��Maven����һ�pom.xmlѡ��run as �е�����ִ�м��� |
| 197 | +6.���������ķ�Χ�Դ����Ե�Ӱ�� |
| 198 | + ����Hello�����Ӷ�spring-core������ |
| 199 | + <dependency> |
| 200 | + <groupId>org.springframework</groupId> |
| 201 | + <artifactId>spring-core</artifactId> |
| 202 | + <version>4.0.0.RELEASE</version> |
| 203 | + <scope>compile</scope> |
| 204 | + </dependency> |
| 205 | + ����HelloFriend�в鿴spring-core�Ƿ���������ʱ���� |
| 206 | + �۽�Hello�ж�spring-core��������Χ��Ϊtest���ٵ�HelloFriend�м�� |
| 207 | + �ܽ�Hello�ж�spring-core��������Χ��Ϊprovided���ٵ�HelloFriend�м�� |
| 208 | + �ݽ��ۣ���compile��Χ���������ܴ��ݣ�����������Ҫ�Ĺ����е������� |
| 209 | +7.��������ԭ�� |
| 210 | + ��·����������� |
| 211 | + [1]��Hello������log4j-1.2.17 |
| 212 | + <dependency> |
| 213 | + <groupId>log4j</groupId> |
| 214 | + <artifactId>log4j</artifactId> |
| 215 | + <version>1.2.17</version> |
| 216 | + </dependency> |
| 217 | + |
| 218 | + [2]��HelloFriend������log4j-1.2.14 |
| 219 | + <dependency> |
| 220 | + <groupId>log4j</groupId> |
| 221 | + <artifactId>log4j</artifactId> |
| 222 | + <version>1.2.14</version> |
| 223 | + </dependency> |
| 224 | + [3]�鿴MakeFriends���Զ������log4j���ĸ��汾 |
| 225 | + |
| 226 | + ��·����ͬʱ������������ |
| 227 | + [1]����OurFriends���̣�����log4j-1.2.17 |
| 228 | + [2]��MakeFriends����OurFriends |
| 229 | + [3]����MakeFriends�У�HelloFriend��OurFriends�������Ⱥ�˳��������log4j�汾֮��Ĺ�ϵ |
| 230 | +8.����Web���� |
| 231 | + ��ServletAPI���� |
| 232 | + <dependency> |
| 233 | + <groupId>javax.servlet</groupId> |
| 234 | + <artifactId>servlet-api</artifactId> |
| 235 | + <version>2.5</version> |
| 236 | + <scope>provided</scope> |
| 237 | + </dependency> |
| 238 | + ��JSPAPI���� |
| 239 | + <dependency> |
| 240 | + <groupId>javax.servlet.jsp</groupId> |
| 241 | + <artifactId>jsp-api</artifactId> |
| 242 | + <version>2.1.3-b06</version> |
| 243 | + <scope>provided</scope> |
| 244 | + </dependency> |
| 245 | +9.Web�����Զ����� |
| 246 | + <build> |
| 247 | + <finalName>AtguiguWeb</finalName> |
| 248 | + <plugins> |
| 249 | + <plugin> |
| 250 | + <groupId>org.codehaus.cargo</groupId> |
| 251 | + <artifactId>cargo-maven2-plugin</artifactId> |
| 252 | + <version>1.2.3</version> |
| 253 | + <configuration> |
| 254 | + <container> |
| 255 | + <containerId>tomcat6x</containerId> |
| 256 | + <home>D:\DevInstall\apache-tomcat-6.0.39</home> |
| 257 | + </container> |
| 258 | + <configuration> |
| 259 | + <type>existing</type> |
| 260 | + <home>D:\DevInstall\apache-tomcat-6.0.39</home> |
| 261 | + <!-- ���Tomcat�˿�ΪĬ��ֵ8080�����ø����� --> |
| 262 | + <properties> |
| 263 | + <cargo.servlet.port>8989</cargo.servlet.port> |
| 264 | + </properties> |
| 265 | + </configuration> |
| 266 | + </configuration> |
| 267 | + <executions> |
| 268 | + <execution> |
| 269 | + <id>cargo-run</id> |
| 270 | + <phase>install</phase> |
| 271 | + <goals> |
| 272 | + <goal>run</goal> |
| 273 | + </goals> |
| 274 | + </execution> |
| 275 | + </executions> |
| 276 | + </plugin> |
| 277 | + </plugins> |
| 278 | + </build> |
| 279 | +10.�̳� |
| 280 | + �ٴ���Parent���̣������ʽΪpom |
| 281 | + ���ռ����з�compile��Χ��������Ϣ��ʹ��dependencyManagement��ǩͳһ���� |
| 282 | + <dependencyManagement> |
| 283 | + <dependencies> |
| 284 | + <dependency> |
| 285 | + <groupId>junit</groupId> |
| 286 | + <artifactId>junit</artifactId> |
| 287 | + <version>4.9</version> |
| 288 | + <scope>test</scope> |
| 289 | + </dependency> |
| 290 | + </dependencies> |
| 291 | + </dependencyManagement> |
| 292 | + ���ڸ����ӹ��������ø����� |
| 293 | + <parent> |
| 294 | + <groupId>com.atguigu.maven</groupId> |
| 295 | + <artifactId>Parent</artifactId> |
| 296 | + <version>0.0.1-SNAPSHOT</version> |
| 297 | + |
| 298 | + <!-- �Ե�ǰ�ļ�Ϊ�����Ҹ�������pom.xml�ļ������·�� --> |
| 299 | + <relativePath>../Parent/pom.xml</relativePath> |
| 300 | + </parent> |
| 301 | + ��ɾ���ӹ����е��ظ���Ϣ |
| 302 | + groupId |
| 303 | + artifactid |
| 304 | + �����ӹ������ҵ��������̹�����������Ϣ��ɾ���汾�Ų��� |
| 305 | + ���ڸ�������ͳһ���ѹ�����������Ϣ�İ汾�ţ����Ƿ��ܹ����������ӹ��� |
| 306 | +11.�ۺ� |
| 307 | + ���ܵľۺϹ����м���������Ϣ |
| 308 | + <modules> |
| 309 | + <module>../Hello</module> |
| 310 | + <module>../HelloFriend</module> |
| 311 | + <module>../MakeFriends</module> |
| 312 | + </modules> |