一个小时学会Maven

1、为何要Maven

在开发中常常须要依赖第三方的包,包与包之间存在依赖关系,版本间还有兼容性问题,有时还里要将旧的包升级或降级,当项目复杂到必定程度时包管理变得很是重要。html

 

Maven是当前最受欢迎的Java项目管理构建自动化综合工具,相似之前Java中的Ant、node.js中的npm、dotNet中的nuget、PHP中的Composerjava

Maven这个单词来自于意第绪语(犹太语),意为知识的积累。node

Maven提供了开发人员构建一个完整的生命周期框架。开发团队能够自动完成项目的基础工具建设,Maven使用标准的目录结构和默认构建生命周期。Maven让开发人员的工做更轻松,同时建立报表,检查,构建和测试自动化设置。Maven简化和标准化项目建设过程。处理编译,分配,文档,团队协做和其余任务的无缝链接。 Maven增长可重用性并负责创建相关的任务。mysql

每一个Java项目的目录结构都没有一个统一的标准,配置文件处处都是,单元测试代码到底应该放在那里也没有一个权威的规范。git

所以,咱们就要用到Maven(使用Ant也能够,不过编写Ant的xml脚本比较麻烦)----一个项目管理工具。web

Maven主要作了两件事:算法

  1. 统一开发规范与工具
  2. 统一管理jar包

若是没有Maven,你可能不得不经历下面的过程:spring

1 若是使用了spring,去spring的官网下载jar包;若是使用hibernate,去hibernate的官网下载Jar包;若是使用Log4j,去log4j的官网下载jar包.....
2 当某些jar包有依赖的时候,还要去下载对应的依赖jar包
3 当jar包依赖有冲突时,不得不一个一个的排查
4 执行构建时,须要使用ant写出不少重复的任务代码
5 当新人加入开发时,须要拷贝大量的jar包,而后重复进行构建
6 当进行测试时,须要一个一个的运行....检查

有了Maven,它提供了三种功能:sql

1 依赖的管理:仅仅经过jar包的几个属性,就能肯定惟一的jar包,在指定的文件pom.xml中,只要写入这些依赖属性,就会自动下载并管理jar包。
2 项目的构建:内置不少的插件与生命周期,支持多种任务,好比校验、编译、测试、打包、部署、发布...
3 项目的知识管理:管理项目相关的其余内容,好比开发者信息,版本等等 

官网:http://maven.apache.org/shell

教程:https://www.yiibai.com/maven/

Maven库:http://repo2.maven.org/maven2/ 

中央仓库资源:

http://mvnrepository.com/

https://search.maven.org/

2、安装与配置

其实主流的开发工具如IDEA、Eclipse都集成了Maven(可见重要性),但为了更加深入的学习与管理该工具(好比多个IDE共享的问题),我的建议仍是单独安装比较好。

2.一、官网下载安装包

在浏览器中打开下载地址:http://maven.apache.org/download.cgi

2.二、配置环境变量

注意:安装maven以前,必须先确保你的机器中已经安装了JDK,若是是Maven3则必须JDK1.7以上。

1.解压压缩包

2.添加环境变量MAVEN_HOME,值为apache-maven的安装路径(没有中文)

3.在Path环境变量的变量值末尾添加%MAVEN_HOME%\bin;

4.在cmd输入mvn –version,若是出现maven的版本信息,说明配置成功。

2.三、本地仓储配置

若是您不配置,默认会在以下位置存放从远程下载到的包:

 

从中央仓库下载的jar包,都会统一存放到本地仓库中。咱们须要配置本地仓库的位置。

打开maven安装目录,打开conf目录下的setting.xml文件。

能够参照下图配置本地仓储位置。

你还能够在运行时指定本地仓库位置:

mvn clean install -Dmaven.repo.local=d:\yourpath

2.四、中央仓库配置

当构建一个Maven项目时,首先检查pom.xml文件以肯定依赖包的下载位置,执行顺序以下:

1、从本地资源库中查找并得到依赖包,若是没有,执行第2步。
2、从Maven默认中央仓库中查找并得到依赖包(http://repo1.maven.org/maven2/),若是没有,执行第3步。
3、若是在pom.xml中定义了自定义的远程仓库,那么也会在这里的仓库中进行查找并得到依赖包,若是都没有找到,那么Maven就会抛出异常。

修改默认中央仓库地址

 

经常使用地址:

1、http://www.sonatype.org/nexus/  私服nexus工具使用
2、http://mvnrepository.com/ (推荐)
3、http://repo1.maven.org/maven2
四、http://maven.aliyun.com/nexus/content/groups/public/ 阿里云 (强力推荐)
5、http://repo2.maven.org/maven2/ 私服nexus工具使用
6、http://uk.maven.org/maven2/
7、http://repository.jboss.org/nexus/content/groups/public
8、http://maven.oschina.net/content/groups/public/  
9、http://mirrors.ibiblio.org/maven2/
10、http://maven.antelink.com/content/repositories/central/
11、http://nexus.openkoala.org/nexus/content/groups/Koala-release/
12、http://maven.tmatesoft.com/content/groups/public/

完整配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.home}/conf/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <localRepository>H:\InstallFiles\javaKit\mavenRes</localRepository>
  <!-- interactiveMode
   | This will determine whether maven prompts you when it needs input. If set to false,
   | maven will use a sensible default value, perhaps based on some other setting, for
   | the parameter in question.
   |
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->
  <!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>false</offline>
  -->
  <!-- pluginGroups
   | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
   | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
   | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
   |-->
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>
  <!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
  </proxies>
  <!-- servers
   | This is a list of authentication profiles, keyed by the server-id used within the system.
   | Authentication profiles can be used whenever maven must make a connection to a remote server.
   |-->
  <servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->
    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
<server> 
    <id>admin</id> 
    <username>admin</username> 
    <password>admin</password>
</server>
</servers>
  <!-- mirrors
   | This is a list of mirrors to be used in downloading artifacts from remote repositories.
   |
   | It works like this: a POM may declare a repository to use in resolving certain artifacts.
   | However, this repository may have problems with heavy traffic at times, so people have mirrored
   | it to several places.
   |
   | That repository definition will have a unique id, so we can create a mirror reference for that
   | repository, to be used as an alternate download site. The mirror site will be the preferred
   | server for that repository.
   |-->
  <mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
  <mirror>
    <id>nexus-aliyun</id>  
    <mirrorOf>*,!jeecg,!jeecg-snapshots</mirrorOf>  
    <name>Nexus aliyun</name>  
    <url>http://maven.aliyun.com/nexus/content/groups/public</url>  
  </mirror>
</mirrors>
  <!-- profiles
   | This is a list of profiles which can be activated in a variety of ways, and which can modify
   | the build process. Profiles provided in the settings.xml are intended to provide local machine-
   | specific paths and repository locations which allow the build to work in the local environment.
   |
   | For example, if you have an integration testing plugin - like cactus - that needs to know where
   | your Tomcat instance is installed, you can provide a variable here such that the variable is
   | dereferenced during the build process to configure the cactus plugin.
   |
   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
   | section of this document (settings.xml) - will be discussed later. Another way essentially
   | relies on the detection of a system property, either matching a particular value for the property,
   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
   | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
   | Finally, the list of active profiles can be specified directly from the command line.
   |
   | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
   |       repositories, plugin repositories, and free-form properties to be used as configuration
   |       variables for plugins in the POM.
   |
   |-->
  <profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
    <profile>
      <id>jdk-1.4</id>
      <activation>
        <jdk>1.4</jdk>
      </activation>
      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->
    <!--
     | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
     | might hypothetically look like:
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>
      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>
      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->
<profile>   
    <id>jdk1.7</id>    
    <activation>   
        <activeByDefault>true</activeByDefault>    
        <jdk>1.7</jdk>   
    </activation>    
    <properties>   
        <maven.compiler.source>1.7</maven.compiler.source>    
        <maven.compiler.target>1.7</maven.compiler.target>    
        <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>   
    </properties>   
</profile>  
</profiles>
  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
</settings>
View Code

2.五、命令行建立maven项目

方法一:

