码迷,mamicode.com
首页 > 其他好文 > 详细

maven

时间:2017-10-05 23:37:52      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:lob   mon   官网   create   none   清理   工作   auth   eth   

1 传统web项目在开发中存在的问题

  • ①一个项目就是一个工程。
    • 如果项目非常庞大,就不适合继续使用package来划分模块。最好是每一个模块对应一个工程,利于分工协作。
    • 借助于maven,就可以将一个项目拆分成多个工程。  
  • ②项目中需要的jar包必须手动“复制”、“粘贴”到WEB-INF/lib目录下。
    • 带来的问题:同样的jar文件重复出现在不同的项目工程中,一方面浪费空间,另外也让工程比较臃肿。
    • 借助于maven,可以将jar包仅仅保存在“仓库”中,有需要使用的工程只需要引用一下,并不需要真的jar包复制过来。 
  • ③jar包需要别人替我们准备好,或者到官网上下载。
    • 不同技术的官网提供jar包下载的形式是五花八门的。
    • 有些技术的官网就是通过maven或者svn等专门的工具来提供下载的。
    • 借助于maven,我们可以以一种规范的方式下载jar包。因为所有知名框架或者第三方工具的jar包按照统一的规范存在了maven的中央仓库中。以规范的方式下载jar包,内容也是可靠的。  
  • ④一个jar包依赖的其他jar包需要自己手动加入到项目中。
    • 比如:commons-fileupload依赖于commons-io。
    • 如果所有jar包之间的依赖关系都需要程序员自己非常清楚的了解,那么就会极大的增加学习成本。
    • maven会自动的将依赖的jar包导入进来。  

 

2 maven是什么?

  • ①maven是一款服务于java平台的自动化构建工具。
    • Make-->Ant-->Maven-->Gradle
  • ②构建:
    • 概念:以“java源代码”、“框架配置文件”、“jsp”、“图片”等资源为“原材料”去生产一个可以运行的项目的过程。
      • 编译
        • java源代码-->编译-->class字节码文件-->交给JVM执行。
      • 部署
        • 一个BS项目最终运行的不是动态web工程本身,而是这个动态web工程“编译的结果”。  
      • 搭建      
  • ③构建过程中的各个环节
    • 清理:将以前编译得到的旧的class字节码文件删除,为下一次编译做准备。
    • 编译:将java程序编译成class字节码文件。
    • 测试:自动的测试,自动调用Junit程序。
    • 报告:测试程序执行的结果。
    • 打包:动态的web工程打war包,java工程打jar包。
    • 安装:maven特定的概念--将打包得到的文件复制到"仓库"指定的位置。
    • 部署:将动态web工程生成的war包复制到Servlet容器指定目录下,使其可以运行。  
  • ④自动化构建

 

3 安装maven核心程序

  • ①配置JAVA_HOME环境变量

技术分享

  • ②解压maven核心程序的压缩包

技术分享

  • ③配置MAVEN_HOME环境变量(和配置JAVA_HOME环境变量类似)

技术分享

  • 验证:运行mvn -v

技术分享

 

4 maven的核心概念

  • 约定的目录结构
  • POM
  • 坐标
  • 依赖
  • 仓库
  • 生命周期/插件/目标
  • 继承
  • 聚合

 

5 第一个maven工程

  • ①创建约定的目录结构
    • 根目录:工程名
      • src目录:源码
        • main目录:存放主程序
          • java目录:java源程序
          • resoiurces:框架配置文件或其他工具文件  
        • test目录:存放测试程序  
      • pom.xml文件  
  • ②为什么要遵守约定的目录结构?
    • maven要负责项目的自动化构建,以编译为例,maven要想自动进行编译,必须知道java源文件保存在哪里。
    • 如果我们自定义的东西想让框架或工具知道,有两种办法
      • 以配置的方式明确告诉框架
      • 遵守框架内部已经存在的约定    
    • 约定大于配置,配置大于编码。  
  • ③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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.haha</groupId>
  <artifactId>Hello</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>

  <name>Hello Maven Webapp</name>
  <url>http://maven.apache.org</url>

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

  <build>
    <finalName>Hello</finalName>
  </build>
</project>

技术分享

  • ④编写主程序代码:在src/main/java下新建com.haha.maven.Hello类
package com.haha.maven;

/**
 * 2017/10/5
 * 说明:
 */
public class Hello {
    public String sayHello(String name){
        return "你好:"+name;
    }
}  
  • ⑤编写测试代码:在src/test/java下新建com.haha.maven.HelloTest测试类
package com.haha.maven;

import org.junit.Assert;
import org.junit.Test;

