maven3-常用命令及pom引见(2)

maven3-常用命令及pom介绍(2)

1.常用命令

  •  1.1 快速创建命令:(通过maven项目骨架生产项目)
生成java项目:mvn archetype:generate -DarchetypeCatalog=internal -DarchetypeArtifactId=maven-archetype-quickstart
生成web项目:mvn archetype:generate -DarchetypeCatalog=internal -DarchetypeArtifactId=maven-archetype-webapp

  

     根据提示向导 填入groupId (项目组名) artifactId(项目唯一标示) version(版本号),这三个属性定义成maven的仓库中坐标

         maven3-常用命令及pom引见(2)

 

  •    1.2 生命周期运行需要命令
mvn  clean :清除目标目录中的生成结果
mvn  compile :编译源代码             
mvn  test : 运行应用程序中的单元测试
mvn  package : 依据项目生成 jar 文件
mvn  install :提交到本地仓库
mvn  deploy:  提交到中央仓库
mvn jetty:run :启动jetty容器
mvn eclipse:clean :清除eclipse的一些系统设置             
mvn eclipse:eclipse :生成 Eclipse 项目文件

 

       2.pom介绍

 

        2.1 maven约定俗称

            目录结构,其中pom.xml在其项目的根目录

        maven3-常用命令及pom引见(2)

    2.2 pom结构

    

<project xmlns="http://maven.apache.org/POM/4.0.0"
 2          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
 4             http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>        
 6     
 7     <!-- 坐标 -->
       <parent> ... </parent>
 8     <groupId>...</groupId> 
 9     <artifactId>...</artifactId>
10     <version>...</version>
11     <packaging>...</packaging>
       <!-- 仓库依赖 -->
12     <dependencies>...</dependencies>
14     <dependencyManagement>...</dependencyManagement>
       <!-- 项目模块配置 -->
15     <modules>...</modules>
       <!-- 全局配置文件 -->
16     <properties>...</properties>
17     
18     <!-- 构建过程的设置 -->
19     <build>...</build>
20     <reporting>...</reporting>
21     
22     <!-- 项目信息设置 -->
23     <name>...</name>
24     <description>...</description>
25     <url>...</url>
26     <inceptionYear>...</inceptionYear>
27     <licenses>...</licenses>
28     <organization>...</organization>
29     <developers>...</developers>
30     <contributors>...</contributors>
31     
32     <!-- 环境设置 -->
33     <issueManagement>...</issueManagement>
34     <ciManagement>...</ciManagement>
35     <mailingLists>...</mailingLists>
36     <scm>...</scm>
37     <prerequisites>...</prerequisites>
38     <repositories>...</repositories>
39     <pluginRepositories>...</pluginRepositories>
40     <distributionManagement>...</distributionManagement>
41     <profiles>...</profiles>
42 </project>
   2.3 标签详细介绍

   坐标:

   

<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>org.codehaus.mojo</groupId>
  <artifactId>my-project</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>
</project>
 

 

  1. groupId : 组织标识,例如:org.codehaus.mojo,在M2_REPO目录下,将是: org/codehaus/mojo目录。
  2. artifactId : 项目名称,例如:my-project,在M2_REPO目录下,将是:org/codehaus/mojo/my-project目录。
  3. version : 版本号,例如:1.0,在M2_REPO目录下,将是:org/codehaus/mojo/my-project/1.0目录。
  4. packaging : 打包的格式,可以为:pom , jar , maven-plugin , ejb , war , ear , rar , par
  5. modelVersion:定义pom版本号
  依赖:(依赖关系列表(dependency list)是POM的重要部分,也就是我们对jar包的管理)
 <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.0</version>
      <scope>test</scope>
    </dependency>
    …
  </dependencies>
   
  1. groupId , artifactId , version :引用的坐标
  2. scope : compile(default),provided,runtime,test,system  依赖的范围
  3. exclusions  需要排除的依赖的jar包
继承和聚合(子pom对父pom依赖 和 父项目对模块的依赖)
<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>org.codehaus.mojo</groupId>
  <artifactId>my-parent</artifactId>
  <version>2.0</version>
  <modules>
    <module>my-spring-web<module>
    <module>my-spring-service<module>
    <module>my-spring-common<module>
    <module>my-spring-dao<module>
  </modules>
</project>
 构建build:(可以帮我们指定 需要的maven插件,主要标签:Resources和Plugins)
    Resources:用于排除或包含某些资源文件
<resources>
      <resource>
        <targetPath>META-INF/plexus</targetPath>
        <filtering>false</filtering>
        <directory>${basedir}/src/main/plexus</directory>
        <includes>
          <include>configuration.xml</include>
        </includes>
        <excludes>
          <exclude>**/*.properties</exclude>
        </excludes>
      </resource>
    </resources>
   Plugins:设置构建的插件
 <build>
    …
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.0</version>
        <extensions>false</extensions>
        <inherited>true</inherited>
        <configuration>
          <classifier>test</classifier>
        </configuration>
        <dependencies>…</dependencies>
        <executions>…</executions>
      </plugin>