输入命令 mvn archetype:generate,按回车,根据提示输入参数,若是是第一次使用,须要下载插件,稍等几分钟便可。

切换目录,输入指令

选择骨架(模板):

输入座标:

确认后下载骨架,成功后的提示以下:

将项目转换成IDEA项目:

成功后能够看到增长了项目信息:

在IDEA中就能够直接打开项目了:

将项目打包

输入指令:mvn package

打包成功后:

方法二:

 在命令中指定参数

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=myapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

 执行结果:

在命令行输入:

 mvn archetype:generate -DgroupId=com.zhangguo.mvntest04 -DartifactId=MvnTest04 -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
View Code

 在命令行输入指令时请注意当前目录:

成功提示:

 新建一个简单web项目

mvn archetype:generate -DgroupId=com.zhangguo  -DartifactId=webappdemo  -Dpackage=com.zhangguo.webappdemo  -DarchetypeArtifactId=maven-archetype-webapp   -Dversion=1.0 -DinteractiveMode=No

若是下载骨架的速度太慢,能够用-DarchetypeCatalog=指定下载位置,以下所示:

mvn archetype:generate \
    -DgroupId=com.mycom.helloworld \
    -DartifactId=helloworld \
    -DarchetypeArtifactId=maven-archetype-quickstart \
    -DinteractiveMode=false \
    -DarchetypeCatalog=http://maven.aliyun.com/nexus/content/groups/public/

更多解决方法:https://www.cnblogs.com/del88/p/6286887.html

2.六、Jetty运行Web项目

是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境。Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布。开发人员能够将Jetty容器实例化成一个对象,能够迅速为一些独立运行(stand-alone)的Java应用提供网络和web链接。

官网:http://www.eclipse.org/jetty/

jetty的特色:

//易用性
易用性是 Jetty 设计的基本原则,易用性主要体如今如下几个方面:
经过 XML 或者 API 来对Jetty进行配置;默认配置能够知足大部分的需求;将 Jetty 嵌入到应用程序当中只须要很是少的代码;
//可扩展性
在使用了 Ajax 的 Web 2.0 的应用程序中,每一个链接须要保持更长的时间,这样线程和内存的消耗量会急剧的增长。这就使得咱们担忧整个程序会由于单个组件陷入瓶颈而影响整个程序的性能。可是有了 Jetty:
即便在有大量服务请求的状况下,系统的性能也能保持在一个能够接受的状态。利用 Continuation 机制来处理大量的用户请求以及时间比较长的链接。 另外 Jetty 设计了很是良好的接口,所以在 Jetty 的某种实现没法知足用户的须要时,用户能够很是方便地对 Jetty 的某些实现进行修改,使得 Jetty 适用于特殊的应用程序的需求。
//易嵌入性
Jetty 设计之初就是做为一个优秀的组件来设计的,这也就意味着 Jetty 能够很是容易的嵌入到应用程序当中而不须要程序为了使用 Jetty 作修改。从某种程度上,你也能够把 Jetty 理解为一个嵌入式的Web服务器。
Jetty 能够做为嵌入式服务器使用,Jetty的运行速度较快,并且是轻量级的,能够在Java中能够从test case中控制其运行。从而可使自动化测试再也不依赖外部环境,顺利实现自动化测试。
View Code

maven项目直接在jetty中运行

2.6.一、修改pom,增长jetty插件:

     <plugins>
            <!-- jetty插件 -->
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.26</version>
                <configuration>
                    <webAppSourceDirectory>src/main/webapp</webAppSourceDirectory>
                    <scanIntervalSeconds>3</scanIntervalSeconds>
                    <contextPath>/jetty</contextPath>
                    <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>4000</port>
                        </connector>
                    </connectors>
                </configuration>
            </plugin>
        </plugins>

2.6.二、将项目部署到jetty中运行:

mvn jetty:run            //运行项目于jetty上, 

2.6.三、在浏览器中输入访问地址:

http://127.00.0.1:4000/jetty

结束运行使用ctrl+c

2.6.四、idea中使用maven方式使用jetty

配置好后可直接点击idea中右边的maven选项卡,在plugins下有jetty选项,展开随便点那个均可以启动jetty,启动好后就能够在浏览器中输入地址访问web应用了。

2.七、在tomcat中运行

添加插件:

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <version>1.1</version>
                <configuration>
                    <path>/wp</path>
                    <port>8080</port>
                    <uriEncoding>UTF-8</uriEncoding>
                    <url>http://localhost:8080/manager/html</url>
                    <server>tomcat6</server>
                </configuration>
            </plugin>

 

执行命令:

tomcat:deploy   --部署一个web war包
tomcat:reload   --从新加载web war包
tomcat:start    --启动tomcat
tomcat:stop    --中止tomcat
tomcat:undeploy--中止一个war包
tomcat:run  启动嵌入式tomcat ,并运行当前项目

3、Maven概要

Maven 是一个Java项目管理工具,主要功能是统一开发规范与包的依赖管理。

3.一、Maven名词解释

1. POM(Project Object Model)项目对象模型

Maven项目对象模型(POM),能够经过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具

POM 与 Java 代码实现了解耦,当须要升级版本时,只须要修改POM,而不须要更改Java代码,而在POM稳定后,平常的Java代码开发基本不涉及POM的修改。

2. 坐标

groupId , artifactId , version 三个元素是项目的坐标,惟一的标识这个项目。

groupId 项目所在组,通常是组织或公司

artifactId 是当前项目在组中的惟一ID;

version 表示版本,SNAPSHOT表示快照,表示此项目还在开发中,不稳定。

groupId 和实际项目不必定是一一对应的,maven 有模块的概念,例如 spring-core, spring-context...;groupId 也不该该只对应公司或组织名,建议具体到项目名,由于公司或者组织下有多个项目,而artifactId只能表明模块名。

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

3.依赖范围

1. compile : 编译,测试,运行都有效,默认的选择
2. test : 测试有效,例如junit
3. provided : 编译,测试有效,例如 servlet ,运行时容器会提供实现
4. runtime : 运行和测试有效,例如 jdbc,编译时只需相应的接口,测试和运行时才须要具体的实现
5. system : 编译,测试有效。使用此范围的依赖必须经过systemPath元素显式的指定依赖文件,于是
此类依赖是不经过Maven仓库解析的,通常适合于本机测试环境下,依赖本地起的服务。

构建

maven支持许多种的应用程序类型,对于每一种支持的应用程序类型都定义好了一组构建规则和工具集。

输出管理

maven能够管理项目构建的产物,并将其加入到用户库中。这个功能能够用于项目组和其余部门之间的交付行为

依赖关系

maven对依赖关系的特性进行细致的分析和划分,避免开发过程当中的依赖混乱和相互污染行为

文档和构建结果

maven的site命令支持各类文档信息的发布,包括构建过程的各类输出,javadoc,产品文档等。

项目关系

一个大型的项目一般有几个小项目或者模块组成,用maven能够很方便地管理

移植性管理

maven能够针对不一样的开发场景,输出不一样种类的输出结果

  • Project: 项目
    • 任何你想 build 的事物,Maven都会把它们看成是一个 Project。
    • 这些 Project 被定义为 POM(Project Object Model)。
    • 一个 Project 能够依赖其余的project,一个 project 也能够有多个子project组成。
  • POM:文档对象模型
    • POM(pom.xml) 是 Maven 的核心文件,它是指示 Maven 如何工做的元数据文件,相似 ant 的 build.xml 文件。
    • pom.xml 文件应该位于每一个 Project 的根目录。
    GroupId: 组织号
    • 顾名思义,这个应该是公司名或组织名。
  • ArtifactId:项目名
    • 构建出来的文件名,通常来讲或,这个也是project名。
  • Packaging:打包
    • 项目打包的类型,能够是将jar、war、rar、ear、pom,默认是jar。
  • Version:版本
    • 项目的版本,项目的惟一标识由 groupId+artifactId+packaging+versionz 组成。
  • Dependency: 依赖
    • 为了可以 build 或运行,一个典型的java project会依赖其余的包,在Maven中,这些被依赖的包就被称为 dependency。
  • Plug-in:插件
    • Maven是有插件组织的,它的每个功能都是由插件提供的,主要的插件是由 java 来写的,可是他也支持 beanshell 和 ant 脚本编写的插件。
  • Repository:仓库
    • 仓库用来存放artifact的,能够是本地仓库,也能够是远程仓库,Maven是由一个默认的仓库
  • Snapshot:快照
    • 工程中能够(也应该)有这样一个特殊的版本:这个版本能够告诉Maven,该工程正在处于开发阶段,会常常更新(但还为发布)。当其余工程依赖此类型的artifact时,Maven会在仓库中寻找该artifact的最新版本,并自动下载、使用该最新版本。

