maven依赖范围及常用操作


1.依赖范围, 使用scope表示的。
    scope的值有compile, test, provided ,默认是compile

    scope:表示依赖使用的范围,也就是在maven构建项目的那些阶段中起作用。
    maven构建项目:编译, 测试 ,打包, 安装 ,部署 过程(阶段)

例:
junit的依赖范围是 test

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>
  
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>provided</scope>
  </dependency>
</dependencies>


写好项目中的用到的所有依赖(jar ),必须在本地仓库中有。
没有必须通过maven下载, 包括provided的都必须下载。

你在servlet需要继承HttpServlet( provided) , 你使用的HttpServlet是maven仓库中的。

当你的写好的程序, 放到 tomat服务器中运行时, 此时你的程序中不包含servlet的jar
因为tomcat提供了 servlet的.jar

2.maven常用操作
  maven的属性设置
           <properties> 设置maven的常用属性
  maven的全局变量
        自定义的属性:在<properties> 通过自定义标签声明变量(标签名就是变量名)
           在pom.xml文件中的其它位置,使用 ${标签名} 使用变量的值

  自定义全局变量一般是定义依赖的版本号, 当你的项目中要使用多个相同的版本号,
  先使用全局变量定义, 在使用${变量名}

例:

properties>
  <spring.version>4.3.10.RELEASE</spring.version>
</properties>
<!--引用全局变量-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${spring.version}</version>
</dependency>

3.指定资源位置资源插件

    src/main/java 和 src/test/java 这两个目录中的所有*.java 文件会分别在 comile 和 test-comiple 阶段被编译

    编译结果分别放到了 target/classes 和 targe/test-classes 目录中;

    但是这两个目录中的其他文件都会被忽略掉,如果需要把 src 目录下的文件包放到 target/classes 目录,作为输出的 jar 一部分。

    那么需要指定资源文件位置。以下内容放到标签中。

<build>
    <resources>
      <resource>
        <directory>src/main/java</directory><!--所在的目录-->
        <includes><!--包括目录下的.properties,.xml 文件都会扫描到-->
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <!--filtering 选项 false 不启用过滤器, *.property 已经起到过滤的作用了 -->
        <filtering>false</filtering>
      </resource>
    </resources>
</build>

     作用: 默认没有使用resources的时候, maven执行编译代码时, 会把src/main/resource目录中的文件拷贝到target/classes目录中。

4.Maven 系统采用的变量:

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!--项目构建使用的编码,避免中文乱码-->
<maven.compiler.source>1.8</maven.compiler.source><!--源码编译 jdk 版本-->
<maven.compiler.target>1.8</maven.compiler.target><!--运行代码的 jdk 版本-->
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><!--生成报告的编码-->
</properties>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

输出机器

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值