1.maven是什么?
2.怎么使用?
3.原理是什么?
4.解决了什么问题?
本期内容必须联网,并且网络质量要很好
maven项目默认是没有tomcat依赖的
一、Maven简单介绍
Apache Maven是个项目管理和自动构建工具,基于项目对象模型(POM)的概念。
作用:完成项目的相关操作,如:编译,构造,单元测试,安装,网站生成和基于Maven部署项目
二、 Maven安装与配置
Maven3.5.0下载地址1、Maven3.5.0下载Maven3.5.0下载地址
2、选择左侧Download
3、 点击箭头所指的链接进行下载
?4、下载完成后,选择一个路径进行解压(解压到非中文的文件目录下)
2.然后配置path环境变量
1、步骤:此电脑--属性--高级系统设置--环境变量--新建
2、新增MAVEN_HOME:MAVEN_HOME = E:\maven\apache-maven-3.5.0-bin\apache-maven-3.5.0-bin\apache-maven-3.5.0(这里看你自己把它存放在哪)
3、?在path中添加Maven路径(path = %MAVEN_HOME%\bin?)
4、然后win+R 运行cmd 输入 mvn -version,如图所示则配置成功
?如果有以上的显示信息,则表示Maven3.5.0安装成功!
3.配置本地仓库及私有仓库
maven文件--conf文件--settings.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>E:\mavenCangku</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>alimaven</id> ? ? ? <mirrorOf>central</mirrorOf> ? ? ? <name>aliyun maven</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> ? ? --> ? </profiles>
? <!-- activeProfiles ? ?| List of profiles that are active for all builds. ? ?| ? <activeProfiles> ? ? <activeProfile>alwaysActiveProfile</activeProfile> ? ? <activeProfile>anotherAlwaysActiveProfile</activeProfile> ? </activeProfiles> ? --> </settings> ?
?ctr+f找到localRepository把它最后一行给扣皮出来
可以设置一个自己盘符,为本地仓库(maven文件解压路径)
例:<localRepository>E:\mavenCangku</localRepository>
设置私有仓库↓:
使用阿里云私服,镜像仓库(ctrl+f搜索mirrors)
?<mirror> ? ? ? ? ? ? <id>alimaven</id> ? ? ? ? ? ? <mirrorOf>central</mirrorOf> ? ? ? ? ? ? <name>aliyun maven</name> ? ? ? ? ? ? <url>http://maven.aliyun.com/nexus/content/groups/public/</url> ? ? ? ? </mirror>
三、原理
当配置结束后
①、通过pom文件的坐标优先读取本地仓库的jar包,如果曾经下载过,那么直接自动导入到当前项目中用
②、如果本地仓库没有,那么就会到阿里云镜像服务器中去找开发需要的jar包,找到了自动下载到本地仓库,并且会引入到项目
③、如果阿里云镜像服务器没有,那么会到国外的maven中央仓库去下载,如果有直接下到本地仓库,并且引入项目使用
④、如果国外maven中央仓库都没有找到,那么肯定是代码问题,会报错
四、解决的问题
1.使用jar方式开发项目,需要一个个的寻找jar包
2.jar直接的冲突问题
比如:com.jdbc.mysql.Driver在工程中出现俩次,就会使工程运行不了,意味着存在jar冲突
五、Maven的使用案例
? ? 目标:在maven中将服务跑动起来,意味着需要在maven项目中导入tomcat相关的jar依赖,
或者说servlet相关的依赖
1、eclipse中使用maven
首先在eclipse进行配置
? ? ? ?①? 在window里点击perferences进行查找maven
②点击列表中的Installation
?⑥点击add
?⑦点击勾选
?⑧然后配置用户建立
?⑨本地仓库配置?
?2.创建Maven项目
右键新建一个maven项目
?
?Internal优先读取本地
?Next
group id:组织id,一般就是公司域名反写
artifact id:项目名字
version:版本
package:java包名
3.打印Hello World
?右键项目更多
?修改版本:
还需要修改xml文件
??把2.3修改成3.1
<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">
<display-name>Archetype Created Web Application</display-name>
</web-app>
?4.配置jdk
右键Bulid path
?
?5. 在maven中服务器跑起来(maven项目默认是没有tomcat依赖的)
所以需要自己导入tomcat相关依赖
在https://mvnrepository.com/popular去寻找
?点进去第一个找到4.0.0
?复制到pom.xml(设置了servlet插件)
<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.zxy</groupId> ? <artifactId>SHH_maven</artifactId> ? <packaging>war</packaging> ? <version>0.0.1-SNAPSHOT</version> ? <name>SHH_maven Maven Webapp</name> ? <url>http://maven.apache.org</url> ? <dependencies> ? <dependency> ? ? ? ? <groupId>javax.servlet</groupId> ? ? ? ? <artifactId>javax.servlet-api</artifactId> ? ? ? ? <version>4.0.0</version> ? ? ? ? <scope>provided</scope> ? ? </dependency> ? </dependencies> ? <build> ? ? <finalName>SHH_maven</finalName> ? </build> </project> ?
接下来我们类就有了servlet插件
?可以用坐标的方式使用项目:
<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.zxy</groupId> ? <artifactId>SHH_maven</artifactId> ? <packaging>war</packaging> ? <version>0.0.1-SNAPSHOT</version> ? <name>SHH_maven Maven Webapp</name> ? <url>http://maven.apache.org</url>
? <!-- 定义版本号 --> ? <properties> ? ? <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> ? ? <maven.compiler.source>1.8</maven.compiler.source> ? ? <maven.compiler.target>1.8</maven.compiler.target> ? ? <maven.compiler.plugin.version>3.7.0</maven.compiler.plugin.version>
? ? <!--添加jar包依赖--> ? ? <!--mysql--> ? ? <mysql.version>5.1.44</mysql.version> ? ? <!--5.其他--> ? ? <junit.version>4.12</junit.version> ? ? <servlet.version>4.0.0</servlet.version> ? ? <jackson.version>2.9.3</jackson.version> ? ? <jstl.version>1.2</jstl.version> ? ? <standard.version>1.1.2</standard.version> ? ? <tomcat-jsp-api.version>8.0.47</tomcat-jsp-api.version> ? ? <commons-beanutils.version>1.9.3</commons-beanutils.version> ? ? <dom4j.version>1.6.1</dom4j.version> ? ? <jaxen.version>1.1.6</jaxen.version> ? </properties> ?? ?? ?<!-- ?使用jar包 --> ? <dependencies> ? ? <dependency> ? ? ? <groupId>jaxen</groupId> ? ? ? <artifactId>jaxen</artifactId> ? ? ? <version>${jaxen.version}</version> ? ? </dependency>
? ? <dependency> ? ? ? <groupId>dom4j</groupId> ? ? ? <artifactId>dom4j</artifactId> ? ? ? <version>${dom4j.version}</version> ? ? </dependency>
? ? <dependency> ? ? ? <groupId>commons-beanutils</groupId> ? ? ? <artifactId>commons-beanutils</artifactId> ? ? ? <version>${commons-beanutils.version}</version> ? ? </dependency>
? ? <!--mysql--> ? ? <dependency> ? ? ? <groupId>mysql</groupId> ? ? ? <artifactId>mysql-connector-java</artifactId> ? ? ? <version>${mysql.version}</version> ? ? </dependency>
? ? <dependency> ? ? ? <groupId>junit</groupId> ? ? ? <artifactId>junit</artifactId> ? ? ? <version>${junit.version}</version> ? ? ? <scope>test</scope> ? ? </dependency>
? ? <dependency> ? ? ? <groupId>javax.servlet</groupId> ? ? ? <artifactId>javax.servlet-api</artifactId> ? ? ? <version>${servlet.version}</version> ? ? ? <scope>provided</scope> ? ? </dependency>
? ? <dependency> ? ? ? <groupId>com.fasterxml.jackson.core</groupId> ? ? ? <artifactId>jackson-databind</artifactId> ? ? ? <version>${jackson.version}</version> ? ? </dependency> ? ? <dependency> ? ? ? <groupId>com.fasterxml.jackson.core</groupId> ? ? ? <artifactId>jackson-core</artifactId> ? ? ? <version>${jackson.version}</version> ? ? </dependency> ? ? <dependency> ? ? ? <groupId>com.fasterxml.jackson.core</groupId> ? ? ? <artifactId>jackson-annotations</artifactId> ? ? ? <version>${jackson.version}</version> ? ? </dependency>
? ? <dependency> ? ? ? <groupId>jstl</groupId> ? ? ? <artifactId>jstl</artifactId> ? ? ? <version>${jstl.version}</version> ? ? </dependency> ? ? <dependency> ? ? ? <groupId>taglibs</groupId> ? ? ? <artifactId>standard</artifactId> ? ? ? <version>${standard.version}</version> ? ? </dependency> ? ? <dependency> ? ? ? <groupId>org.apache.tomcat</groupId> ? ? ? <artifactId>tomcat-jsp-api</artifactId> ? ? ? <version>${tomcat-jsp-api.version}</version> ? ? </dependency>
? ? <dependency> ? ? ? <groupId>org.projectlombok</groupId> ? ? ? <artifactId>lombok</artifactId> ? ? ? <version>1.18.10</version> ? ? ? <scope>provided</scope> ? ? </dependency>
? ? <dependency> ? ? ? <groupId>com.belerweb</groupId> ? ? ? <artifactId>pinyin4j</artifactId> ? ? ? <version>2.5.0</version> ? ? </dependency>
? ? <dependency> ? ? ? <groupId>com.github.davidcarboni</groupId> ? ? ? <artifactId>encrypted-file-upload</artifactId> ? ? ? <version>2.1.0</version> ? ? </dependency> ? </dependencies>
? <build> ? ? <finalName>SHH_maven</finalName> ? </build> </project>
?结束
|