3.二、Maven的生命周期

maven把项目的构建划分为不一样的生命周期(lifecycle)。粗略一点的话,它这个过程(phase)包括:编译、测试、打包、集成测试、验证、部署。maven中全部的执行动做(goal)都须要指明本身在这个过程当中的执行位置,而后maven执行的时候,就依照过程的发展依次调用这些goal进行各类处理。

这个也是maven的一个基本调度机制。通常来讲,位置稍后的过程都会依赖于以前的过程。固然,maven一样提供了配置文件,能够依照用户要求,跳过某些阶段。

三种生命周期

  1. Clean Lifecycle 在进行真正的构建以前进行一些清理工做。
  2. Default Lifecycle 构建的核心部分,编译,测试,打包,部署等等。
  3. Site Lifecycle 生成项目报告,站点,发布站点。

下面列出了default、clean和site生命周期的全部构建阶段,这些阶段按照指定的顺序执行。

clean生命周期

执行阶段 描述说明
pre-clean 在实际的项目清理以前执行所需的过程
clean 删除前一个构建生成的全部文件
post-clean 执行完成项目清理所需的过程


Default生命周期

执行阶段 描述说明
validate 验证项目是正确的,全部必要的信息都是可用的。
initialize 初始化构建状态,例如设置属性或建立目录。
generate-sources 生成包含在编译中的任何源代码。
process-sources 处理源代码,例如过滤任何值。
generate-resources 生成包含在包中的资源。
process-resources 将资源复制并处理到目标目录中,准备打包。
compile 编译项目的源代码。
process-classes 从编译后生成生成的文件,例如在Java类上执行字节码加强。
generate-test-sources 生成包含在编译中的任何测试源代码。
process-test-sources 处理测试源代码,例如过滤任何值。
generate-test-resources 为测试建立资源。
process-test-resources 将资源复制并处理到测试目标目录中。
test-compile 将测试源代码编译到测试目标目录
process-test-classes 从测试编译后post-process生成文件,例如在Java类上执行字节码加强。对于Maven 2.0.5和以上。
test 使用合适的单元测试框架运行测试。这些测试不该该要求打包或部署代码。
prepare-package 在实际包装前执行必要的准备工做。这一般会致使包的一个未打包的、通过处理的版本。(Maven 2.1及以上)
package 使用已编译的代码,并将其打包成可部署格式,例如JAR。
pre-integration-test 执行集成测试以前须要执行的操做。这可能涉及到设置所需的环境等问题。
integration-test 在须要集成测试的环境中,处理并部署包。
post-integration-test 执行集成测试后所须要的操做。这可能包括清理环境。
verify 运行任何检查以验证包是否有效,并知足质量标准。
install 将该包安装到本地存储库中,做为本地其余项目的依赖项。
deploy 在集成或发布环境中完成,将最终包复制到远程存储库中,以便与其余开发人员和项目共享。

 

Site生命周期

 

执行阶段 描述说明
pre-site 在实际的项目站点生成以前执行过程
site 生成项目的站点文档
post-site 执行肯定站点生成的过程,并为站点部署作好准备
site-deploy 将生成的站点文档部署到指定的web服务器

注意:执行某个生命周期的某个阶段不会影响其它的生命周期!

若是要同时执行多个生命周期的阶段可在命令行输入多个命令,中间以空格隔开,例如: clean package 该命令执行clean生命周期的clean阶段和default生命周期的package阶段。

3.三、Maven标准工程结构

Maven的标准工程结构以下:

3.四、Maven的"约束优于配置"

所谓的"约束优于配置",在maven中并非彻底不能够修改的,他们只是一些配置的默认值而已。可是除非必要,并不须要去修改那些约定内容。maven默认的文件存放结构如1.3所示。

每个阶段的任务都知道怎么正确完成本身的工做,好比compile任务就知道从src/main/java下编译全部的java文件,并把它的输出class文件存放到target/classes中。

对maven来讲,采用"约定优于配置"的策略能够减小修改配置的工做量,也能够下降学习成本,更重要的是,给项目引入了统一的规范。

3.五、Maven的版本规范

maven有本身的版本规范,通常是以下定义 <major version>.<minor version>.<incremental version>-<qualifier> ,好比1.2.3-beta-01。要说明的是,maven本身判断版本的算法是major,minor,incremental部分用数字比较,qualifier部分用字符串比较,因此要当心 alpha-2和alpha-15的比较关系,最好用 alpha-02的格式。

maven在版本管理时候可使用几个特殊的字符串 SNAPSHOT,LATEST,RELEASE。好比"1.0-SNAPSHOT"。各个部分的含义和处理逻辑以下说明:

SNAPSHOT
这个版本通常用于开发过程当中,表示不稳定的版本。

LATEST
指某个特定构件的最新发布,这个发布多是一个发布版,也多是一个snapshot版,具体看哪一个时间最后。

RELEASE
是指仓库中最后的一个非快照版本

规范:

一、同一项目中全部模块版本保持一致
二、子模块统一继承父模块的版本
三、统一在顶层模块Pom的<dependencyManagement/>节中定义全部子模块的依赖版本号,子模块中添加依赖时不要添加版本号
四、开发测试阶段使用SNAPSHOT
五、生产发布使用RELEASE
六、新版本迭代只修改顶层POM中的版本

3.六、项目骨架Maven Archetype

什么是Maven Archetype? 简单的说就是一个Maven项目的基础模板,利用这个模板咱们就可快速的创建一个新的该类型项目,同时也能够创建本身的项目骨架。
Maven所提供的archetype功能都是由插件Maven Archetype Plugin完成的

官网地址:http://maven.apache.org/archetype/maven-archetype-plugin/

主要命令:

  • archetype:generate   从项目骨架建立一个maven项目,老版本里使用的是archetype:create 
  • archetype:create-from-project  根据一个项目建立项目骨架

使用archetype:generate建立项目

 mvn archetype:generate命令参数解释
项目相关参数:

参数

含义

groupId

当前应用程序隶属的Group的ID

artifactId 

当前应用程序的ID

package

代码生成时使用的根包的名字,若是没有给出,默认使用archetypeGroupId

原型有关参数表

 

参数 含义

archetypeGroupId

原型(archetype)的Group ID

archetypeArtifactId 

原型(archetype)ID

archetypeVersion 

原型(archetype)版本

archetypeRepository

包含原型(archetype)的资源库

archetypeCatalog

archetype分类,这里按位置分类有:
‘local’  本地,一般是本地仓库的archetype-catalog.xml文件
‘remote’  远程,是maven的中央仓库
file://...' 直接指定本地文件位置archetype-catalog.xml
http://...' or 'https://...'  网络上的文件位置 archetype-catalog.xml
'internal'
默认值是remote,local

filter

查找时过滤artifactId or groupId:artifactId

package

代码生成时使用的根包的名字,若是没有给出,默认使用archetypeGroupId

命令示例:
新建一个简单web项目
mvn archetype:generate -DgroupId=com.domain 
  -DartifactId=webappdemo
  -Dpackage=com.domain.webappdemo
  -DarchetypeArtifactId=maven-archetype-webapp 
  -Dversion=1.0 -DinteractiveMode=No

新建一个struts2 web项目
mvn archetype:generate -B -DgroupId=com.mycompany.mysystem
                            -DartifactId=myWebApp
                            -DarchetypeGroupId=org.apache.struts
                            -DarchetypeArtifactId=struts2-archetype-convention
                            -DarchetypeVersion=<CURRENT_STRUTS_VERSION>
                            -DremoteRepositories=http://struts.apache.org
