一、 Maven在IDEA中的应用

  • 在idea中设置maven ,让idea和maven结合使用。

  • idea中内置了maven ,一般不使用内置的, 因为用内置修改maven的设置不方便。

  • 使用自己安装的maven, 需要覆盖idea中的默认的设置。

  • 让idea指定maven安装位置等信息

配置步骤:

配置当前工程的设置:

第一步:file->settings ->Build, Excution,Deployment->Build Tools–Maven
在这里插入图片描述

  • Maven Home directory:maven的安装目录

  • User Settings File :就是maven安装目录conf/setting.xml配置文件

    Local Repository :本机仓库的目录位置

第二步:Build Tools->Maven->Runner
在这里插入图片描述
VM Options : archetypeCatalog=internal
JRE: 项目的jdk
archetypeCatalog=internal , maven项目创建时,会联网下载模版文件,
比较大, 使用-DarchetypeCatalog=internal,不用下载, 创建maven项目速度快。

其次:配置以后新建工程的设置, file–other settings–Settings for New Project。步骤和上面一样

二、idea使用Maven创建JavaSE工程

第一步:新建项目

在这里插入图片描述

第二步:修改域名和修改版本号

在这里插入图片描述

第三步:新建resources,并把它设置为配置文件存放区

在这里插入图片描述

第四步:书写测试代码。此时我们在IDEA右方打开Maven,可以执行Maven的一些cmd命令

在这里插入图片描述

三、 使用IDEA创建WEB项目

第一步:创建项目

在这里插入图片描述

第二步:修改域名,项目名等

在这里插入图片描述

第三步:创建完整的文件路径

在这里插入图片描述

第四步:在pom.xml中导入tomcat

    <!--加入Servlet的依赖-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
    
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
    </dependency>
  </dependencies>

四、 依赖管理

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

依赖范围, 使用scope表示的

scope的值有 compile, test, provided ,默认是compile

  • scope:表示依赖使用的范围,也就是在maven构建项目的那些阶段中起作用。
  • maven构建项目的声明周期为:编译, 测试 ,打包, 安装 ,部署 过程(阶段)
    • test属性
      我们查看junit的依赖范围是 test,作用范围只在测试阶段生效

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

在这里插入图片描述

五、 Maven常用设置

1.设置Maven属性设置

使用 <properties> 设置maven的常用属性

<properties>
<!--maven构建项目使用编码,避免中文乱码-->
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <!--编译代码使用的jdk版本-->
  <maven.compiler.source>1.8</maven.compiler.source>
  <!--运行程序使用的jdk版本-->
  <maven.compiler.target>1.8</maven.compiler.target>
  <!--自定义变量,表示版本号-->
  <spring-version>5.2.5.RELEASE</spring-version>
</properties>
!--添加Spring依赖-->
 <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring-version}</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>${spring-version}</version>
  </dependency>
</dependencies>

那么我们如何自定义一些属性呢?
1.在<properties> 通过自定义标签声明变量(标签名就是变量名)

2.在pom.xml文件中的其它位置,使用 ${标签名} 使用变量的值

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

2.资源插件

  <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目录中

3.编译插件

 <build>
    <plugins>
      <!--JDK1.8编译插件-->
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