Maven运用(二)用Maven创建一个简单的项目

1、Maven Archetype Plugin

先来看一个最简单的创建Java项目的命令:

mvn archetype:create
   -DgroupId=[your project's group id]
   -DartifactId=[your project's artifact id]

archetype:create称为一个Maven目标 (goal)。而像-Dname=value这样的对是将会被传到目标中的参数,它们使用-D属性这样的形式,类似于你通过命令行向Java虚拟机传递系统属性。archetype:create这个目标的目的通过archetype快速创建一个项目。在这里,一个archetype被定义为“一个原始的模型或者类型,在它之后其它类似的东西与之匹配”。Maven有许多可用的archetype,从生成一个简单的Swing应用,到一个复杂的Web应用。

 groupId:
定义了项目属于哪个组,这个组往往和项目所在的组织或公司存在关联,它以创建这个项目的组织名称的逆向域名(reverse domain name)开头。
artifactId:
定义了当前Maven项目在groupId中的唯一ID,一般跟项目的名称一致,如项目名称为:myproject,则artifactId为myproject。

The archetype plugin has four goals for direct use:

 

and three goals bound to default lifecycle by 'maven-archetype' packaging:

  • archetype:jar (bound to the package phase) is used to build the archetype jar artifact.
  • archetype:integration-test (bound to the integration-test phase) is used to execute archetype integration tests by generating sample projects from the just built archetype.
  • archetype:update-local-catalog (bound to the install phase) is used to update the local catalog.

2、Maven项目的目录结构

maven是遵循"约定优于配置",默认目录结构如下:

  • pom.xml               maven的配置文件,位于项目的根目录下
  • /src/main/java        项目主代码目录
  • /src/main/resource    项目主资源目录
  • /src/main/webapp      项目Web资源目录(必须包含WEB-INF,还有jsp、css、JavaScript等文件)
  • /src/test/java        项目测试代码目录
  • /src/test/resource    项目测试资源目录
  • /target               输出目录,所有的输出都存放在这个目录
  • /target/classes       编译之后,主代码class文件目录
  • /target/test-classes  编译之后,测试代码class文件目录

3、创建一个简单的Maven项目

在cmd中运行如下命令:

mvn archetype:create -DgroupId=com.zheng.mavenExample -DartifactId=firstMavenProject



 

maven的大致结构如下:




 在项目的根目录下,会有一个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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.zheng.mavenExample</groupId>
  <artifactId>firstMavenProject</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>firstMavenProject</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

上面代码中最重要的是groupId,artifactId和version三行。这三个元素定义了一个项目基本的坐标,在Maven的世界,任何的jar、pom、war构件,都是以基于这些基本的坐标进行区分的,每个maven项目,必需要声明groupId、artifactId、version,不能同时拥有一个具有相同groupId、artifactId、version标识的项目。

groupId:
定义了项目属于哪个组,这个组往往和项目所在的组织或公司存在关联,它以创建这个项目的组织名称的逆向域名(reverse domain name)开头。
artifactId:
定义了当前Maven项目在groupId中的唯一ID,一般跟项目的名称一致,如项目名称为:myproject,则artifactId为myproject。
version:
指定了maven项目当前的版本,SNAPSHOT意为快照,说明该项目还处于开发中,是不稳定的版本。随着项目的发展,version会不断更新,如升级为1.0、1.1-SNAPSHOT、1.1、2.0等等。
packaging:
项目的打包类型,默认是jar,描述了项目打包后的输出,其值可以为jar、war、pom。
name:
声明了一个对于用户更为友好的项目名称,虽然这不是必须的,但还是推荐为每个POM声明name,以方便信息交流。
 properties:

元素定义了一个maven属性,然后在pom的其他地方使用${属性名}的方式引用该属性值,这样做的意义在于消除重复。

dependencies:

<dependencies>元素下可以包含多个dependency元素以声明项目的依赖,这里我们添加了一个依赖,groupId是junit,artifactId是junit,version是3.8.1。前面我们提到groupId、artifactId和version是任何一个Maven项目最基本的坐标,有了这段声明,Maven就能够自动访问中央仓库(http://repo.maven.apache.org/maven2/),根据groupId、artifactId、version生成下载地址:http://repo.maven.apache.org/maven2/junit/junit/3.8.1/,下载junit-3.8.1.jar构件,第一个junit是groupId,第二个junit是artifactId,第三个是版本号。

4、Maven 常用命令:

在项目所在路径的命令行终端,执行:

mvn compile


Maven开始下载依赖包,然后编译,最后提示“BUILD SUCCESS"证明编译成功。在项目mvnlearn目录下面生成target目录,编译好的字节码class问件就在这个目录里面。

 

 

5、Maven 常用命令:

ü  mvn  archetype:create -DgroupId=com.huawei.bme -DartifactId=test –Dversion=1.0

创建普通应用项目

ü  mvn archetype:create -DgroupId=com.huawei.bme -DartifactId=testWeb -DarchetypeArtifactId=maven-archetype-webapp

创建WEB项目

ü  mvn eclipse:eclipse

生成 Eclipse 项目文件及包引用定义,注意,需事先定义Classpath Variables: M2_REPO,指向本地maven类库目录。

ü  mvn compile

编译主程序源代码,不会编译test目录的源代码。第一次运行时,会下载相关的依赖包,可能会比较费时。

ü  mvn test-compile

编译测试代码,compile之后会生成target文件夹,主程序编译在classes下面,测试程序放在test-classes下。

ü  mvn test

运行应用程序中的单元测试

ü  mvn site

生成项目相关信息的网站,发布站点在target/site里。

ü  mvn clean

清除目标目录(target)中的生成结果。

ü  mvn package

依据项目,打包类型为jar的,在目录target中就会生成 jar 文件,打包类型为war的,生成 war 文件,打包之前会进行编译,测试。

ü  mvn install

在本地 Repository 中安装 jar

ü  mvn deploy

在远程 Repository 中发布。

ü  mvn install -DskipTests=true

忽略单元测试,在本地 Repository 中安装 jar

ü  mvn eclipse:clean

清除 Eclipse 项目文件及包引用定义

ü  mvn install:install -file -Dfile=D:/opensource/dbunit-2.2/dbunit-2.2.jar -DgroupId=dbunit-DartifactId=junitperf -Dversion=2.2 -Dpackaging=jar

发布到本地仓库

ü  mvn deploy:deploy -file -DgroupId=staticComparison -DartifactId=static-comparison -Dversion=2.0 -Dpackaging=jar -Dfile=c:/staticComparison_2.0.jar

发布到远程仓库

ü  mvn help:describe -Dplugin=compiler -Dmojo=compile –Dfull

列出Compiler 插件的compile 目标的所有信息

ü  pom: mvn help:effective-pom

 查看实际使用的pom

ü  mvn dependency:analyze

分析项目依赖

1、Maven Archetype Plugin 先来看一个最简单的创建Java项目的命令:
mvn archetype:create
   -DgroupId=[your project's group id]
   -DartifactId=[your project's artifact id]

猜你喜欢

转载自agileshell.iteye.com/blog/1837741