maven默认提供的archetype类型能够参考http://maven.apache.org/guides/introduction/introduction-to-archetypes.html

从一个已有项目生成一个archetype

mvn clean archetype:create-from-project -Darchetype.properties=./archetype.properties -Darchetype.filteredExtentions=java,xml,jsp,properties,sql

这里首先定义了一个archetype.properties文件在命令行被执行的目录,里面的内容是
tablePrefix是QucikStart项目里用到的,想在新项目中替换掉的内容。
-DfilteredExtentions,由于maven默认不会扫描sql文件,而这里是但愿修改tablePrefix的.
properties参考http://maven.apache.org/archetype/maven-archetype-plugin/create-from-project-mojo.html

3.七、maven的项目对象模型

Maven包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理系统(Dependency Management System),和用来运行定义在生命周期阶段(phase)中插件(plugin)目标(goal)的逻辑。

项目对象模型 (Project Object Model)

一个maven工程都有一个pom.xml文件,经过pom.xml文件定义项目的坐标、项目依赖、项目信息、插件目标等。

依赖管理系统(Dependency Management System)

经过maven的依赖管理对项目所依赖的jar 包进行统一管理。 好比:项目依赖junit4.9,经过在pom.xml中定义junit4.9的依赖即便用junit4.9,以下所示是junit4.9的依赖定义:

<!-- 依赖关系 -->
    <dependencies>
        <!-- 此项目运行使用junit,因此此项目依赖junit -->
        <dependency>
            <!-- junit的项目名称 -->
            <groupId>junit</groupId>
            <!-- junit的模块名称 -->
            <artifactId>junit</artifactId>
            <!-- junit版本 -->
            <version>4.9</version>
            <!-- 依赖范围:单元测试时使用junit -->
            <scope>test</scope>
        </dependency>
    </dependencies>

一个项目生命周期(Project Lifecycle)

使用maven完成项目的构建,项目构建包括:清理、编译、测试、部署等过程,maven将这些过程规范为一个生命周期,以下所示是生命周期的各各阶段:

 

maven经过执行一些简单命令便可实现上边生命周期的各各过程,好比执行mvn compile执行编译、执行mvn clean执行清理。

一组标准集合

maven将整个项目管理过程定义一组标准,好比:经过maven构建工程有标准的目录结构,有标准的生命周期阶段、依赖管理有标准的坐标定义等。

插件(plugin)目标(goal)

maven 管理项目生命周期过程都是基于插件完成的。

Maven坐标是一组能够唯一标识构件的三元组值

  • groupId,表明构件的实体或组织例如:org.inspur.loushang
  • artifactId,实际的构件的名称,例如framework
  • version,该构件件的版本号
  • packaging :定义Maven项目打包的方式,首先,打包方式一般与所生成构件的文件扩展名对应,如上例中的packaging为jar,最终的文件名为my-app-0.0.1-SNAPSHOT.jar。也能够打包成war, ear等。当不定义packaging的时候,Maven 会使用默认值jar

    classifier: 该元素用来帮助定义构建输出的一些附件。附属构件与主构件对应,如上例中的主构件为my-app-0.0.1-SNAPSHOT.jar,该项目可能还会经过一些插件生成如my-app-0.0.1-SNAPSHOT-javadoc.jar,my-app-0.0.1-SNAPSHOT-sources.jar, 这样附属构件也就拥有了本身惟一的坐标

4、IDE中使用Maven(IDEA或Eclipse)

4.一、将现有项目转换成Maven项目

若是有一个非maven的项目想转换成maven管理的项目,只需以下操做便可:

在项目上右键,添加框架支持

 选择maven与其它想添加的框架

选择容许自动导入

 这样就成功转换成了一个maven项目

4.二、在IDEA中建立Maven项目

4.2.一、建立项目

4.1是一种建立maven项目的办法,但不推荐,由于没有使用统一的骨架,能够一开始就选择建立maven项目,步骤以下:

步骤一:首先先建立一个project,上次我说过了建立一个project就是一个工做空间,在这里就是建立一个maven的工做空间

步骤二:选择maven项目,指定骨架,这里选择的是一个webapp,固然webapp骨架有很是多,这里选择apache提供的

步骤三:填写项目的座标,公司编号(通常倒置域名),项目名称,版本:

 

步骤四:由于IDEA内置了Maven,能够选择默认内置的Maven;固然最好是选择本身安装并配置好的环境,让全部的IDE统一:

这里能够点绿色的小加号添加参数 

 步骤五:选择项目名称,位置,通常默认

点击Finish项目就建立完成了,以下图所示:

4.2.二、配置Tomcat

点击右上角下拉框,选择Edit Configurations,编辑配置

添加tomcat的配置

配置tocat服务器,命名,选择tomcat版本,等想配置的信息;最关键的是须要将项目部署出去,能够直接击fix

选择war explored,他们的区别

固然也能够手动选择要部署出去的项目,特别是有多个项目的状况

配置项目结构,特别是要注意依赖的包须要部署到WEB-INF的lib目录下

完成后能够启服务器,效果以下:

固然能够直接使用插件让项目部署到jetty中经过maven运行,不需tomcat。

4.2.三、war和war exploded的区别

是选择war仍是war exploded 这里首先看一下他们两个的区别:


(1)war模式这种能够称之为是发布模式,看名字也知道,这是先打成war包,再发布;

(2)war exploded模式是直接把文件夹、jsp页面 、classes等等移到Tomcat 部署文件夹里面,进行加载部署。所以这种方式支持热部署,通常在开发的时候也是用这种方式。

(3)在平时开发的时候,使用热部署的话,应该对Tomcat进行相应的设置,这样的话修改的jsp界面什么的东西才能够及时的显示出来。

 

两种方式得部署方式是不同的,在获取项目的路径的时候获得的结果是不同的

String contextPath = request.getSession().getServletContext().getRealPath("/");

 4.2.四、修改项目结构

maven有一个很重要的功能是规范项目,标准的项目结构以下所示:

可是你会发现默认建立的项并不是是完整的,如写源代码的目录没有,添加方法以下

当前项目结构以下:

添加相应的目录,选择打开项目结构:

项目结构以下:

蓝色:源代码

绿色:测试

资源文件(配置信息)

测试资源文件

被排除的(打包里被忽视)

目标位置右键添加目录:

添加后的目录结构以下:

4.2.五、不能添加Servlet的解决方法

打开项目描述文件,指定源代码目录:

        <sourceRoots>
        <root url="file://$MODULE_DIR$/src/main/java" />
        </sourceRoots>

 修改方法:

这样就能够了:

若是没有添加tomcat的配置信息或没有servet的核心包也可能出现该问题,建议修改pom加上:

    <!-- JSTL -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <!-- Servlet核心包 -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.0.1</version>
      <scope>provided</scope>
    </dependency>
    <!--JSP -->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>

4.三、在Eclipse中建立Maven工程

Maven插件

在Eclipse中建立Maven工程,须要安装Maven插件。

通常较新版本的Eclipse都会带有Maven插件,若是你的Eclipse中已经有Maven插件,能够跳过这一步骤。

点击Help -> Eclipse Marketplace,搜索maven关键字,选择安装红框对应的Maven插件。

Maven环境配置

点击Window -> Preferences

以下图所示,配置settings.xml文件的位置

建立Maven工程

File -> New -> Maven Project -> Next,在接下来的窗口中会看到一大堆的项目模板,选择合适的模板。

接下来设置项目的参数,以下:

groupId是项目组织惟一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构。

artifactId就是项目的惟一的标识符,实际对应项目的名称,就是项目根目录的名称。

点击Finish,Eclipse会建立一个Maven工程。

4.四、使用Maven进行构建

Eclipse中构建方式

在Elipse项目上右击 -> Run As 就能看到不少Maven操做。这些操做和maven命令是等效的。例如Maven clean,等同于mvn clean命令。

你也能够点击Maven build,输入组合命令,并保存下来。以下图:

Maven命令构建方式

固然,你也能够直接使用maven命令进行构建。

进入工程所在目录,输入maven命令就能够了。

