JAVAEE细细看 JavaWeb 23 - Maven

Maven

1. mac 安装 Maven

1、下载Maven
打开Maven官网下载页面:http://maven.apache.org/download.cgi
下载:apache-maven-3.6.0-bin.tar.gz
解压下载的安装包到某一目录,比如:/Users/xxx/Documents/maven
2、配置环境变量

打开终端terminel输入以下命令:

vim ~/.bash_profile   打开.bash_profile文件,在次文件中添

加设置环境变量的命令

export M2_HOME=/Users/xxx/Documents/maven/apache-maven-3.6.0
export PATH=$PATH:$M2_HOME/bin

添加之后保存并退出,执行以下命令使配置生效:

source ~/.bash_profile

3、查看配置是否生效

输入:mvn -v命令

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /Users/thor/Documents/maven/apache-maven-3.6.3
Java version: 11.0.2, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home
Default locale: zh_CN_#Hans, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.3", arch: "x86_64", family: "mac"

则配置成功。
注意:
终端每次重启, 发现zsh加载的是 ~/.zshrc文件,而 ‘.zshrc’ 文件中并没有定义任务环境变量,这样会导致每次重启终端都需要执行source ~/.bash_profile。
解决办法
在~/.zshrc文件最后,增加一行: source ~/.bash_profile

2.maven的依赖管理

maven要求,所有的jar包都放在jar包仓库中,所有的maven项目,都通过jar包仓库来使用jar包.
maven通过jar包坐标来获取jar包.
	<groupId>公司</groupId>
	<artifactId>项目或jar包名</artifactId>
	<version>版本</version>

3.Maven的仓库

本地仓库
远程仓库(私服)
中央仓库

在这里插入图片描述

4.默认配置 setting.xml

设置仓库位置

<!--配置本地仓库位置-->
    <localRepository>D:\Maven_Repository</localRepository>

设置阿里云私服

<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>central</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>

5.Maven的命令和生命周期

clean	  //清除所有编译后的资源
compile   //编译maven程序
test	  //不仅编译,而且执行测试(会把所有的测试代码都执行一遍)
package	  //不仅编译,而且测试,而且打包
install	  //不仅编译,测试,打包,而且把打包后的程序,放在了本地仓库
deploy	  //不仅编译,测试,打包,把打包后的程序放在本地仓库,而且会把打包后的程序上传到私服中.

在这里插入图片描述

6. 目录结构

在这里插入图片描述

7. 项目设置

在这里插入图片描述
windows 还需要改
在这里插入图片描述

8.使用idea创建maven项目

不使用骨架.
如果创建的是Java项目,则不需要做任何修改.
如果创建的是web项目:
	1.需要在pom.xml文件中,添加一个<packaging>war</packaging>
	2.在main文件夹下,创建一个webapp文件夹.
	3.在Project Structure界面中,点击  目标模块-》web-》加号-》生成web.xml文件
	4.把生成的"WEB-INF"文件夹,拖入webapp文件夹中.

9.Idea的Maven的VMOption配置,处理中文乱码问题(不一定可以处理的了

	-DarchetypeCatalog=internal -Dfile.encoding=GBK

10.处理tomcat中servlet 插件与项目servlet插件冲突问题

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
            <!--tomcat插件-->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.1</version>
                <configuration>
                    <!--uriEncoding:用来解决Tomcat7插件的get请求的中文乱码问题-->
                    <uriEncoding>UTF-8</uriEncoding>
                    <!--port:端口号-->
                    <port>8080</port>
                    <!--path:设置虚拟路径-->
                    <path>/day24</path>
                </configuration>
            </plugin>
        </plugins>
    </build>

11.常用的依赖

<dependencies>
        <!--druid连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
            <scope>runtime</scope>
        </dependency>
        <!--jdbcTemplate-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--servlet-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!--jsp-->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.0</version>
            <scope>provided</scope>
        </dependency>
        <!--jstl-->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!--beanUtils-->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.2</version>
            <scope>compile</scope>
        </dependency>
        <!--jackson-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.3.3</version>
        </dependency>
        <!--jedis-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.7.0</version>
        </dependency>
    </dependencies>

12.设置模板 常用tomcat7插件热建 和 常用依赖热键

settings -> 搜索live -> line Templates -> 设置模板群组 -> 写模板和设置只支持xml文件

发布了78 篇原创文章 · 获赞 30 · 访问量 3647

猜你喜欢

转载自blog.csdn.net/ZMW_IOS/article/details/104719872
今日推荐