一、Maven安装与配置
1、下载安装maven
去 Maven下载主页 下载 Maven 3.0.4
apache-maven-3.0.4-bin.tar.gz 解压到 D:/apache-maven-3.0.4,并在环境变量加入M2_HOME=D:/apache-maven-3.0.4,PATH中加入bin路径:%M2_HOME%\bin。
确认安装:cmd中输入:mvn -version ,看到版本信息说明配置成功。
Apache Maven 3.0.4 (r1232337; 2012-01-17 16:44:56+0800)
Maven home: D:\apache-maven-3.0.4
Java version: 1.6.0_18, vendor: Sun Microsystems Inc.
Java home: C:\Program Files\Java\jdk1.6.0_18\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows xp", version: "5.1", arch: "x86", family: "windows"
更新maven:下载新的版本,然后将环境变量的M2_HOME修改为新版的路径即可。
2、配置maven
执行 mvn help:system
该命令会打印出所有的Java系统属性和环境变量,这些信息对我们日常的编程工作很有帮助。运行这条命令的目的是为了让Maven执行一个真正的任务。我们可以从命令行输出看到Maven会下载maven-help-plugin,包括pom文件和jar文件。这些文件都被下载到了Maven本地仓库中。我的下载的路径是在:C:\Documents and Settings\Administrator\.m2下。
常用命令
Maven2 的运行命令为 : mvn
常用命令为 :
mvn archetype:create :创建 Maven 项目
mvn compile :编译源代码
mvn test-compile :编译测试代码
mvn test : 运行应用程序中的单元测试
mvn site : 生成项目相关信息的网站
mvn clean :清除目标目录中的生成结果
mvn package : 依据项目生成 jar 文件
mvn install :在本地 Repository 中安装 jar
mvn eclipse:eclipse :生成 Eclipse 项目文件
生成项目
建一个 JAVA 项目 : mvn archetype:create -DgroupId=com.demo -DartifactId=App
建一个 web 项目 : mvn archetype:create -DgroupId=com.demo -DartifactId=web-app -DarchetypeArtifactId=maven-archetype-webapp
pom.xml 文件基本节点介绍
:文件的根节点 .
: pom.xml 使用的对象模型版本 .
:创建项目的组织或团体的唯一 Id.
:项目的唯一 Id, 可视为项目名 .
:打包物的扩展名,一般有 JAR,WAR,EAR 等
:产品的版本号 .
:项目的显示名,常用于 Maven 生成的文档。
:组织的站点,常用于 Maven 生成的文档。
:项目的描述,常用于 Maven 生成的文档。
Maven的项目文件的主要配置如下
<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>web_server</groupId>
<artifactId>web_server</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>webchat_server</name>
<url>http://maven.apache.org</url>
<properties>
<org.springframework.version>3.0.7.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-rmi</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>org.nutz</groupId>
<artifactId>nutz</artifactId>
<version>1.b.45</version>
</dependency>
<dependency>
<groupId>platform</groupId>
<artifactId>platform-server</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>E:/myworkspace/maven_webchat_server/lib/platform-server.jar</systemPath>
</dependency>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
<scope>system</scope>
<systemPath>E:/myworkspace/maven_webchat_server/lib/asm-3.3.1.jar</systemPath>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.cedarsoft.commons</groupId>
<artifactId>commons</artifactId>
<version>5.1.3</version>
</dependency>
<dependency>
<groupId>commons</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
<scope>system</scope>
<systemPath>E:/myworkspace/maven_webchat_server/lib/commons-dbcp-1.4.jar</systemPath>
</dependency>
<dependency>
<groupId>commons</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
<scope>system</scope>
<systemPath>E:/myworkspace/maven_webchat_server/lib/commons-lang-2.5.jar</systemPath>
</dependency>
<dependency>
<groupId>commons</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
<scope>system</scope>
<systemPath>E:/myworkspace/maven_webchat_server/lib/commons-logging-1.1.1.jar</systemPath>
</dependency>
<dependency>
<groupId>commons</groupId>
<artifactId>commons-pool</artifactId>
<version>1.5.4</version>
<scope>system</scope>
<systemPath>E:/myworkspace/maven_webchat_server/lib/commons-pool-1.5.4.jar</systemPath>
</dependency>
<dependency>
<groupId>fastjson</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.22</version>
<scope>system</scope>
<systemPath>E:/myworkspace/maven_webchat_server/lib/fastjson-1.1.22.jar</systemPath>
</dependency>
<dependency>
<groupId>hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>2.0.3</version>
<scope>system</scope>
<systemPath>E:/myworkspace/maven_webchat_server/lib/hazelcast-2.0.3.jar</systemPath>
</dependency>
<dependency>
<groupId>hazelcast</groupId>
<artifactId>hazelcast-client</artifactId>
<version>2.0.3</version>
<scope>system</scope>
<systemPath>E:/myworkspace/maven_webchat_server/lib/hazelcast-client-2.0.3.jar</systemPath>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>system</scope>
<systemPath>E:/myworkspace/maven_webchat_server/lib/log4j-1.2.16.jar</systemPath>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
<scope>system</scope>
<systemPath>E:/myworkspace/maven_webchat_server/lib/slf4j-api-1.6.1.jar</systemPath>
</dependency>
<dependency>
<groupId>slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
<scope>system</scope>
<systemPath>E:/myworkspace/maven_webchat_server/lib/slf4j-log4j12-1.6.6.jar</systemPath>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<outputDirectory>target/bin</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.guagua.server.CreatePageStart</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>target/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>