以下图

4.五、建立Maven多模块项目

多模块项目不必定要使用Maven,普通项目也能够。

4.5.一、maven多模块优势

一、复用,划分出来的模块能够供其余项目使用。
二、固化,划分出来的某个模块可以让专人开发,沉淀技术,分工协做。
三、优化依赖,每一个模块有各自的pom。
四、节省时间,可在单个模块上进行build。

4.5.二、在IDEA中建立多模块项目

一、建立父模块

 建立一个普通的Maven项目,不须要选择骨架(archetype)

指定座标

完成后一个普通的Maven项目就建立好了

二、建立子模块

子模块能够有多个,相互间能够依赖

在父模块上右键建立一个Module(模块)

utils

建立普通maven项目没有骨架

指定模块名称

完成后以下:

entities

建立成一个非maven项目(固然能够是maven的)

指定项目名称

完成后

webapp

建立一个基于骨架的maven WebApp项目

设置名称

选择maven home

完成后的结果

三、模块间依赖

方法一:依赖class

 打开项目结构

添加模块依赖

指定要依赖的模块

依赖成功

指定输出

最终项目结构

结果

方法一:经过座标直接依赖jar

hr-ui模块的pom文件

<?xml version="1.0" encoding="UTF-8"?>

<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">
    <parent>
        <artifactId>HR</artifactId>
        <groupId>com.zhangguo.hr</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>hr-ui</artifactId>
    <packaging>war</packaging>

    <name>hr-ui Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
 <dependency> <groupId>com.zhangguo.hr</groupId> <artifactId>hr-core</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
    </dependencies>

    <build>
        <finalName>hr-ui</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

结果

4.六、多模块项目依赖管理与依赖继承

4.6.一、指定父模块与默认继承

dependencies即便在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(所有继承)

父模块的pom

<?xml version="1.0" encoding="UTF-8"?>
<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.zhangguo.maven03</groupId>
    <artifactId>Maven03</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>Maven03-Dao</module>
        <module>Maven03-Service</module>
    </modules>


    <properties>
        <junit.version>4.12</junit.version>
        <spring.version>RELEASE</spring.version>
    </properties>

        <dependencies>
            <!-- junit -->
            <!-- https://mvnrepository.com/artifact/junit/junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>

</project>

子模块的pom

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!--声明本身的父模块,将继承父模块的全部依赖-->
    <parent>
        <artifactId>Maven03</artifactId>
        <groupId>com.zhangguo.maven03</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>Maven03-Dao</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
            <!--版本是从父模块依赖过来的properties-->
        </dependency>
    </dependencies>

</project>

RELEASE表示最新的发布版本,结果以下:

4.6.二、依赖管理

dependencyManagement里只是声明依赖,并不实现引入,所以子项目须要显式的声明须要用的依赖。若是不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,而且没有指定具体版本,才会从父项目中继承该项,而且version和scope都读取自父pom;另外若是子项目中指定了版本号,那么会使用子项目中指定的jar版本。

 当我把父项目中的依赖放到依赖管理中的效果以下所示:

<?xml version="1.0" encoding="UTF-8"?>
<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.zhangguo.maven03</groupId>
    <artifactId>Maven03</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>Maven03-Dao</module>
        <module>Maven03-Service</module>
    </modules>


    <properties>
        <junit.version>4.12</junit.version>
        <spring.version>RELEASE</spring.version>
    </properties>

    <!--dependencyManagement里只是声明依赖,并不实现引入,
    所以子项目须要显式的声明须要用的依赖。
    若是不在子项目中声明依赖,是不会从父项目中继承来的;
    只有在子项目中写了该依赖项,而且没有指定具体版本,才会从父项目中继承该项,
    而且version和scope都读取自父pom;
    另外若是子项目中指定了版本号,那么会使用子项目中指定的jar版本。-->

    <dependencyManagement>
        <dependencies>
            <!-- junit -->
            <!-- https://mvnrepository.com/artifact/junit/junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
</project>

结果

这里的父模块与子模块都并无依赖junit了,这时的依赖只是用于管理,并无真正依赖。

<dependencyManagement>元素既能让子模块继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性。

使用<dependencyManagement>声明的依赖即不会引入依赖,也不会给他的子模块引入依赖。但这段配置是能够继承的。

在子类中,依赖配置较原来就简单了。能够在子类中只配置groupId和artifactId ,省去了version。由于完整的依赖声明已经包含在父POM中。 这样能够统一项目范围中依赖的版本,帮助下降依赖冲突的概率。
若是子模块不声明依赖的使用,即便该依赖已经在父POM的dependencyManangement中声明了,也不会产生任何实际的效果。

若是想要在某个模块中使用和另外一个模块中彻底同样的dependencyManagement配置,除了赋值和继承外,还可使用import范围依赖将这一配置导入。

咱们要达到的目的是:父模块做版本管理不实际依赖,子模块按需依赖。

父模块pom

<?xml version="1.0" encoding="UTF-8"?>
<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.zhangguo.maven03</groupId>
    <artifactId>Maven03</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>Maven03-Dao</module>
        <module>Maven03-Service</module>
    </modules>
    
    <!--版本管理-->
    <properties>
        <junit.version>4.12</junit.version>
        <spring.version>RELEASE</spring.version>
    </properties>

    <!--依赖声明-->
    <dependencyManagement>
        <dependencies>
            <!-- junit -->
            <!-- https://mvnrepository.com/artifact/junit/junit -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

子模块pom:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>Maven03</artifactId>
        <groupId>com.zhangguo.maven03</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>Maven03-Service</artifactId>


    <dependencies>
        <!--按需依赖,版本被父模块控制,能够自行声明,优先级更高-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>

</project>

结果:

 

这样作的好处:统一管理项目的版本号,确保应用的各个项目的依赖和版本一致,才能保证测试的和发布的是相同的成果,所以,在顶层pom中定义共同的依赖关系。同时能够避免在每一个使用的子项目中都声明一个版本号,这样想升级或者切换到另外一个版本时,只须要在父类容器里更新,不须要任何一个子项目的修改;若是某个子项目须要另一个版本号时,只须要在dependencies中声明一个版本号便可。子类就会使用子类声明的版本号,不继承于父类版本号。

5、Maven中经常使用指令使用

5.一、在命令行中使用Maven指令

一、 显示版本信息

mvn -version
mvn -v 

结果:

二、使用互动模式建立项目

mvn archetype:generate

结果:

具体请看本文前面:https://www.cnblogs.com/best/p/9622472.html#_lab2_1_4

三、使用非互动模式(指定参数建立项目)

普通项目骨架:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=myapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Web项目骨架:

mvn archetype:generate -DgroupId=com.zhangguo  -DartifactId=webappdemo  -Dpackage=com.zhangguo.webappdemo  -DarchetypeArtifactId=maven-archetype-webapp   -Dversion=1.0 -DinteractiveMode=No

具体请看本文前面:https://www.cnblogs.com/best/p/9622472.html#_lab2_1_4

四、将项目转化为idea项目

mvn idea:idea

结果:

 

五、将项目转化为Eclipse项目

mvn eclipse:eclipse

结果:

六、编译,将Java 源程序编译成 class 字节码文件

mvn compile

结果:

七、测试,并生成测试报告

mvn test

导入到idea中,添加测试用例

结果:

 

八、将之前编译获得的旧的 class 字节码文件删除

mvn clean

结果:

九、打包,动态 web工程打 war包,Java工程打 jar 包

mvn pakage

结果:

十、将项目生成 jar 包放在仓库中,以便别的模块调用,安装到本地

mvn install

 结果:

在其它项目中依赖

11. 生成项目相关信息的网站:mvn site


12. 编译测试的内容:mvn test-compile


13. 只打jar包:mvn jar:jar


14. 只打war包:mvn war:war


15. 清除eclipse的一些系统设置:mvn eclipse:clean


16. 运行项目于jetty上:mvn jetty:run


17. 生成Wtp插件的Web项目:mvn -Dwtpversion=1.0 eclipse:eclipse


18. 清除Eclipse项目的配置信息(Web项目):mvn -Dwtpversion=1.0 eclipse:clean


