使用Maven构建项目

使用Maven构建项目

一、Maven的下载

Maven

1)M2_HOME:maven安装目录,d:\ apache-maven-2.0.8

2)M2: %M2_HOME%”\bin

3) MAVEN_OPTS:可选项, Xms256m -Xmx512m

4) 更新或创建 PATH: %M2%;%Path%

5) 如果你还没有创建JAVA_HOME,PATH,请创建它们

JAVE_HOME:JAVA安装目录,Path:%JAVA_HOME%”\bin

Java version: 1.6.0_02

OS name: “windows xp” version: “5.1” arch: “x86” Family: “windows”

二、创建项目

1)创建普通的应用,使用下面的命令:

mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app

2)创建WEB应用,使用下面的命令:

mvn archetype:create “

-DarchetypeGroupId=org.apache.maven.archetypes “

-DarchetypeArtifactId=maven-archetype-webapp “

-DgroupId=com.mycompany.app “

-DartifactId=my-webapp

简单解释一下:

* maven-archetype-archetype

* maven-archetype-j2ee-simple

* maven-archetype-mojo

* maven-archetype-portlet

* maven-archetype-profiles (currently under development)

* maven-archetype-quickstart

* maven-archetype-simple (currently under development)* maven-archetype-site

* maven-archetype-site-simple

* maven-archetype-webappgroupId artifactId 项目的名称,生成的项目目录也是这个名字,可修改

2)compile:编译项目源代码

3)test :对所有编译过的源代码进行单元测试,这些测试不需要代码被打包或发布

4)package把所有编译后的源代码和包打到一个包中,像jar包

11)jetty:run或tomcat:run运行web项目

12)eclipse:eclipse生成可导入eclipse的项目四、POM.xml介绍

pom作为项目对象模型。通过xml表示maven项目,使用pom.xml来实现。主要描述了项目:包括配置文件;开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的url,项目的依赖性,以及其他所有的项目相关因素。快速察看:<project> <modelVersion>4.0.0</modelVersion>

<!– The Basics –> <groupId>…</groupId> <artifactId>…</artifactId> <version>…</version> <packaging>…</packaging> <dependencies>…</dependencies> <parent>…</parent> <dependencyManagement>…</dependencyManagement> <modules>…</modules> <properties>…</properties>

<!– Build Settings –> <build>…</build> <reporting>…</reporting>

<!– More Project Information –> <name>…</name> <description>…</description> <url>…</url> <inceptionYear>…</inceptionYear> <licenses>…</licenses> <organization>…</organization> <developers>…</developers> <contributors>…</contributors>

<!– Environment Settings –> <issueManagement>…</issueManagement> <ciManagement>…</ciManagement> <mailingLists>…</mailingLists> <scm>…</scm> <prerequisites>…</prerequisites> <repositories>…</repositories> <pluginRepositories>…</pluginRepositories> <distributionManagement>…</distributionManagement> <profiles>…</profiles></project>

基本内容: POM包括了所有的项目信息。maven 相关:pom定义了最小的maven2元素,允许groupId,artifactId,version。所有需要的元素

groupId:项目或者组织的唯一标志,并且配置时生成的路径也是由此生成,如org.codehaus.mojo生成的相对路径为:/org/codehaus/mojo artifactId: 项目的通用名称 version:项目的版本 packaging: 打包的机制,如pom, jar, maven-plugin, ejb, war, ear, rar, par classifier: 分类POM关系:主要为依赖,继承,合成 依赖关系: <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.0</version> <type>jar</type> <scope>test</scope> <optional>true</optional> </dependency> … </dependencies>

groupId, artifactId, version:描述了依赖的项目唯一标志可以通过以下方式进行安装:

只需勇敢前行,梦想自会引路,有多远,走多远,把足迹连成生命线。

使用Maven构建项目

相关文章:

你感兴趣的文章:

标签云: