Maven笔记

maven地址配置

web网站搜索
https://maven.aliyun.com/repository/public
https://search.maven.org/artifact/org.bitbucket.javatek/jdbc/0.5/jar

配置步骤

 <mirror>
      <id>aliyun</id>
      <mirrorOf>*</mirrorOf>
      <name>aliyun maven</name>
      <url>https://maven.aliyun.com/repository/central</url>
    </mirror>
  </mirrors>

配置xml

<?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:\maven\maven-repo</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
     | 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>aliyun</id>
      <mirrorOf>*</mirrorOf>
      <name>aliyun maven</name>
      <url>https://maven.aliyun.com/repository/central</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>
      <id>jdk-1.8</id>
      <activation>
	  <activeByDefault>true</activeByDefault>
        <jdk>1.8</jdk>
      </activation>
		<properties>
		<maven.compiler.sour>1.8</maven.compiler.sour>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
      		</properties>
    </profile>
    <!-- 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 tocompilerVersion 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>
    -->
  </profiles>

  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
</settings>

中央仓库

是一个网络仓库,存储jar包和maven插件
阿帕奇1
阿帕奇2
带图形界面
上面两个是一样的,相当于ftp,第3个带图形界面。(3个都是外国网址)

本地仓库

从中央仓库下载的jar包的存放位置,也是一个仓库,只不过是存放在本地电脑上

镜像仓库

对某一仓库做的镜像 阿里云提供的nexus私服(不能浏览,但能用)
http://maven.aliyun.com/nexus/content/groups/public/

私服

局域网内部搭建的maven服务器(公司不能连外网,只有部分能连外网)

单词

repository 仓库
archetype 原型、骨架(模板)
artifact 手工制品,成品(项目/jar/war)

配置环境变量

Windows

Maven依赖于JAVA_HOME

echo %JAVA_HOME% echo $JAVA_HOME

Maven环境变量:
M2_HOME=Maven安装目录
PATH=%M2_HOME%/bin
测试:mvn ­-version(cmd命令)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Linux/Mac OS

编辑/home/soft01/.bashrc文件,在最后一行添加内容:

cd /home/soft01
 vi .bashrc 
 export M2_HOME=/home/soft01/maven/apache­maven­3.2.5 
 export PATH=$PATH:$M2_HOME/bin

使修改立即生效:

source .bashrc

测试:mvn ­-version
成功的界面:

Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014­12­ 15T01:29:23+08:00) Maven home: /home/soft01/maven/apache­maven­3.2.5 Java version: 1.7.0_80, vendor: Oracle Corporation Java home: /usr/lib/jvm/java­7­oracle/jre Default locale: zh_CN, platform encoding: UTF­8 OS name: "linux", version: "3.16.0­30­generic", arch: "i386", family: "unix"

配置本地仓库

本地仓库的默认位置

~/.m2/repository

修改本地仓库的位置

编辑conf/setting.xml

<localRepository>D:\maven\maven­repo</localRepository>

测试:

cd maven/maven­projects 
mvn archetype:generate #创建maven项目

提示:选择创建项目时使用的模板archetype:
按回车 输入groupId,一般为组织域名反向+项目名:com.itany.ums
输入artifactId:test 版本:按回车
包名:按回车 确认:按回车

配置镜像仓库

编辑conf/setting.xml

<mirrors> 
<mirror> 
<id>aliyun</id> 
<mirrorOf>*</mirrorOf> <!­­ 所有访问都使用该镜像仓库 ­­> 
<name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> 
</mirror> 
</mirrors>

配置Maven的JDK版本

默认使用maven创建的项目使用的是jdk1.5
修改maven默认使用的jdk版本,编辑conf/setting.xml,
在profiles标签中添加如下内容:

<profile> 
<id>jdk‐1.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>

创建Maven项目

1. Java项目的创建

2. 目录结构

Java工程约定的目录结构如下:
在这里插入图片描述

基本操作 步骤:

1. 创建Java项目

mvn archetype:generate

指定的信息如下:

archetype:(maven­archetype­quickstart) #使用骨架创建java项目
 groupId:com.itany.study #指定组织域名反向+项目名 artifactId:sms 
 #指定模块名称 
 version:1.0.1 #版本 
 package:com.itany.study #包名

2. 执行maven操作

cd sms #先切换到项目所在的目录 
mvn compile
 mvn clean compile test #可以同时执行多个命令

自定义Java类

2. Web项目的创建

目录结构

Web项目的约定目录结构如下:
在这里插入图片描述

基本操作

步骤:1. 创建web项目

mvn archetype:generate

指定的信息如下:

archetype:(maven­archetype­webapp) #使用模板创建web项目 .....

执行maven操作

mvn clean compile package

发布方式1:使用外部的tomcat 将shop.war包复制到webapps目录下,然后启动tomcat

cd tomcat.../bin 
./startup.sh

方式2:使用maven的tomcat插件

mvn tomcat:run

访问
http://localhost:8080/shop

Maven命令

1. 常用命令

在这里插入图片描述
注意: 所有的命令都要跟在mvn的后面,
如mvn archetype 所有的命令都必须在某个项目的根目录
多个命令可以一起执行

生命周期

一个项目的构建build的过程通常包括:清理、编译、测试、打包、集成测试、部署等 Maven定义了三套生命周期:clean、default、site 每个生命周期之间是互相独立,每个生命周期内都包含多个阶段,这些阶段是有顺序的,后续的阶段 必须等待前面的阶段执行后才能执行 default生命周期包含:compile、test、package

pom.xml文件

  1. 简介pom:project object model 项目对象模型 pom.xml是Maven的核心配置文件 一个Maven项目有且只有一个pom.xml文件,该文件必须在项目的根目录下

坐标详解