1九、部署项目:

mvn deploy:deploy-file -DgroupId=com -DartifactId=client -Dversion=0.1.0 -Dpackaging=jar -Dfile=d:\client-0.1.0.jar -DrepositoryId=maven-repository-inner -Durl=ftp://xxxxxxx/opt/maven/repository/

其它指令:

mvn -e              显示详细错误 信息. 

mvn validate        验证工程是否正确,全部须要的资源是否可用。
 
mvn test-compile    编译项目测试代码。

mvn integration-test     在集成测试能够运行的环境中处理和发布包。 

mvn verify               运行任何检查,验证包是否有效且达到质量标准。    
 
mvn generate-sources     产生应用须要的任何额外的源代码,如xdoclet。

mvn help:describe -Dplugin=help 使用 help 插件的  describe 目标来输出 Maven Help 插件的信息。 

mvn help:describe -Dplugin=help -Dfull 使用Help 插件输出完整的带有参数的目标列 

mvn help:describe -Dplugin=compiler -Dmojo=compile -Dfull 获取单个目标的信息,设置  mojo 参数和  plugin 参数。此命令列出了Compiler 插件的compile 目标的全部信息 

mvn help:describe -Dplugin=exec -Dfull 列出全部 Maven Exec 插件可用的目标 

mvn help:effective-pom 看这个“有效的 (effective)”POM,它暴露了 Maven的默认设置

mvn archetype:create -DgroupId=org.sonatype.mavenbook.ch03 -DartifactId=simple -DpackageName=org.sonatype.mavenbook 建立Maven的普通java项目,在命令行使用Maven Archetype 插件 

mvn exec:java -Dexec.mainClass=org.sonatype.mavenbook.weather.Main Exec 插件让咱们可以在不往 classpath 载入适当的依赖的状况下,运行这个程序 

mvn dependency:resolve 打印出已解决依赖的列表 

mvn dependency:tree 打印整个依赖树

mvn install -X 想要查看完整的依赖踪影,包含那些由于冲突或者其它缘由而被拒绝引入的构件,打开 Maven 的调试标记运行 

mvn install -Dmaven.test.skip=true 给任何目标添加maven.test.skip 属性就能跳过测试 

mvn install assembly:assembly 构建装配Maven Assembly 插件是一个用来建立你应用程序特有分发包的插件

mvn jetty:run     调用 Jetty 插件的 Run 目标在 Jetty Servlet 容器中启动 web 应用 

mvn compile       编译你的项目 

mvn clean install 删除再编译

mvn hibernate3:hbm2ddl 使用 Hibernate3 插件构造数据库

5.二、经常使用Maven命令

生命周期

阶段描述

mvn validate

验证项目是否正确,以及全部为了完整构建必要的信息是否可用

mvn generate-sources

生成全部须要包含在编译过程当中的源代码

mvn process-sources

处理源代码,好比过滤一些值

mvn generate-resources

生成全部须要包含在打包过程当中的资源文件

mvn process-resources

复制并处理资源文件至目标目录,准备打包

mvn compile

编译项目的源代码

mvn process-classes