/**
* 2017/10/5
* 说明:
*/
public class HelloTest {
@Test
public void demo1(){
Hello hello = new Hello();
Assert.assertEquals("你好:世界",hello.sayHello("世界"));
}

}
  • ⑥运行maven基本命令
    • mvn clean:清理
    • mvn compile:编译
    • mvn  test:测试
    • mvn package:打包 
    • mvn install:安装
    • mvn site:生成站点 

 

6 关于联网问题

  • ①maven的核心程序仅仅定义了抽象的生命周期,但是具体的工作必须由特定的插件来完成,而插件本身并不包含在maven的核心程序中。
  • ②当我们执行maven命令需要用到某些插件的时候,maven核心程序会首先到本地仓库中查找。
  • ③本地仓库的默认位置:[系统中当前用户的家目录]\.m2\repository
  • ④maven核心程序如果在本地仓库中查找不到需要的插件,它会自动连接外网,到中央仓库中下载。
  • ⑤如果此时无法连接外网,则构建失败。
  • ⑥修改默认的本地仓库位置,可以让maven核心程序到我们事先准备好的目录下查找
    • 找到maven解压目录/conf/settings.xml文件  

技术分享

 

    • 修改settings.xml文件,是的maven核心程序查找我们事先准备好的目录  
<?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.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>D:/develop/apache-maven-3.5.0/repository</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>
    -->
  </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>
        <id>nexus-aliyun</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror> 

    <!-- 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>
     -->
  </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.8</id>    
    <activation>   
        <activeByDefault>true</activeByDefault>    
        <jdk>1.8</jdk>   
    </activation>    
    <properties>   
        <maven.compiler.source>1.8</maven.compiler.source>    
        <maven.compiler.target>1.8</maven.compiler.target>    
        <maven.compiler.compilerVersion>1.8</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>

技术分享  

 

 

7 pom

  • 含义:Project Object model项目对象模型
  • pom.xml对于maven工程是核心配置文件,与构建过程相关的一切设置都在这个文件中进行设置。重要程序相当于动态web工程中的web.xml文件。

 

8 坐标

  • ①数学中的坐标
    • 在平面上,使用x和y两个向量可以唯一的确定平面上的任何一个点。
    • 在空间上,使用x、y和z三个向量可以唯一的确定空间上的任何一个点。  
  • ②maven的坐标
    • 使用下面的三个向量在仓库中唯一的定位一个maven工程
      • groupid:公司或组织的域名反转+项目名    
<groupId>com.taobao.middleware</groupId>
      • artifactid:模块的名称    
<artifactId>logger.api</artifactId>
      • version:版本号    
<version>0.1.5</version>
  • ③maven工程的坐标与仓库中路径的对应关系
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.0.RELEASE</version>
org/springframework/spring-aop/4.0.0.RELEASE/spring-aop-4.0.0.RELEASE.jar

 

9 仓库

  • ①仓库的分类:
    • 本地仓库:当前电脑上部署的仓库目录,为当前电脑上所有的maven工程服务。
    • 远程仓库:
      • 局域网(私服):搭建在局域网中,为当前局域网中的所有maven工程服务。
      • 中央仓库:搭建在Internet上,为全世界所有的maven工程服务。
      • 中央仓库的镜像:为了分担中央参考的流量,提升用户的访问速度。    
  • ②仓库中保存的内容
    • maven自身需要的插件
    • 第三方框架或工具的jar包。
    • 自己开发的maven工程  

 

10 依赖

  • ①maven解析依赖信息时会到本地仓库中查找被依赖的jar包。
    • 对于我们自己的maven工程,我们需要通过mvn install命令将工程放入到仓库中。  
  • ②依赖的范围:
    • compile范围依赖:
      • 对主程序是否有效:有效
      • 对测试程序是否有效:有效
      • 是否参与打包:是
    • test范围依赖
      • 对主程序是否有效:无效
      • 对测试程序是否有效:有效
      • 是否参与打包:否 
      • 典型的例子:junit 
    • provided范围依赖
      • 对主程序是否有效:有效
      • 对测试程序是否有效:有效
      • 是否参与打包:不参与
      • 是否参与部署:不参与 
      • 典型的例子:servlet-api.jar 

 

11 生命周期

  • ①各个构建环节执行的顺便:不能打乱顺序,必须按照指定的顺序来执行。
  • ②maven的核心程序定义了抽象的生命周期,生命周期中各个阶段的具体任务是由插件来完成的。
  • ③maven核心程序为了更好的实现自动化构建,按照这一特点执行生命周期中的各个阶段:不论现在要执行生命周期中的哪一个阶段,都是从这个生命周期的最初的位置开始的。

 

maven

标签:lob   mon   官网   create   none   清理   工作   auth   eth   

原文地址:http://www.cnblogs.com/xuweiweiwoaini/p/7629782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!