Maven坐标是通过一些元素来定义:groupId、artifactId、version
在这里插入图片描述
dependency
基本配置

如何查找一个jar包的坐标?(网址)

maven1

maven2
或阿里云,失效了
国内阿里云

<dependency> 
<groupId>mysql</groupId> 
<artifactId>mysql­connector­java</artifactId> 
<version>5.1.38</version> 
</dependency> <dependency> 
<groupId>commons­fileupload</groupId> 
<artifactId>commons­fileupload</artifactId> 
<version>1.3.1</version> 
</dependency> 
<dependency> 
<groupId>log4j</groupId>
 <artifactId>log4j</artifactId> 
 <version>1.2.17</version> 
</dependency>

scope作用域

表示依赖的作用域,用来配置依赖的jar包可使用的范围
在这里插入图片描述

properties 全局属性

一般情况下用于定义全局的jar包版本

<properties> 
<project.build.sourceEncoding>UTF­8</project.build.sourceEncoding> <fileupload.version>1.3.1</fileupload.version> 
<junit.version>4.12</junit.version> 
<mysql­connector­java.version>5.1.38</mysql­connector­java.version> <spring.version>4.3.12.RELEASE</spring.version> 
</properties> 
<dependencies> <!­­ junit ­­> <dependency> 
<groupId>junit</groupId> 
<artifactId>junit</artifactId> 
<version>${junit.version}</version> 
<scope>test</scope> 
</dependency> 
</dependencies>

repositories

用来配置当前工程使用的远程仓库
依赖查找顺序:本地仓库——>当前工程pom.xml中配置的远程仓库——>setting.xml中配置的远程仓 库

<!­­ 配置远程仓库 ­­> 
<repositories> <!­­ 有些最新的jar包,可能在中央仓库上并没有提供,此时可以使用jar包官方提供的仓库 ­ ­><repository> 
<id>spring_repo</id> 
<url>http://repo.spring.io/milestone</url> 
</repository> 
</repositories>

plugins 配置插件

是一种工具 maven­clean­plugin maven­compile­plugin

<!­­ maven­clean­plugin­­> 
<plugin> 
<groupId>org.apache.maven.plugins</groupId> 
<artifactId>maven­clean­plugin</artifactId>
 <version>2.6.1</version> 
 </plugin> <!­­ tomcat插件 ­­> <plugin> 
 <groupId>org.apache.tomcat.maven</groupId> 
 <artifactId>tomcat7­maven­plugin</artifactId> 
 <version>2.2</version> 
 <configuration>
  <path>/maven01</path> 
  <port>8888</port>
   </configuration> 
   </plugin>

超级POM

所有的pom.xml文件都继承一个父POM,这个父POM称为超级POM(Super POM、Effective POM)

Maven中的关系

1.继承

一个Maven项目可以继承自另一个Maven项目,分为称为子项目、父项目 场景:多个子项目中使用的是相同的依赖或插件,此时可以把相同的配置抽取到一个父项目中,进行 统一的管理,保持一致性 步骤:1. 将父项目的打包方式设置为pom
在这里插入图片描述
2. 在子项目中引用父项目,指定父项目的坐标

<!­­ 引用父项目,指定父项目的坐标 ­­> 
<parent>
 <groupId>com.itany.study</groupId> 
 <artifactId>parent</artifactId> 
 <version>1.0­SNAPSHOT</version> <!­­ 指定父项目的pom.xml文件的相对物理路径 ­­> 
 <relativePath>../parent/pom.xml</relativePath> 
 </parent>

问题:有时并不是父项目的所有依赖都需要被子项目继承,但是又希望能够通过父项目对依赖进行统 一管理,如版本的控制 解决:使用dependencyManagement 步骤:1. 在父项目中配置dependencyManagement 2. 在子项目中引用父项目中的依赖

Maven Web项目

步骤:1. 修改web.xml文件

<?xml version="1.0" encoding="UTF­8"?> 
<web­app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema­instance" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://xmlns.jcp.org/xml/ns/javaee/web­app_3_1.xsd" version="3.1"> 
</web­app>
  1. 配置pom.xml
<dependencies> <!­­ Java EE ­­> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet­api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp­api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <finalName>maven02</finalName> <!­­ tomcat插件 ­­> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7­maven­plugin</artifactId> <version>2.2</version> <configuration> <path>/</path> <port>8888</port> </configuration> </plugin> </plugins> </build>

使用Nexus搭建Maven私服

  1. 为什么要搭建私服 公司不提供外网给开发人员,无法使用maven访问远程仓库,此时可以在局域网内找一台能访问外网 的电脑搭建maven私服
  2. Nexus工具 用来搭建nexus工具 3. 操作步骤 步骤:1. 解压缩并安装为系统服务
nexus­2.14.2­01­bundle\nexus­2.14.2­01\bin\jsw\windows­x86­ 64\install­nexus.bat
  1. 访问
  2. http://localhost:8081/nexus
    5 登陆并配置私服 登陆账户和密码:admin/admin123
  3. 添加阿里云代理(镜像)仓库到私服中
  4. 添加本地jar到私服中
    相关文章
    简介
    下载后代开nexus的网址(已存百度云)
    http://localhost:8081/nexus
    百度网盘地址:

http://pan.baidu.com/s/1mgznhd2
下载地址

私服搭建步骤

解压
在这里插入图片描述
在这里插入图片描述
运行
在这里插入图片描述
访问localhost:8081/nexus
admin
pw:admin123
搜索
在这里插入图片描述
私服网址,把maven修改成私服网址
在这里插入图片描述
修改地址
在这里插入图片描述
配置仓库查找顺序
在这里插入图片描述
中央仓库没有的jar,自定义jar的上传,在pom用自定义的坐标
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
可一次加多个jar
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值