初识Maven(一)

官方文档:http://maven.apache.org/guides/index.html

Maven 项目的目录结构

src
    -main
        -java
            -package
        -resources
    -test
        -java
            -package
        -resources

Maven 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/maven-v4_0_0.xsd">

    <!--指定了当前pom的版本,必须的-->
    <modelVersion>4.0.0</modelVersion>

    <!--groupId 项目的包名(反写的公司网址+项目名)-->
    <groupId>spring-boot-imooc-demo</groupId>

    <!--artifactId 模块名(项目名+模块名)-->
    <artifactId>lxd-springboot-demo</artifactId>

    <!--项目打包方式  
        默认是jar
        其他方式:war、zip、pom
        -->
    <packaging>jar</packaging>

    <!--版本,0.0.1-SNAPSHOT快照版本
        | 第一个0表示大版本号
        | 第二个0表示分支版本号
        | 第三个1表示小版本号
        SNAPSHOT (快照)
        ALPHA (内部测试)
        BETA (公测)
        RELEASE (稳定)
        GA (正式发布)
    -->
    <version>0.0.1-SNAPSHOT</version>

    <!-- 项目描述名 -->
    <name>lxd-springboot-demo</name>

    <!-- 项目地址 -->
    <url></url>

    <!-- 项目描述 -->
    <description></description>

    <!-- 开发者 -->
    <developers></developers>

    <!-- 许可证信息(如开源框架) -->
    <licenses></licenses>

    <!-- 组织信息 -->
    <organization></organization>

    <!-- 依赖列表 -->
    <dependencies>

        <dependency>
                <groupId></groupId>
                <artifactId></artifactId>
                <version></version>
                <type></type>
                <scope></scope>
                <!-- 设置依赖是否可选true/false -->
                <optional></optional>
                <!-- 排除依赖传递列表 -->
                <exclusions>
                    <exclusion>
                    </exclusion>
                </exclusions>
        </dependency>

    </dependencies>

    <!-- 依赖的管理 抽象出一个父模块(共同使用的模块),用来供子模块来继承 -->
    <dependencyManagement>

        <dependencies>

            <dependency></dependency>

        </dependencies>

    </dependencyManagement>

    <build>
        <!-- 插件列表 -->
        <plugins>

            <plugin>
                <groupId></groupId>
                <artifactId></artifactId>
                <version></version>
            </plugin>

        </plugins>

    </build>

    <!--在子模块中对父模块的继承-->
    <parent></parent>

    <!-- 聚合运行多个的模块,集中进行编译 -->
    <modules>
        <module></module>
    </modules>

</project>

Maven 常用构建命令

mvn
- -v 查看maven版本
- compile 编译
- test 测试
- package 打包,在target目录下生成一个jar包/war包
- clean 删除target目录
- install 安装jar包到本地仓库中

创建目录的两种方式:

  1. archetype:generate 按照提示进行选择
  2. archetype:generate -DgroupId=组织名(公司网址的反写+项目名)

    -DartifactId=项目名-模块名

    -Dversion=版本号

    -Dpackage=代码所在的包名

Maven中的坐标和仓库

坐标

构件

仓库

本地仓库和远程仓库

镜像仓库

<!--阿里镜像仓库-->
<mirror> 
    <!--唯一标识-->
    <id>alimaven</id>
    <!--镜像名称-->
    <name>aliyun maven</name>
    <!--镜像地址-->
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
    <mirrorOf>central</mirrorOf>          
</mirror> 

本地仓库存储位置

 <!-- 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/m2/repository/</localRepository>

完整的项目构建过程:

清理、编译、测试、打包、集成测试、验证、部署

clean、validate、compile、test、package、verify、install、deploy