后处理编译生成的文件,例如对Java类进行字节码加强(bytecode enhancement

mvn generate-test-sources

生成全部包含在测试编译过程当中的测试源码

mvn process-test-sources

处理测试源码,好比过滤一些值

mvn generate-test-resources

生成测试须要的资源文件

mvn process-test-resources

复制并处理测试资源文件至测试目标目录

mvn test-compile

编译测试源码至测试目标目录

mvn test

使用合适的单元测试框架运行测试。这些测试应该不须要代码被打包或发布

mvn prepare-package

在真正的打包以前,执行一些准备打包必要的操做。这一般会产生一个包的展开的处理过的版本(将会在Maven 2.1+中实现)

mvn package

将编译好的代码打包成可分发的格式,如JARWAR,或者EAR

mvn pre-integration-test

执行一些在集成测试运行以前须要的动做。如创建集成测试须要的环境

mvn integration-test

若是有必要的话,处理包并发布至集成测试能够运行的环境

mvn post-integration-test

执行一些在集成测试运行以后须要的动做。如清理集成测试环境。

mvn verify

执行全部检查,验证包是有效的,符合质量规范

mvn install

安装包至本地仓库,以备本地的其它项目做为依赖使用

mvn deploy

复制最终的包至远程仓库,共享给其它开发人员和项目(一般和一次正式的发布相关)

使用参数

-Dmaven.test.skip=true: 跳过单元测试(eg: mcn clean package -Dmaven.test.skip=true)

 

 

mvn archetype:create 建立Maven项目
mvn compile 编译源代码
mvn test 运行应用程序中的单元测试
mvn install 在本地Respository中安装jar
mvn eclipse:eclipse 生成eclipse项目文件
mvn jetty:run 启动jetty服务
mvn clean 清除项目目录中的生成结果
mvn site 生成项目相关信息的网站
mvn package 根据项目生成的jar

5.三、在IDE(集成开发工具)中使用指令

 5.3.一、工具栏介绍

图标1:从新导入全部的maven项目,刷新

图标2:从新生成全部源代码并更新文件夹

图标3:下载源码或文件

图标4:添加一个外部的Maven项目

图标5:执行选择的指令

图标6:执行自定义的指令

图标7:切换离线模式

图标8:切换成跳过测试模式

图标9:显示依赖结构图

图标10:折叠全部

图标11:Maven设置

 5.3.二、快捷执行Maven命令

选择命令后右键run

建立命令,可反复使用并指定名称与参数

定义细节

执行

6、使用指导

6.一、如何添加外部依赖jar包

在POM文件中引入对应的<dependency>标签便可

<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.zp.maven</groupId>

  <artifactId>MavenDemo</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>jar</packaging>

  <name>MavenDemo</name>

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

 

  <properties>

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

  <junit.version>3.8.1</junit.version>

  </properties>

 

  <dependencies>

  <dependency>

  <groupId>junit</groupId>

  <artifactId>junit</artifactId>

  <version>${junit.version}</version>

  <scope>test</scope>

  </dependency>

  <dependency>

  <groupId>log4j</groupId>

  <artifactId>log4j</artifactId>

  <version>1.2.12</version>

  <scope>compile</scope>

  </dependency>

  </dependencies>

</project>
View Code

<dependency>标签最经常使用的四个属性标签:

groupId:项目组织惟一的标识符,实际对应JAVA的包的结构。

artifactId:项目惟一的标识符,实际对应项目的名称,就是项目根目录的名称。

version:jar包的版本号。能够直接填版本数字,也能够在properties标签中设置属性值。

scope:jar包的做用范围。能够填写compile、runtime、test、system和provided。用来在编译、测试等场景下选择对应的classpath

6.二、如何寻找jar包

能够在http://mvnrepository.com/站点搜寻你想要的jar包版本

6.三、如何使用Maven插件(Plugin)

要添加Maven插件,能够在pom.xml文件中添加<plugin>标签。

<plugins>
            <!-- jetty插件 -->
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.26</version>
                <configuration>
                    <webAppSourceDirectory>src/main/webapp</webAppSourceDirectory>
                    <scanIntervalSeconds>3</scanIntervalSeconds>
                    <contextPath>/jetty</contextPath>
                    <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>4000</port>
                        </connector>
                    </connectors>
                </configuration>
            </plugin>
        </plugins>

6.四、如何一次编译多个工程

在Maven中,容许一个Maven Project中有多个Maven Module

1.建立maven父工程步骤:new-->other-->选择maven project-->next-->勾选create a simple project-->next-->填写Group IdArtifact IdVersion --> packaging选择pom-->finish

2.建立maven子工程步骤:选中刚才建立的父工程右键-->new-->other-->选择maven module-->next-->勾选create a simple project-->填写module name(其实就是artifact id-->next-->GAV继承父工程-->packaging选择你须要的-->finish

3.完成,刷新父工程;若有多个子工程,继续按照第二步骤建立。

这时打开XXX中的pom.xml能够看到其中有如下标签

<modules>
  <module>xxx1</module>
</modules>

 

选择编译XXX时,会依次对它的全部Module执行相同操做。

6.五、经常使用Maven插件

Maven 是一个执行插件的框架,每个任务其实是由插件完成的。Maven 提供如下两种类型插件:构建插件,在生成过程当中执行,并在 pom.xml 中的<build/> 元素进行配置;报告插件,在网站生成期间执行,在 pom.xml 中的 <reporting/> 元素进行配置。

maven-antrun-plugin

http://maven.apache.org/plugins/maven-antrun-plugin/

maven-antrun-plugin能让用户在Maven项目中运行Ant任务。用户能够直接在该插件的配置以Ant的方式编写Target,而后交给该插件的run目标去执行。在一些由Ant往Maven迁移的项目中,该插件尤为有用。此外当你发现须要编写一些自定义程度很高的任务,同时又以为Maven不够灵活时,也能够以Ant的方式实现之。maven-antrun-plugin的run目标一般与生命周期绑定运行。

maven-archetype-plugin

http://maven.apache.org/archetype/maven-archetype-plugin/

Archtype指项目的骨架,Maven初学者最开始执行的Maven命令可能就是mvn archetype:generate,这实际上就是让maven-archetype-plugin生成一个很简单的项目骨架,帮助开发者快速上手。可能也有人看到一些文档写了mvn archetype:create,但实际上create目标已经被弃用了,取而代之的是generate目标,该目标使用交互式的方式提示用户输入必要的信息以建立项目,体验更好。 maven-archetype-plugin还有一些其余目标帮助用户本身定义项目原型,例如你由一个产品须要交付给不少客户进行二次开发,你就能够为他们提供一个Archtype,帮助他们快速上手。

maven-assembly-plugin

http://maven.apache.org/plugins/maven-assembly-plugin/

maven-assembly-plugin的用途是制做项目分发包,该分发包可能包含了项目的可执行文件、源代码、readme、平台脚本等等。 maven-assembly-plugin支持各类主流的格式如zip、tar.gz、jar和war等,具体打包哪些文件是高度可控的,例如用户能够按文件级别的粒度、文件集级别的粒度、模块级别的粒度、以及依赖级别的粒度控制打包,此外,包含和排除配置也是支持的。maven-assembly- plugin要求用户使用一个名为assembly.xml的元数据文件来表述打包,它的single目标能够直接在命令行调用,也能够被绑定至生命周期。

maven-dependency-plugin

http://maven.apache.org/plugins/maven-dependency-plugin/

maven-dependency-plugin最大的用途是帮助分析项目依赖,dependency:list可以列出项目最终解析到的依赖列表,dependency:tree能进一步的描绘项目依赖树,dependency:analyze能够告诉你项目依赖潜在的问题,若是你有直接使用到的却未声明的依赖,该目标就会发出警告。maven-dependency-plugin还有不少目标帮助你操做依赖文件,例如dependency:copy-dependencies能将项目依赖从本地Maven仓库复制到某个特定的文件夹下面。

maven-enforcer-plugin

http://maven.apache.org/plugins/maven-enforcer-plugin/

在一个稍大一点的组织或团队中,你没法保证全部成员都熟悉Maven,那他们作一些比较愚蠢的事情就会变得很正常,例如给项目引入了外部的 SNAPSHOT依赖而致使构建不稳定,使用了一个与你们不一致的Maven版本而常常抱怨构建出现诡异问题。maven-enforcer- plugin可以帮助你避免之类问题,它容许你建立一系列规则强制你们遵照,包括设定Java版本、设定Maven版本、禁止某些依赖、禁止 SNAPSHOT依赖。只要在一个父POM配置规则,而后让你们继承,当规则遭到破坏的时候,Maven就会报错。除了标准的规则以外,你还能够扩展该插件,编写本身的规则。maven-enforcer-plugin的enforce目标负责检查规则,它默认绑定到生命周期的validate阶段。

maven-help-plugin

http://maven.apache.org/plugins/maven-help-plugin/

maven-help-plugin是一个小巧的辅助工具,最简单的help:system能够打印全部可用的环境变量和Java系统属性。help:effective-pom和help:effective-settings最为有用,它们分别打印项目的有效POM和有效settings,有效POM是指合并了全部父POM(包括Super POM)后的XML,当你不肯定POM的某些信息从何而来时,就能够查看有效POM。有效settings同理,特别是当你发现本身配置的 settings.xml没有生效时,就能够用help:effective-settings来验证。此外,maven-help-plugin的describe目标能够帮助你描述任何一个Maven插件的信息,还有all-profiles目标和active-profiles目标帮助查看项目的Profile。

maven-release-plugin

http://maven.apache.org/plugins/maven-release-plugin/

maven-release-plugin的用途是帮助自动化项目版本发布,它依赖于POM中的SCM信息。release:prepare用来准备版本发布,具体的工做包括检查是否有未提交代码、检查是否有SNAPSHOT依赖、升级项目的SNAPSHOT版本至RELEASE版本、为项目打标签等等。release:perform则是签出标签中的RELEASE源码,构建并发布。版本发布是很是琐碎的工做,它涉及了各类检查,并且因为该工做仅仅是偶尔须要,所以手动操做很容易遗漏一些细节,maven-release-plugin让该工做变得很是快速简便,不易出错。maven-release-plugin的各类目标一般直接在命令行调用,由于版本发布显然不是平常构建生命周期的一部分。

maven-resources-plugin

http://maven.apache.org/plugins/maven-resources-plugin/

为了使项目结构更为清晰,Maven区别对待Java代码文件和资源文件,maven-compiler-plugin用来编译Java代码,maven-resources-plugin则用来处理资源文件。默认的主资源文件目录是src/main/resources,不少用户会须要添加额外的资源文件目录,这个时候就能够经过配置maven-resources-plugin来实现。此外,资源文件过滤也是Maven的一大特性,你能够在资源文件中使用${propertyName}形式的Maven属性,而后配置maven-resources-plugin开启对资源文件的过滤,以后就能够针对不一样环境经过命令行或者Profile传入属性的值,以实现更为灵活的构建。

maven-surefire-plugin

http://maven.apache.org/plugins/maven-surefire-plugin/

多是因为历史的缘由,Maven 2/3中用于执行测试的插件不是maven-test-plugin,而是maven-surefire-plugin。其实大部分时间内,只要你的测试类遵循通用的命令约定(以Test结尾、以TestCase结尾、或者以Test开头),就几乎不用知晓该插件的存在。然而在当你想要跳过测试、排除某些测试类、或者使用一些TestNG特性的时候,了解maven-surefire-plugin的一些配置选项就颇有用了。例如 mvn test -Dtest=FooTest 这样一条命令的效果是仅运行FooTest测试类,这是经过控制maven-surefire-plugin的test参数实现的。

build-helper-maven-plugin

http://mojo.codehaus.org/build-helper-maven-plugin/

Maven默认只容许指定一个主Java代码目录和一个测试Java代码目录,虽然这实际上是个应当尽可能遵照的约定,但偶尔你仍是会但愿可以指定多个源码目录(例如为了应对遗留项目),build-helper-maven-plugin的add-source目标就是服务于这个目的,一般它被绑定到默认生命周期的generate-sources阶段以添加额外的源码目录。须要强调的是,这种作法仍是不推荐的,由于它破坏了 Maven的约定,并且可能会遇到其余严格遵照约定的插件工具没法正确识别额外的源码目录。

build-helper-maven-plugin的另外一个很是有用的目标是attach-artifact,使用该目标你能够以classifier的形式选取部分项目文件生成附属构件,并同时install到本地仓库,也能够deploy到远程仓库。

exec-maven-plugin

http://mojo.codehaus.org/exec-maven-plugin/

exec-maven-plugin很好理解,顾名思义,它能让你运行任何本地的系统程序,在某些特定状况下,运行一个Maven外部的程序可能就是最简单的问题解决方案,这就是exec:exec的用途,固然,该插件还容许你配置相关的程序运行参数。除了exec目标以外,exec-maven-plugin还提供了一个java目标,该目标要求你提供一个mainClass参数,而后它可以利用当前项目的依赖做为classpath,在同一个JVM中运行该mainClass。有时候,为了简单的演示一个命令行Java程序,你能够在POM中配置好exec-maven-plugin的相关运行参数,而后直接在命令运行mvn exec:java 以查看运行效果。

jetty-maven-plugin

http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin

在进行Web开发的时候,打开浏览器对应用进行手动的测试几乎是没法避免的,这种测试方法一般就是将项目打包成war文件,而后部署到Web容器中,再启动容器进行验证,这显然十分耗时。为了帮助开发者节省时间,jetty-maven-plugin应运而生,它彻底兼容 Maven项目的目录结构,可以周期性地检查源文件,一旦发现变动后自动更新到内置的Jetty Web容器中。作一些基本配置后(例如Web应用的contextPath和自动扫描变动的时间间隔),你只要执行 mvn jetty:run ,而后在IDE中修改代码,代码经IDE自动编译后产生变动,再由jetty-maven-plugin侦测到后更新至Jetty容器,这时你就能够直接测试Web页面了。须要注意的是,jetty-maven-plugin并非宿主于Apache或Codehaus的官方插件,所以使用的时候须要额外的配置settings.xml的pluginGroups元素,将org.mortbay.jetty这个pluginGroup加入。

versions-maven-plugin

http://mojo.codehaus.org/versions-maven-plugin/

不少Maven用户遇到过这样一个问题,当项目包含大量模块的时候,为他们集体更新版本就变成一件烦人的事情,到底有没有自动化工具能帮助完成这件事情呢?(固然你可使用sed之类的文本操做工具,不过不在本文讨论范围)答案是确定的,versions-maven- plugin提供了不少目标帮助你管理Maven项目的各类版本信息。例如最经常使用的,命令 mvn versions:set -DnewVersion=1.1-SNAPSHOT 就能帮助你把全部模块的版本更新到1.1-SNAPSHOT。该插件还提供了其余一些颇有用的目标,display-dependency- updates能告诉你项目依赖有哪些可用的更新;相似的display-plugin-updates能告诉你可用的插件更新;而后use- latest-versions能自动帮你将全部依赖升级到最新版本。最后,若是你对所作的更改满意,则可使用 mvn versions:commit 提交,不满意的话也可使用 mvn versions:revert 进行撤销。

更多详情请参考https://maven.apache.org/plugins/

7、常见问题

7.一、dependencies和dependencyManagement,plugins和pluginManagement的区别?

dependencyManagement是表示依赖jar包的声明,即你在项目中的dependencyManagement下声明了依赖,maven不会加载该依赖,dependencyManagement声明能够被继承。

dependencyManagement的一个使用案例是当有父子项目的时候,父项目中能够利用dependencyManagement声明子项目中须要用到的依赖jar包,以后,当某个或者某几个子项目须要加载该插件的时候,就能够在子项目中dependencies节点只配置 groupId 和 artifactId就能够完成插件的引用。

dependencyManagement主要是为了统一管理插件,确保全部子项目使用的插件版本保持一致,相似的仍是plugins和pluginManagement。

7.二、不能添加Servlet

一、修改项目文件.iml,添加以下内容

<sourceRoots>
<root url="file://$MODULE_DIR$/src/main/java" />
</sourceRoots>

二、添加Servlet-api核心包

8、综合示例

这里使用Maven+多模块项目+IDEA+Git+MySQL完成一个简单的综合示例,完成一个TaskList项目,实现任务管理功能。

8.一、建立项目

8.二、建立数据数据库与表

建立表

添加数据

8.三、编写实体模块

8.四、编写工具模块

8.五、数据访问模块

添加依赖

修改Pom

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>TaskList</artifactId>
        <groupId>com.zhangguo.tasklist</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>taskList-dao</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.zhangguo.tasklist</groupId>
            <artifactId>taskList-entities</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.zhangguo.tasklist</groupId>
            <artifactId>taskList-utils</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- JUnit单元测试工具 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
    </dependencies>

</project>

编写任务表的数据访问类

package com.zhangguo.tasklist.dao;

import com.zhangguo.tasklist.entities.Task;
import com.zhangguo.tasklist.utils.JDBCUtils;

import java.util.List;

public class TaskDao {

    /**得到全部任务清单*/
    public List<Task> getAllTasks(){
        return JDBCUtils.queryForList("select id,title,status from task",Task.class);
    }

}

8.六、单元测试

8.七、服务模块

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>TaskList</artifactId>
        <groupId>com.zhangguo.tasklist</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>taskList-services</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.zhangguo.tasklist</groupId>
            <artifactId>taskList-dao</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.zhangguo.tasklist</groupId>
            <artifactId>taskList-entities</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

代码:

8.八、Web项目
pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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">
    <parent>
        <artifactId>TaskList</artifactId>
        <groupId>com.zhangguo.tasklist</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>taskList-ui</artifactId>
    <packaging>war</packaging>

    <name>taskList-ui Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.zhangguo.tasklist</groupId>
            <artifactId>taskList-utils</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.zhangguo.tasklist</groupId>
            <artifactId>taskList-services</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- Servlet核心包 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!--JSP -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <finalName>taskList-ui</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

Servlet:

package com.zhangguo.tasklist.ui.controllers;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import com.zhangguo.tasklist.services.ITaskService;
import com.zhangguo.tasklist.services.TaskService;
import com.zhangguo.tasklist.utils.R;

@WebServlet("/TaskServlet")
public class TaskServlet extends HttpServlet {

    ITaskService service=new TaskService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        request.setCharacterEncoding("utf-8");

        String json=R.ok(service.queryAllTask()).Json();

        response.getWriter().write(json);
    }
}

项目结构:

目录结构:

运行结果:

8.九、Git同步到远程仓库

Git位置:https://git.coding.net/zhangguo5/TaskListSys.git

9、视频

https://www.bilibili.com/video/av31623453/

10、做业

一、请下载、安装配置好maven,在控制台输出版本号

二、请配置本地仓库路径与远程仓库的镜像

三、请在命令行模式下新建一个maven普通项目,导入到IDEA中,运行,打包输出结果

四、请在命令行模式下新建一个webapp项目,使用jetty运行,要求输出服务器时间,注意添加相应的javaEE依赖

参数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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zhangguo</groupId>
    <artifactId>ssm</artifactId>
    <version>0.0.1</version>
    <packaging>war</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.0.RELEASE</spring.version>
    </properties>
    <dependencies>
        <!--Spring框架核心库 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- Spring MVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- aspectJ AOP 织入器 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>
        <!--mybatis-spring适配器 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!--Spring java数据库访问包,在本例中主要用于提供数据源 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--mysql数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <!--log4j日志包 -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.6.1</version>
        </dependency>
        <!-- mybatis ORM框架 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!-- JUnit单元测试工具 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
        <!--c3p0 链接池 -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <!-- JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- Servlet核心包 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!--JSP -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <!-- jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.5.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.2</version>
        </dependency>
        <!--JSR303 -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.2.Final</version>
        </dependency>
        <!--文件上传 -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!-- FreeMarker -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.23</version>
        </dependency>
    </dependencies>
</project>
View Code

五、我的项目功能需求列表,下周五前提交

下载地址

六、将考试的两个项目修改为Maven项目,管理全部的包,测试运行经过

七、预习Git,安装好环境,到coding.net上注册一个账号

八、完成任务指导手册中全部的理论题

九、将考试中的两个项目合并成一个多模块的Maven项目,两个ui层,一个使用jstl一个使用纯HTML

十、熟练使用maven中经常使用的指令,自定义一个Maven项目,编写测试用例,部署到本地仓库并在其它项目中引用

2018-09-21

十一、根据任务列表示例完成一个汽车管理系统CarSystem(车名,颜色,速度,车牌,详细描述),实现增长、修改、删除、查询功能;最终完成的项目要求同步到远程仓库;

十二、完成Git中协同开发的8个场景,2人以上交叉扮演组长与组员

1三、完成任务指导手册中的理论题

1四、完成我的项目的功能需求,提交给学习委员

1五、完成我的项目的数据库设计,提交给组长,组长给学习委员

1六、补考的同窗请下周二上课前完成试题,并经过学习委员的检查

相关文章
相关标签/搜索