Maven生命周期:

  1. clean 清理项目
    • per-clean 执行清理前的工作
    • clean 清理上一次构建生成的所有文件
    • post-clean 执行清理后的文件
  2. default 构建项目(最核心)

    • compile
    • test
    • package
    • install
  3. site 生成项目站点

    • pre-site 在生成项目站点前要完成的工作
    • site 生成项目的站点文档
    • post-site 在生成项目站点后要完成的工作
    • site-deploy 发布生成的站点到服务器上

Maven插件

<build>

    <plugins>
        <!--Spring Boot的Maven插件 可以将项目打包成一个可执行jar -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

                <!--Maven的一个插件,将源代码打包 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.4</version>
                <executions>
                        <execution>
                            <!--package阶段运行source插件-->  
                            <phase>package</phase>
                        </execution>
                        <goals>
                            <!--运行目标-->  
                            <goal>jar-no-fork</goal>
                        </goals>
                </executions>
        </plugin>

    </plugins>

</build>

Maven pom.xml常用元素介绍

pom.xml 依赖范围

例如:

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

三种classpath:
1. 编译
2. 测试
3. 运行

Dependency Scope:
- compile 默认的范围,编译测试运行都有效
- provided 在编译和测试时有效
- runtime 在运行时和测试时有效
- test 只在测试时有效
- system 编译和测试时有效,与本机系统相关联,可移植性差
- import 导入的范围,它只使用在dependencyManagement中,表示从其他的pom中导入dependency的配置(继承过来的依赖)

Maven依赖传递

修改Maven默认JDK编译版本
在setting.xml中,找到profile 添加如下代码

<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>

依赖冲突

解决原则:

1.短路优先

A->B->C-X(jar)

A->D->X(jar) 优先解析

2.先声明先优先

如果路径长度相同,则谁先声明,先解析谁

聚合和继承
- 聚合

解决方法:

1.步骤一

新建一个项目,将打包方式修改为pom

<!-- 打包方式为pom -->
<packaging>pom</packaging>
  1. 步骤二

将项目聚合到modules里面,集中打包


<!-- 聚合运行多个的模块,集中进行编译 -->
    <modules>
        <module>../hongxing-bge</module>
        <module>../hongxing-nange</module>
        <module>../hongxing-shanji</module>
    </modules>
  • 继承

解决方法
1. 步骤一
新建一个项目hongxing-parent,pom中添加

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <!--可抽象出来版本放到最上面-->
            <version>4.9</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
<dependencyManagement>

可将4.9抽取出来,替换

<properties>
    <junit.version>4.9</junit.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <!--可抽象出来版本,类似EL表达式一样调用-->
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
<dependencyManagement>

将打包方式修改为pom

<packaging>pom</packaging>
  1. 步骤二
    在hongxing-bge中去掉junit的依赖版本(标签),添加
<parent>
    <groupId>com.hongxing</groupId>
    <artifactId>hongxing-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

使用Maven创建Web项目

  1. 添加servlet依赖
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <!-- 只在编译时和测试时运行 -->
        <scope>provided</scope>
    </dependency>
</dependencies>
  1. 添加Jetty插件
<build>
    <finalName></finalName>
    <plugins>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>8.1.16v20140903</version>
        <executions>
            <execution>                
                <!--在打包成功后使用jetty:run来运行jetty服务-->                  
                <phase>package</phase>
            </execution>
            <goals>
                <!--运行目标-->  
                <goal>run</goal>
            </goals>
        </executions>
    </plugins>
</build>
  1. 项目右键,maven-build 输入:jetty:run
    此时jetty容器启动

    4.如果是tomcat容器,插件

<build>
    <!--名称作用:启动成功后,访问的路径为
    |http://localhost8080/webdemo
    -->
    <finalName>webdemo</finalName>
    <plugins>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <executions>
            <execution>                
                <!--在打包成功后使用tomcat:run来运行tomcat服务-->                
                <phase>package</phase>
            </execution>
            <goals>
                <!--运行目标-->  
                <goal>run</goal>
            </goals>
        </executions>
    </plugins>
</build>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值