12.Java- Maven 教程

12.Java- Maven 教程一、安装引用菜鸟教程:Maven菜鸟教程地址1.下载1.Maven链接Maven下载地址:,点击跳转2.选择版本3.解压到指定地址4.并配置环境变量,引用的菜鸟教程引用菜鸟教程,菜鸟教程路径地址,点击跳转右键“计算机”,选择“属性”,之后点击“高级系统设置”,点击”环境变量”,来设置环境变量,有以下系统变量需要配置:新建系统变量MAVEN_HOME,变量值:E:\Maven\apache-maven-3.3.9编辑系统变量Path,添加变量值:;%M

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

一、安装

引用菜鸟教程:Maven菜鸟教程地址

1. Maven 链接

Maven 下载地址:,点击跳转

2. 选择版本

在这里插入图片描述

3. 解压到指定地址

在这里插入图片描述

4.并配置环境变量,引用的菜鸟教程

引用菜鸟教程,菜鸟教程路径地址,点击跳转

  1. 右键 “计算机”,选择 “属性”,之后点击 “高级系统设置”,点击”环境变量”,来设置环境变量,有以下系统变量需要配置:

  2. 新建系统变量 MAVEN_HOME,变量值:E:\Maven\apache-maven-3.3.9
    在这里插入图片描述

  3. 直接编辑系统变量 Path,添加变量值:;%MAVEN_HOME%\bin

在这里插入图片描述

5. 测试是否配置成功

win+R 弹出命令弹窗,输入cmd,打开命令窗口,输入mvn -v 查看是否配置成功
在这里插入图片描述

6. 如果配置不成功,重新配置用户path

  1. 在系统变量里面添加M2_HOME, 和 MAVEN_HOME 一样,都指向你的本地maven安装路径
    在这里插入图片描述
  2. 添加到用户变量PATH中 %MAVEN_HOME%\bin%M2_HOME %\bin
    在这里插入图片描述
  3. 为了安全把M2_HOME也配置到系统变量 %MAVEN_HOME%\bin%M2_HOME %\bin
    在这里插入图片描述
  4. 任意路径测试是否成功
    在这里插入图片描述

二、 认识、配置Maven

1. 目录结构

在这里插入图片描述
在这里插入图片描述

2. pom.xl 参数详解

  1. 编写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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>


    <name>demo</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>


知识点:pom.xl 参数详解

[xml] view plain copy print?
<span style="padding:0px; margin:0px"><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.0http://maven.apache.org/maven-v4_0_0.xsd">     
<!--父项目的坐标。如果项目中没有规定某个元素的值,那么父项目中的对应值即为项目的默认值。 坐标包括group ID,
artifact ID和 version。-->    
<parent>    
<!--被继承的父项目的构件标识符-->    
<artifactId/>    
<!--被继承的父项目的全球唯一标识符-->    
<groupId/>    
<!--被继承的父项目的版本-->    
<version/>    
<!-- 父项目的pom.xml文件的相对路径。相对路径允许你选择一个不同的路径。默认值是../pom.xml。Maven首先在构建当前项目
的地方寻找父项目的pom,其次在文件系统的这个位置(relativePath位置),然后在本地仓库,最后在远程仓库寻找父项目的pom。-->    
<relativePath/>    
</parent>    
<!--声明项目描述符遵循哪一个POM模型版本。模型本身的版本很少改变,虽然如此,但它仍然是必不可少的,这是为了当Maven引入了
新的特性或者其他模型变更的时候,确保稳定性。-->       
<modelVersion>4.0.0</modelVersion>     
<!--项目的全球唯一标识符,通常使用全限定的包名区分该项目和其他项目。并且构建时生成的路径也是由此生成,
如com.mycompany.app生成的相对路径为:/com/mycompany/app-->     
<groupId>asia.banseon</groupId>     
<!-- 构件的标识符,它和group ID一起唯一标识一个构件。换句话说,你不能有两个不同的项目拥有同样的artifact ID和groupID;
在某个特定的group ID下,artifact ID也必须是唯一的。构件是项目产生的或使用的一个东西,
Maven为项目产生的构件包括:JARs,源 码,二进制发布和WARs等。-->     
<artifactId>banseon-maven2</artifactId>     
<!--项目产生的构件类型,例如jar、war、ear、pom。插件可以创建他们自己的构件类型,所以前面列的不是全部构件类型-->     
<packaging>jar</packaging>     
<!--项目当前版本,格式为:主版本.次版本.增量版本-限定版本号-->     
<version>1.0-SNAPSHOT</version>     
<!--项目的名称, Maven产生的文档用-->     
<name>banseon-maven</name>     
<!--项目主页的URL, Maven产生的文档用-->     
<url>http://www.baidu.com/banseon</url>     
<!-- 项目的详细描述, Maven 产生的文档用。  当这个元素能够用HTML格式描述时(例如,CDATA中的文本会被解析器忽略,
就可以包含HTML标 签), 不鼓励使用纯文本描述。如果你需要修改产生的web站点的索引页面,你应该修改你自己的索引页文件,
而不是调整这里的文档。-->     
<description>A maven project to study maven.</description>     
<!--描述了这个项目构建环境中的前提条件。-->    
<prerequisites>    
<!--构建该项目或使用该插件所需要的Maven的最低版本-->    
<maven/>    
</prerequisites>    
<!--项目的问题管理系统(Bugzilla, Jira, Scarab,或任何你喜欢的问题管理系统)的名称和URL,本例为 jira-->     
<issueManagement>    
<!--问题管理系统(例如jira)的名字,-->     
<system>jira</system>     
<!--该项目使用的问题管理系统的URL-->    
<url>http://jira.baidu.com/banseon</url>     
</issueManagement>     
<!--项目持续集成信息-->    
<ciManagement>    
<!--持续集成系统的名字,例如continuum-->    
<system/>    
<!--该项目使用的持续集成系统的URL(如果持续集成系统有web接口的话)。-->    
<url/>    
<!--构建完成时,需要通知的开发者/用户的配置项。包括被通知者信息和通知条件(错误,失败,成功,警告)-->    
<notifiers>    
<!--配置一种方式,当构建中断时,以该方式通知用户/开发者-->    
<notifier>    
<!--传送通知的途径-->    
<type/>    
<!--发生错误时是否通知-->    
<sendOnError/>    
<!--构建失败时是否通知-->    
<sendOnFailure/>    
<!--构建成功时是否通知-->    
<sendOnSuccess/>    
<!--发生警告时是否通知-->    
<sendOnWarning/>    
<!--不赞成使用。通知发送到哪里-->    
<address/>    
<!--扩展配置项-->    
<configuration/>    
</notifier>    
</notifiers>    
</ciManagement>    
<!--项目创建年份,4位数字。当产生版权信息时需要使用这个值。-->    
<inceptionYear/>    
<!--项目相关邮件列表信息-->     
<mailingLists>    
<!--该元素描述了项目相关的所有邮件列表。自动产生的网站引用这些信息。-->     
<mailingList>     
<!--邮件的名称-->    
<name>Demo</name>     
<!--发送邮件的地址或链接,如果是邮件地址,创建文档时,mailto: 链接会被自动创建-->     
<post>banseon@126.com</post>     
<!--订阅邮件的地址或链接,如果是邮件地址,创建文档时,mailto: 链接会被自动创建-->     
<subscribe>banseon@126.com</subscribe>     
<!--取消订阅邮件的地址或链接,如果是邮件地址,创建文档时,mailto: 链接会被自动创建-->     
<unsubscribe>banseon@126.com</unsubscribe>     
<!--你可以浏览邮件信息的URL-->    
<archive>http:/hi.baidu.com/banseon/demo/dev/</archive>     
</mailingList>     
</mailingLists>     
<!--项目开发者列表-->     
<developers>     
<!--某个项目开发者的信息-->    
<developer>     
<!--SCM里项目开发者的唯一标识符-->    
<id>HELLO WORLD</id>     
<!--项目开发者的全名-->    
<name>banseon</name>     
<!--项目开发者的email-->    
<email>banseon@126.com</email>     
<!--项目开发者的主页的URL-->    
<url/>    
<!--项目开发者在项目中扮演的角色,角色元素描述了各种角色-->    
<roles>     
<role>Project Manager</role>     
<role>Architect</role>     
</roles>    
<!--项目开发者所属组织-->    
<organization>demo</organization>     
<!--项目开发者所属组织的URL-->    
<organizationUrl>http://hi.baidu.com/banseon</organizationUrl>     
<!--项目开发者属性,如即时消息如何处理等-->    
<properties>     
<dept>No</dept>     
</properties>    
<!--项目开发者所在时区, -1112范围内的整数。-->    
<timezone>-5</timezone>     
</developer>     
</developers>     
<!--项目的其他贡献者列表-->     
<contributors>    
<!--项目的其他贡献者。参见developers/developer元素-->    
<contributor>    
<name/><email/><url/><organization/><organizationUrl/><roles/><timezone/><properties/>    
</contributor>         
</contributors>       
<!--该元素描述了项目所有License列表。 应该只列出该项目的license列表,不要列出依赖项目的 license列表。
如果列出多个license,用户可以选择它们中的一个而不是接受所有license。-->     
<licenses>    
<!--描述了项目的license,用于生成项目的web站点的license页面,其他一些报表和validation也会用到该元素。-->     
<license>    
<!--license用于法律上的名称-->    
<name>Apache 2</name>     
<!--官方的license正文页面的URL-->    
<url>http://www.baidu.com/banseon/LICENSE-2.0.txt</url>     
<!--项目分发的主要方式:    
repo,可以从Maven库下载    
manual, 用户必须手动下载和安装依赖-->    
<distribution>repo</distribution>     
<!--关于license的补充信息-->    
<comments>A business-friendly OSS license</comments>     
</license>     
</licenses>     
<!--SCM(Source Control Management)标签允许你配置你的代码库,供Maven web站点和其它插件使用。-->     
<scm>     
<!--SCM的URL,该URL描述了版本库和如何连接到版本库。欲知详情,请看SCMs提供的URL格式和列表。该连接只读。-->     
<connection>     
scm:svn:http://svn.baidu.com/banseon/maven/banseon/banseon-maven2-trunk(dao-trunk)      
</connection>     
<!--给开发者使用的,类似connection元素。即该连接不仅仅只读-->    
<developerConnection>     
scm:svn:http://svn.baidu.com/banseon/maven/banseon/dao-trunk      
</developerConnection>    
<!--当前代码的标签,在开发阶段默认为HEAD-->    
<tag/>           
<!--指向项目的可浏览SCM库(例如ViewVC或者Fisheye)的URL。-->     
<url>http://svn.baidu.com/banseon</url>     
</scm>     
<!--描述项目所属组织的各种属性。Maven产生的文档用-->     
<organization>     
<!--组织的全名-->    
<name>demo</name>     
<!--组织主页的URL-->    
<url>http://www.baidu.com/banseon</url>     
</organization>    
<!--构建项目需要的信息-->    
<build>    
<!--该元素设置了项目源码目录,当构建项目的时候,构建系统会编译目录里的源码。该路径是相对于pom.xml的相对路径。-->    
<sourceDirectory/>    
<!--该元素设置了项目脚本源码目录,该目录和源码目录不同:绝大多数情况下,该目录下的内会被拷贝到输出目录
(因为脚本是被解释的,而不是被编译的)-->    
<scriptSourceDirectory/>    
<!--该元素设置了项目单元测试使用的源码目录,当测试项目的时候,构建系统会编译目录里的源码。该路径是相对于pom.xml的相对路径-->    
<testSourceDirectory/>    
<!--被编译过的应用程序class文件存放的目录。-->    
<outputDirectory/>    
<!--被编译过的测试class文件存放的目录。-->    
<testOutputDirectory/>    
<!--使用来自该项目的一系列构建扩展-->    
<extensions>    
<!--描述使用到的构建扩展。-->    
<extension>    
<!--构建扩展的groupId-->    
<groupId/>    
<!--构建扩展的artifactId-->    
<artifactId/>    
<!--构建扩展的版本-->    
<version/>    
</extension>    
</extensions>    
<!--当项目没有规定目标(Maven2 叫做阶段)时的默认值-->    
<defaultGoal/>    
<!--这个元素描述了项目相关的所有资源路径列表,例如和项目相关的属性文件,这些资源被包含在最终的打包文件里。-->    
<resources>    
<!--这个元素描述了项目相关或测试相关的所有资源路径-->    
<resource>    
<!-- 描述了资源的目标路径。该路径相对target/classes目录(例如${ 
project.build.outputDirectory})。
举个例子,如果你想资源在特定的包里(org.apache.maven.messages),你就必须该元素设置为org/apache/maven /messages。
然而,如果你只是想把资源放到源码目录结构里,就不需要该配置。-->    
<targetPath/>    
<!--是否使用参数值代替参数名。参数值取自properties元素或者文件里配置的属性,文件在filters元素里列出。-->    
<filtering/>    
<!--描述存放资源的目录,该路径相对POM路径-->    
<directory/>    
<!--包含的模式列表,例如**/*.xml.--> <includes/> <!--排除的模式列表,例如**/*.xml-->    
<excludes/>    
</resource>    
</resources>    
<!--这个元素描述了单元测试相关的所有资源路径,例如和单元测试相关的属性文件。-->    
<testResources>    
<!--这个元素描述了测试相关的所有资源路径,参见build/resources/resource元素的说明-->    
<testResource>    
<targetPath/><filtering/><directory/><includes/><excludes/>    
</testResource>    
</testResources>    
<!--构建产生的所有文件存放的目录-->    
<directory/>    
<!--产生的构件的文件名,默认值是${ 
artifactId}-${ 
version}-->    
<finalName/>    
<!--当filtering开关打开时,使用到的过滤器属性文件列表-->    
<filters/>    
<!--子项目可以引用的默认插件信息。该插件配置项直到被引用时才会被解析或绑定到生命周期。给定插件的任何本地配置都会覆盖这里的配置-->    
<pluginManagement>    
<!--使用的插件列表 。-->    
<plugins>    
<!--plugin元素包含描述插件所需要的信息。-->    
<plugin>    
<!--插件在仓库里的group ID-->    
<groupId/>    
<!--插件在仓库里的artifact ID-->    
<artifactId/>    
<!--被使用的插件的版本(或版本范围)-->    
<version/>    
<!--是否从该插件下载Maven扩展(例如打包和类型处理器),由于性能原因,只有在真需要下载时,该元素才被设置成enabled。-->    
<extensions/>    
<!--在构建生命周期中执行一组目标的配置。每个目标可能有不同的配置。-->    
<executions>    
<!--execution元素包含了插件执行需要的信息-->    
<execution>    
<!--执行目标的标识符,用于标识构建过程中的目标,或者匹配继承过程中需要合并的执行目标-->    
<id/>    
<!--绑定了目标的构建生命周期阶段,如果省略,目标会被绑定到源数据里配置的默认阶段-->    
<phase/>    
<!--配置的执行目标-->    
<goals/>    
<!--配置是否被传播到子POM-->    
<inherited/>    
<!--作为DOM对象的配置-->    
<configuration/>    
</execution>    
</executions>    
<!--项目引入插件所需要的额外依赖-->    
<dependencies>    
<!--参见dependencies/dependency元素-->    
<dependency>    
......    
</dependency>    
</dependencies>         
<!--任何配置是否被传播到子项目-->    
<inherited/>    
<!--作为DOM对象的配置-->    
<configuration/>    
</plugin>    
</plugins>    
</pluginManagement>    
<!--使用的插件列表-->    
<plugins>    
<!--参见build/pluginManagement/plugins/plugin元素-->    
<plugin>    
<groupId/><artifactId/><version/><extensions/>    
<executions>    
<execution>    
<id/><phase/><goals/><inherited/><configuration/>    
</execution>    
</executions>    
<dependencies>    
<!--参见dependencies/dependency元素-->    
<dependency>    
......    
</dependency>    
</dependencies>    
<goals/><inherited/><configuration/>    
</plugin>    
</plugins>    
</build>    
<!--在列的项目构建profile,如果被激活,会修改构建处理-->    
<profiles>    
<!--根据环境参数或命令行参数激活某个构建处理-->    
<profile>    
<!--构建配置的唯一标识符。即用于命令行激活,也用于在继承时合并具有相同标识符的profile。-->    
<id/>    
<!--自动触发profile的条件逻辑。Activation是profile的开启钥匙。profile的力量来自于它    
能够在某些特定的环境中自动使用某些特定的值;这些环境通过activation元素指定。activation元素并不是激活profile的唯一方式。-->    
<activation>    
<!--profile默认是否激活的标志-->    
<activeByDefault/>    
<!--当匹配的jdk被检测到,profile被激活。例如,1.4激活JDK1.41.4.0_2,而!1.4激活所有版本不是以1.4开头的JDK。-->    
<jdk/>    
<!--当匹配的操作系统属性被检测到,profile被激活。os元素可以定义一些操作系统相关的属性。-->    
<os>    
<!--激活profile的操作系统的名字-->    
<name>Windows XP</name>    
<!--激活profile的操作系统所属家族('windows')-->    
<family>Windows</family>    
<!--激活profile的操作系统体系结构 -->    
<arch>x86</arch>    
<!--激活profile的操作系统版本-->    
<version>5.1.2600</version>    
</os>    
<!--如果Maven检测到某一个属性(其值可以在POM中通过${ 
名称}引用),其拥有对应的名称和值,Profile就会被激活。如果值    
字段是空的,那么存在属性名称字段就会激活profile,否则按区分大小写方式匹配属性值字段-->    
<property>    
<!--激活profile的属性的名称-->    
<name>mavenVersion</name>    
<!--激活profile的属性的值-->    
<value>2.0.3</value>    
</property>    
<!--提供一个文件名,通过检测该文件的存在或不存在来激活profile。missing检查文件是否存在,如果不存在则激活    
profile。另一方面,exists则会检查文件是否存在,如果存在则激活profile。-->    
<file>    
<!--如果指定的文件存在,则激活profile。-->    
<exists>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/</exists>    
<!--如果指定的文件不存在,则激活profile。-->    
<missing>/usr/local/hudson/hudson-home/jobs/maven-guide-zh-to-production/workspace/</missing>    
</file>    
</activation>    
<!--构建项目所需要的信息。参见build元素-->    
<build>    
<defaultGoal/>    
<resources>    
<resource>    
<targetPath/><filtering/><directory/><includes/><excludes/>    
</resource>    
</resources>    
<testResources>    
<testResource>    
<targetPath/><filtering/><directory/><includes/><excludes/>    
</testResource>    
</testResources>    
<directory/><finalName/><filters/>    
<pluginManagement>    
<plugins>    
<!--参见build/pluginManagement/plugins/plugin元素-->    
<plugin>    
<groupId/><artifactId/><version/><extensions/>    
<executions>    
<execution>    
<id/><phase/><goals/><inherited/><configuration/>    
</execution>    
</executions>    
<dependencies>    
<!--参见dependencies/dependency元素-->    
<dependency>    
......    
</dependency>    
</dependencies>    
<goals/><inherited/><configuration/>    
</plugin>    
</plugins>    
</pluginManagement>    
<plugins>    
<!--参见build/pluginManagement/plugins/plugin元素-->    
<plugin>    
<groupId/><artifactId/><version/><extensions/>    
<executions>    
<execution>    
<id/><phase/><goals/><inherited/><configuration/>    
</execution>    
</executions>    
<dependencies>    
<!--参见dependencies/dependency元素-->    
<dependency>    
......    
</dependency>    
</dependencies>    
<goals/><inherited/><configuration/>    
</plugin>    
</plugins>    
</build>    
<!--模块(有时称作子项目) 被构建成项目的一部分。列出的每个模块元素是指向该模块的目录的相对路径-->    
<modules/>    
<!--发现依赖和扩展的远程仓库列表。-->    
<repositories>    
<!--参见repositories/repository元素-->    
<repository>    
<releases>    
<enabled/><updatePolicy/><checksumPolicy/>    
</releases>    
<snapshots>    
<enabled/><updatePolicy/><checksumPolicy/>    
</snapshots>    
<id/><name/><url/><layout/>    
</repository>    
</repositories>    
<!--发现插件的远程仓库列表,这些插件用于构建和报表-->    
<pluginRepositories>    
<!--包含需要连接到远程插件仓库的信息.参见repositories/repository元素-->        
<pluginRepository>    
<releases>    
<enabled/><updatePolicy/><checksumPolicy/>    
</releases>    
<snapshots>    
<enabled/><updatePolicy/><checksumPolicy/>    
</snapshots>    
<id/><name/><url/><layout/>    
</pluginRepository>    
</pluginRepositories>    
<!--该元素描述了项目相关的所有依赖。 这些依赖组成了项目构建过程中的一个个环节。它们自动从项目定义的仓库中下载。
要获取更多信息,请看项目依赖机制。-->    
<dependencies>    
<!--参见dependencies/dependency元素-->    
<dependency>    
......    
</dependency>    
</dependencies>    
<!--不赞成使用. 现在Maven忽略该元素.-->    
<reports/>       
<!--该元素包括使用报表插件产生报表的规范。当用户执行“mvn site”,这些报表就会运行。 在页面导航栏能看到所有报表的链接。
参见reporting元素-->    
<reporting>    
......    
</reporting>    
<!--参见dependencyManagement元素-->    
<dependencyManagement>    
<dependencies>    
<!--参见dependencies/dependency元素-->    
<dependency>    
......    
</dependency>    
</dependencies>    
</dependencyManagement>    
<!--参见distributionManagement元素-->    
<distributionManagement>    
......    
</distributionManagement>    
<!--参见properties元素-->    
<properties/>    
</profile>    
</profiles>    
<!--模块(有时称作子项目) 被构建成项目的一部分。列出的每个模块元素是指向该模块的目录的相对路径-->    
<modules/>    
<!--发现依赖和扩展的远程仓库列表。-->     
<repositories>     
<!--包含需要连接到远程仓库的信息-->    
<repository>    
<!--如何处理远程仓库里发布版本的下载-->    
<releases>    
<!--true或者false表示该仓库是否为下载某种类型构件(发布版,快照版)开启。 -->    
<enabled/>    
<!--该元素指定更新发生的频率。Maven会比较本地POM和远程POM的时间戳。
这里的选项是:always(一直),daily(默认,每日),interval:X(这里X是以分钟为单位的时间间隔),或者never(从不)。-->    
<updatePolicy/>    
<!--Maven验证构件校验文件失败时该怎么做:ignore(忽略),fail(失败),或者warn(警告)。-->    
<checksumPolicy/>    
</releases>    
<!-- 如何处理远程仓库里快照版本的下载。有了releases和snapshots这两组配置,POM就可以在每个单独的仓库中,
为每种类型的构件采取不同的策略。例如,可能有人会决定只为开发目的开启对快照版本下载的支持。
参见repositories/repository/releases元素 -->    
<snapshots>    
<enabled/><updatePolicy/><checksumPolicy/>    
</snapshots>    
<!--远程仓库唯一标识符。可以用来匹配在settings.xml文件里配置的远程仓库-->    
<id>banseon-repository-proxy</id>     
<!--远程仓库名称-->    
<name>banseon-repository-proxy</name>     
<!--远程仓库URL,按protocol://hostname/path形式-->    
<url>http://192.168.1.169:9999/repository/</url>     
<!-- 用于定位和排序构件的仓库布局类型-可以是default(默认)或者legacy(遗留)。
Maven 2为其仓库提供了一个默认的布局;然 而,Maven 1.x有一种不同的布局。
我们可以使用该元素指定布局是default(默认)还是legacy(遗留)。-->    
<layout>default</layout>               
</repository>     
</repositories>    
<!--发现插件的远程仓库列表,这些插件用于构建和报表-->    
<pluginRepositories>    
<!--包含需要连接到远程插件仓库的信息.参见repositories/repository元素-->    
<pluginRepository>    
......    
</pluginRepository>    
</pluginRepositories>    
<!--该元素描述了项目相关的所有依赖。 这些依赖组成了项目构建过程中的一个个环节。它们自动从项目定义的仓库中下载。
要获取更多信息,请看项目依赖机制。-->     
<dependencies>     
<dependency>    
<!--依赖的group ID-->    
<groupId>org.apache.maven</groupId>     
<!--依赖的artifact ID-->    
<artifactId>maven-artifact</artifactId>     
<!--依赖的版本号。 在Maven 2, 也可以配置成版本号的范围。-->    
<version>3.8.1</version>     
<!-- 依赖类型,默认类型是jar。它通常表示依赖的文件的扩展名,但也有例外。一个类型可以被映射成另外一个扩展名或分类器。
类型经常和使用的打包方式对应, 尽管这也有例外。一些类型的例子:jar,war,ejb-client和test-jar。
如果设置extensions为 true,就可以在 plugin里定义新的类型。所以前面的类型的例子不完整。-->    
<type>jar</type>    
<!-- 依赖的分类器。分类器可以区分属于同一个POM,但不同构建方式的构件。分类器名被附加到文件名的版本号后面。
例如,如果你想要构建两个单独的构件成 JAR,一个使用Java 1.4编译器,另一个使用Java 6编译器,
你就可以使用分类器来生成两个单独的JAR构件。-->    
<classifier></classifier>    
<!--依赖范围。在项目发布过程中,帮助决定哪些构件被包括进来。欲知详情请参考依赖机制。    
- compile :默认范围,用于编译      
- provided:类似于编译,但支持你期待jdk或者容器提供,类似于classpath      
- runtime: 在执行时需要使用      
- test:    用于test任务时使用      
- system: 需要外在提供相应的元素。通过systemPath来取得      
- systemPath: 仅用于范围为system。提供相应的路径      
- optional:   当项目自身被依赖时,标注依赖是否传递。用于连续依赖时使用-->     
<scope>test</scope>       
<!--仅供system范围使用。注意,不鼓励使用这个元素,并且在新的版本中该元素可能被覆盖掉。该元素为依赖规定了文件系统上的路径。
需要绝对路径而不是相对路径。推荐使用属性匹配绝对路径,例如${ 
java.home}-->    
<systemPath></systemPath>     
<!--当计算传递依赖时, 从依赖构件列表里,列出被排除的依赖构件集。即告诉maven你只依赖指定的项目,不依赖项目的依赖。
此元素主要用于解决版本冲突问题-->    
<exclusions>    
<exclusion>     
<artifactId>spring-core</artifactId>     
<groupId>org.springframework</groupId>     
</exclusion>     
</exclusions>       
<!--可选依赖,如果你在项目B中把C依赖声明为可选,你就需要在依赖于B的项目(例如项目A)中显式的引用对C的依赖。
可选依赖阻断依赖的传递性。-->     
<optional>true</optional>    
</dependency>    
</dependencies>    
<!--不赞成使用. 现在Maven忽略该元素.-->    
<reports></reports>    
<!--该元素描述使用报表插件产生报表的规范。当用户执行“mvn site”,这些报表就会运行。 在页面导航栏能看到所有报表的链接。-->    
<reporting>    
<!--true,则,网站不包括默认的报表。这包括“项目信息”菜单中的报表。-->    
<excludeDefaults/>    
<!--所有产生的报表存放到哪里。默认值是${ 
project.build.directory}/site。-->    
<outputDirectory/>    
<!--使用的报表插件和他们的配置。-->    
<plugins>    
<!--plugin元素包含描述报表插件需要的信息-->    
<plugin>    
<!--报表插件在仓库里的group ID-->    
<groupId/>    
<!--报表插件在仓库里的artifact ID-->    
<artifactId/>    
<!--被使用的报表插件的版本(或版本范围)-->    
<version/>    
<!--任何配置是否被传播到子项目-->    
<inherited/>    
<!--报表插件的配置-->    
<configuration/>    
<!--一组报表的多重规范,每个规范可能有不同的配置。一个规范(报表集)对应一个执行目标 。例如,有123456789个报表。
125构成A报表集,对应一个执行目标。258构成B报表集,对应另一个执行目标-->    
<reportSets>    
<!--表示报表的一个集合,以及产生该集合的配置-->    
<reportSet>    
<!--报表集合的唯一标识符,POM继承时用到-->    
<id/>    
<!--产生报表集合时,被使用的报表的配置-->    
<configuration/>    
<!--配置是否被继承到子POMs-->    
<inherited/>    
<!--这个集合里使用到哪些报表-->    
<reports/>    
</reportSet>    
</reportSets>    
</plugin>    
</plugins>    
</reporting>    
<!-- 继承自该项目的所有子项目的默认依赖信息。这部分的依赖信息不会被立即解析,而是当子项目声明一个依赖(必须描述group ID和
artifact ID信息),如果group ID和artifact ID以外的一些信息没有描述,则通过group ID和artifact ID 匹配到这里的依赖,
并使用这里的依赖信息。-->    
<dependencyManagement>    
<dependencies>    
<!--参见dependencies/dependency元素-->    
<dependency>    
......    
</dependency>    
</dependencies>    
</dependencyManagement>       
<!--项目分发信息,在执行mvn deploy后表示要发布的位置。有了这些信息就可以把网站部署到远程服务器或者把构件部署到远程仓库。-->     
<distributionManagement>    
<!--部署项目产生的构件到远程仓库需要的信息-->    
<repository>    
<!--是分配给快照一个唯一的版本号(由时间戳和构建流水号)?还是每次都使用相同的版本号?参见repositories/repository元素-->    
<uniqueVersion/>    
<id>banseon-maven2</id>     
<name>banseon maven2</name>     
<url>file://${ 
basedir}/target/deploy</url>     
<layout/>    
</repository>    
<!--构件的快照部署到哪里?如果没有配置该元素,默认部署到repository元素配置的仓库,参见distributionManagement/repository元素-->     
<snapshotRepository>    
<uniqueVersion/>    
<id>banseon-maven2</id>    
<name>Banseon-maven2 Snapshot Repository</name>    
<url>scp://svn.baidu.com/banseon:/usr/local/maven-snapshot</url>     
<layout/>    
</snapshotRepository>    
<!--部署项目的网站需要的信息-->     
<site>    
<!--部署位置的唯一标识符,用来匹配站点和settings.xml文件里的配置-->     
<id>banseon-site</id>     
<!--部署位置的名称-->    
<name>business api website</name>     
<!--部署位置的URL,按protocol://hostname/path形式-->    
<url>     
scp://svn.baidu.com/banseon:/var/www/localhost/banseon-web      
</url>     
</site>    
<!--项目下载页面的URL。如果没有该元素,用户应该参考主页。使用该元素的原因是:帮助定位那些不在仓库里的构件(由于license限制)。-->    
<downloadUrl/>    
<!--如果构件有了新的group ID和artifact ID(构件移到了新的位置),这里列出构件的重定位信息。-->    
<relocation>    
<!--构件新的group ID-->    
<groupId/>    
<!--构件新的artifact ID-->    
<artifactId/>    
<!--构件新的版本号-->    
<version/>    
<!--显示给用户的,关于移动的额外信息,例如原因。-->    
<message/>    
</relocation>    
<!-- 给出该构件在远程仓库的状态。不得在本地项目中设置该元素,因为这是工具自动更新的。有效的值有:none(默认),
converted(仓库管理员从 Maven 1 POM转换过来),partner(直接从伙伴Maven 2仓库同步过来),
deployed(从Maven 2实例部 署),verified(被核实时正确的和最终的)。-->    
<status/>           
</distributionManagement>    
<!--以值替代名称,Properties可以在整个POM中使用,也可以作为触发条件(见settings.xml配置文件里activation元素的说明)。
格式是<name>value</name>-->    
<properties/>    
</project> 
  1. 编写java代码目录路径
    在这里插入图片描述

2. 配置settings.xml文件

  1. 进入你下载安装的maven,进入conf
    在这里插入图片描述
  2. 修改配置文件
    • 【1】修改仓库位置
    <!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${ 
user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<!--修改1:复制上方localRepository代码,修改本地仓库的地址,用来存放jar包,注意这个路径要配置到你自己想要
配置的路径-->
<localRepository>D:/WorkSoftware/apache-maven-3.8.3/java_package</localRepository>
  • 注意上方修改本的地仓库的地址,必须是你自己的路径位置,为了方便我直接在安装的maven文件下面新建了一个java_package目录存放,如下图
    在这里插入图片描述
  • 【2】配置镜像路径
  <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>maven-default-http-blocker</id>
<mirrorOf>external:http:*</mirrorOf>
<name>Pseudo repository to mirror external repositories initially using HTTP.</name>
<url>http://0.0.0.0/</url>
<blocked>true</blocked>
</mirror>
</mirrors>
  • 把上方 mirror 里面的 mirror>替换为
 <!--修改2:更换阿里巴巴镜像更快-->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
  • 替换后代码
<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>maven-default-http-blocker</id>
<mirrorOf>external:http:*</mirrorOf>
<name>Pseudo repository to mirror external repositories initially using HTTP.</name>
<url>http://0.0.0.0/</url>
<blocked>true</blocked>
</mirror>-->
<!--修改2:更换阿里巴巴镜像更快-->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
  • 【3】修改完成后的配置
<?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.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.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>
-->
<!--修改1:复制上方localRepository代码,修改本地仓库的地址,用来存放jar包-->
<localRepository>D:/WorkSoftware/apache-maven-3.8.3/java_package</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>maven-default-http-blocker</id>
<mirrorOf>external:http:*</mirrorOf>
<name>Pseudo repository to mirror external repositories initially using HTTP.</name>
<url>http://0.0.0.0/</url>
<blocked>true</blocked>
</mirror>-->
<!--修改2:更换阿里巴巴镜像更快-->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</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>

3. 编译,可以忽略,用IDEA,只看以上配置

  1. 选中路径,输入cmd,进入当前路径的命令行
    在这里插入图片描述

  2. 输入命令mvn compile,回车
    在这里插入图片描述
    编辑结果,如果出现 BUILD SUCCESS说明成功
    在这里插入图片描述
    查看下载的文件
    在这里插入图片描述

  3. 执行main方法 mvn exec:java-Dexec.mainClass="com.xxx.demo.Hello"
    在这里插入图片描述

4. maven常用命令

在这里插入图片描述

三、IEDA 配置Maven

  1. File->Setting…
  2. Build…->Build Tools->Maven
    在这里插入图片描述

四、Maven项目创建

1. 创建Java项目

1.新建项目

  1. 选择File->New->Project->Maven,Next
    在这里插入图片描述

  2. 配置项目路径和名称,Next
    在这里插入图片描述
    3.配置maven路径和项目信息,Finish
    在这里插入图片描述

  3. 配置完成,如下图
    在这里插入图片描述

2. 添加文件,并被maven识别

  1. 右键main->New->Directory

在这里插入图片描述
2.添加resources
在这里插入图片描述
添加成功如下图
在这里插入图片描述

  1. 右键resource目录->Mark Directory as -> Sources Root,把当前目录当成资源目录。
    在这里插入图片描述
    4.Test文件夹下同样添加sources,和main不同的是,右键resource目录->Mark Directory as -> Test Sources Root,把当前目录当成测试资源目录。
    在这里插入图片描述

3. 编译项目

  1. Add Configuration…->弹出窗口,点击+号,弹出下来框->Maven
    在这里插入图片描述
    2.添加命令名称,compile编译项目->apple->ok
    在这里插入图片描述
  2. 运行命令
    在这里插入图片描述
  3. 运行结果
    在这里插入图片描述

4. 打包

  1. 添加打包命令package->apple->ok,操作流程参考上方 3.编译项目
    在这里插入图片描述
  2. 打包运行成功,会生成一个target目录,里面有一个maven-1.0-SNAPSHOT.jar包,打包成功
    在这里插入图片描述

2. 创建Web项目

1.新建项目

  1. 选择File->New->Project->Maven,Next
    在这里插入图片描述
  2. 输入项目名称,Next
    在这里插入图片描述
  3. 配置项目名称,Finish
    在这里插入图片描述
  4. 打包成功,项目目录如下
    在这里插入图片描述

2. 启动项目

1. 修改pom.xl

1.修改jdk的版本:查看自己jdk的版本,可以java -version 查看,把source 和 target 中的数据修改为自己的版本

  <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 修改1:修改jdk的版本:查看自己jdk的版本,可以java -version 查看,把source 和 target 中的数据修改为自己的版本
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>-->
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
  1. junit的版本修改为4.12
  <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!--修改2:junit的版本修改为4.12
<version>4.11</version>-->
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
  1. 删除 这个吧标签及标签内容全部删除,修改后的代码为
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx</groupId>
<artifactId>webapp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>webapp Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 修改1:修改jdk的版本:查看自己jdk的版本,可以java -version 查看,把source 和 target 中的数据修改为自己的版本
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>-->
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!--修改2:junit的版本修改为4.12
<version>4.11</version>-->
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>webapp</finalName>
<!-- 修改3:删除 <pluginManagement></pluginManagement>这个吧标签及标签内容全部删除-->
<!-- <pluginManagement>&lt;!&ndash; lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) &ndash;&gt;
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
&lt;!&ndash; see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging &ndash;&gt;
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>-->
</build>
</project>

2. 添加WEB部署插件

添加在build标签中,顺序为

  <build>
<plugins>
<!--设置在;plugins标签中-->
<plugin>
<!--这里放置jetty插件-->
</plugin>
<!--设置在;plugins标签中-->
<plugin>
<!--这里放置Tomcat插件-->
</plugin>
</plugins>
</build>
  1. Jetty插件,根据自己的jdk版本选择Jetty,不同的JDK,版本不同

    JDK17的配置代码

           <!--设置在;plugins标签中-->
<!--jetty插件org.mortbay.jett 一下是jdk17版本对应的写法-->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.14.v20161028</version>
<configuration>
<scanIntervalSeconds>2</scanIntervalSeconds><!-- 多少秒进行一次热部署 -->
<httpConnector>
<port>8090</port><!--端口号-->
<host>localhost</host> <!--网址路径域名-->
</httpConnector>
</configuration>
</plugin>

JDK1.8配置代码

            <!--设置在;plugins标签中-->
<!--jetty插件org.mortbay.jett JDK1.8 写法-->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.25</version>
<configuration>
<!-- 热部署,每10秒扫描一次-->
<scanIntervalSeconds>10</scanIntervalSeconds>
<!--可指定当前项目的站点名-->
<contextPath>/test</contextPath>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>9090</port> <!--设置启动的端口号-->
</connector>
</connectors>
</configuration>
</plugin>
  1. 添加Tomcat插件
  <!--设置在;plugins标签中-->
<!--Tomcat插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>8081</port> <!--启动端口,默认:8080-->
<path>/test</path><!-- 项目站点名称,即对外访问路径-->
<uriEncoding>UTF-8</uriEncoding> <!--字符集编码 默认:ISO-8859-1-->
<server>tomcat7</server> <!--服务器名称-->
</configuration>
</plugin>
  1. 更新maven,插件生效,修改配置后一定要更新,否则可能会报错
    在这里插入图片描述
  2. 整体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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx</groupId>
<artifactId>webapp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>webapp Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<!-- 代码库 可放入阿里云的配置等-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 修改1:修改jdk的版本:查看自己jdk的版本,可以java -version 查看,把source 和 target 中的数据修改为自己的版本
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>-->
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<!--添加配置阿里云仓库-->
<!--<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>-->
</properties>
<!--依赖集合-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!--修改2:junit的版本修改为4.12
<version>4.11</version>-->
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--添加servlet依赖-->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>webapp</finalName>
<!-- 修改3:删除 <pluginManagement></pluginManagement>这个吧标签及标签内容全部删除-->
<!-- <pluginManagement>&lt;!&ndash; lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) &ndash;&gt;
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
&lt;!&ndash; see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging &ndash;&gt;
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>-->
<!--插件集合-->
<plugins>
<!--设置在;plugins标签中-->
<!--jetty插件org.mortbay.jett JDK1.8 写法-->
<!-- <plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.25</version>
<configuration>
&lt;!&ndash; 热部署,每10秒扫描一次&ndash;&gt;
<scanIntervalSeconds>10</scanIntervalSeconds>
&lt;!&ndash;可指定当前项目的站点名&ndash;&gt;
<contextPath>/test</contextPath>
<connectors>
<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>9090</port> &lt;!&ndash;设置启动的端口号&ndash;&gt;
</connector>
</connectors>
</configuration>
</plugin>-->
<!--设置在;plugins标签中-->
<!--jetty插件org.mortbay.jett 一下是jdk17版本对应的写法-->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.14.v20161028</version>
<configuration>
<scanIntervalSeconds>2</scanIntervalSeconds><!-- 多少秒进行一次热部署 -->
<httpConnector>
<port>8090</port><!--端口号-->
<host>localhost</host> <!--网址路径域名-->
</httpConnector>
</configuration>
</plugin>
<!--设置在;plugins标签中-->
<!--Tomcat插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8081</port> <!--启动端口,默认:8080-->
<path>/test</path><!-- 项目站点名称,即对外访问路径-->
<uriEncoding>UTF-8</uriEncoding> <!--字符集编码 默认:ISO-8859-1-->
<server>tomcat7</server> <!--服务器名称-->
</configuration>
</plugin>
</plugins>
</build>
</project>

3. Jetty插件启动项目

  1. Add Configuration…->弹出窗口,点击+号,弹出下来框->Maven
    在这里插入图片描述2. 创建jett启动命令
    在这里插入图片描述
  2. 点击运行,运行成功,端口号8090
    在这里插入图片描述
  3. 输入网址 http://localhost:8090/index.jsp ,运行成功
    在这里插入图片描述

4. Tomcat插件启动项目

  1. Add Configuration…->弹出窗口,点击+号,弹出下来框->Maven
    在这里插入图片描述

  2. 创建tomcat启动命令
    在这里插入图片描述

  3. 运行命令,成功并返回访问路径
    在这里插入图片描述

  4. 访问成功链接 http://localhost:8081/test/index.jsp
    在这里插入图片描述

五、仓库

  • 【1】 Maven仓库分为两种:本地仓库和远程仓库
  • 【2】 远程仓库分为 :中央仓库、私服、其他公共库。
  • 中央仓库:是默认配置下, Maven下载jar包的地方,中央仓库包含了2000多个开源项目,每天接受1亿次以上的访问,基本 不会用它
  • 私服:从外部远程仓库下载,缓存到私服,在为maven提供。他是架设在局域网内的仓库服务
  • 其他公共库:例如阿里云仓库

1. 修改本地仓库路径settings.xml

修改localRepository,上方修改配置文件修改过

<!--修改1:复制上方localRepository代码,修改本地仓库的地址,用来存放jar包-->
<localRepository>D:/WorkSoftware/apache-maven-3.8.3/java_package</localRepository>

2. 配置私服pom.xml

<!-- 代码库 可放入阿里云的配置等-->
<repositories>
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>public</id>
<name>public repositories</name>
<url>http://192.168.0.096:8081/content/groups/public</url>
</repository>
<repository>
<id>getui-nexus</id>
<url>http://mvn.gt.igexin.com/nexus/content/repositories/releases</url>
</repository>
</repositories>

3.其他公共库

  • 指定maven镜像有两种方式,分别是修改全局配置和项目pom.xml中配置。这两种方式的使用场景如下:
  • 【1】 全局配置
    适合场景: 开发环境,修改后所有工程都生效了,不需要一个个工程配置
    优点: 一处配置,多处生效
    缺点:只在修改了全局配置的机器上有效, 如果在团队协作中,别人检出代码还是会使用默认国外的中央仓库
  • 【2】 局部配置
    适合场景: 生产环境、开发环境
    优点: 修改后的配置不管工程检出到哪里,构建的时候都会从我们的配置的阿里云镜像仓库下载依赖
    缺点:每个工程的pom.xml中都需要配置镜像仓库,如果工程多的话,配置量有点大(可以使用IDEA里面的代码模板解决)

1. 全局配置

  • 在setting.xml 修改,把系统自带的镜像修改为阿里云
  • 整个环境下所有项目都是当前仓库
    <!--配置自带仓库-->
<!--  <mirror>
<id>maven-default-http-blocker</id>
<mirrorOf>external:http:*</mirrorOf>
<name>Pseudo repository to mirror external repositories initially using HTTP.</name>
<url>http://0.0.0.0/</url>
<blocked>true</blocked>
</mirror>-->
<!--修改2:更换阿里巴巴仓库更快-->
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>

2. 局部修改

  • 放到pom.xml里面修改,用到的项目就修改成这个
  • 如果setting.xml 不修改只修改当前文件,只对当前项目起作用
  <!-- 代码库 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 修改1:修改jdk的版本:查看自己jdk的版本,可以java -version 查看,把source 和 target 中的数据修改为自己的版本
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>-->
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<!--添加配置阿里云仓库-->
<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</properties>

4. 查找配置依赖

1. 查找地址

MavenRepository依赖地址,点击跳转

2. 搜索servlet

点击进入第一个1. Java Servlet API
在这里插入图片描述

3. 选择版本

为了避免不谦容,最好选择使用人数最多的,点击前面版本号3.1.0,进入详情
在这里插入图片描述

4. 复制依赖代码

在这里插入图片描述

5. 放入pom.xml

在这里插入图片描述

六、Maven环境下国家多个模块项目

使用maven提供的多模块构建的特性完成maven环境下多个模块的项目的管理与构建

四个模块为例搭建项目,通俗易懂
模块 maven_parent – 基模块,就是常说的parent(pom),模板选择为(空)
模块 maven_dao – 数据库的访问层,例如jdbc操作(jar)
模块 maven_service – 项目的业务逻辑层(jar)
模块 maven_controller – 用来接收请求,相应数据(war)

1. 创建maven_parent项目

  1. 选择File->New->Project,Next
    在这里插入图片描述
  2. 创建项目名称mavent_parent,Finish
    在这里插入图片描述

2. 创建maven_dao模块

  1. 右键项目名称mavent_parent->New->Module…
    在这里插入图片描述
  2. 选择Mavent的项目模板(普通java项目),Next
    在这里插入图片描述
  3. 创建子类名称maven_dao
    在这里插入图片描述
  4. 选择jdk配置,finish。生成以下目录
    在这里插入图片描述

3. 创建maven_service模块

  • 创建maven_service 模块步骤和maven_dao模块一致
  • 创建完成目录如下
    在这里插入图片描述

4. 创建maven_controller模块

  • 创建maven_controller 模块步骤和maven_dao模块基本一致,只需要将选择Maven模板设置为web项目即可。(模板类型:maven-archetype-webapp).
  1. 选择模板maven-archetype-webapp,Next
    在这里插入图片描述
  2. 其他步骤和maven_dao模块一致,省略。
  3. 创建完成后的路径
    在这里插入图片描述

5. 模块配置

  • 把 maven_dao、maven_service、maven_controller 三个子模块修改配置如下
<?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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx</groupId>
<artifactId>maven_controller</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>maven_controller Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--修改1:把JDK修改成自己当前的版本-->
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!--修改2:junit版本修改为4.12-->
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>maven_controller</finalName>
<!--修改3:删除 <pluginManagement> </pluginManagement> 全部内容-->
</build>
</project>

6. 设置模块之间的依赖

四个模块为例搭建项目,通俗易懂
模块 maven_parent – 基模块,就是常说的parent(pom),模板选择为(空)
模块 maven_dao – 数据库的访问层,例如jdbc操作(jar)
模块 maven_service – 项目的业务逻辑层(jar)
模块 maven_controller – 用来接收请求,相应数据(war)

  • 【1】依赖说明:
  • maven_controller ( 用来接收请求,相应数据)依赖 maven_service(项目的业务逻辑层)
  • maven_service (项目的业务逻辑层) 依赖 maven_dao( 数据库的访问层)
  • 【2】 简易说明
  • maven_controller ( 用来接收请求,相应数据)接收数据后,交给 maven_service(项目的业务逻辑层)来处理逻辑,
  • maven_service(项目的业务逻辑层)处理逻辑完成,调用maven_dao( 数据库的访问层)用来访问数据库,完成操作。
  • maven_dao( 数据库的访问层)操作完成把结果返回 maven_service(项目的业务逻辑层),maven_service(项目的业务逻辑层)处理完后的逻辑结果返回 maven_dao( 数据库的访问层)。在返回给用户

1. maven_dao – 数据库的访问层

  1. 新建包,右键com.xxx->New->package
    在这里插入图片描述
  2. 输入包的名称dao
    在这里插入图片描述
  3. 创建UserDao类,右键dao包->New->java class
    在这里插入图片描述
  4. 添加测试方法
package com.xxx.dao;
public class UserDao { 

public static void testDao(){ 

System.out.println("UserDao test...");
}
}
  1. 创建完成
    在这里插入图片描述

2. maven_service – 项目的业务逻辑层

  1. 在 maven_service中的pom.xml里面的dependencies中加入maven_dao的依赖
 <!--依赖-->
<dependencies>
<!--junit的模块依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 重点在这里:添加maven_dao的依赖-->加入maven_dao的模块依赖-->
<dependency>
<groupId>com.xxx</groupId>
<artifactId>maven_dao</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
  1. 新建包,和maven_dao 操作一样,包名称改为service
  2. 新建UserService类,添加测试方法
package com.xxx.service;
import com.xxx.dao.UserDao;
public class UserService { 

public static void testService() { 

System.out.println("UserService test...");
//调用UserDao的方法
UserDao.testDao();
}
}
  1. 新建完成
    在这里插入图片描述

3. maven_controller– 用来接收请求,相应数据

  1. 在 maven_controller中的pom.xml里面的dependencies中加入maven_service的依赖
<!--依赖-->
<dependencies>
<!--junit的依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!--修改2:junit版本修改为4.12-->
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--重点在这里:添加maven_service的依赖-->
<dependency>
<groupId>com.xxx</groupId>
<artifactId>maven_service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
  1. 在 maven_controller中的pom.xml里面的dependencies中加入servlet依赖
  <!--依赖-->
<dependencies>
<!--junit的依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!--修改2:junit版本修改为4.12-->
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--添加maven_service的依赖-->
<dependency>
<groupId>com.xxx</groupId>
<artifactId>maven_service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--重点在这里:添加servlet的依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
  1. 在main下面新建文件夹java,输入名称java
    在这里插入图片描述
  2. 输入名称java
    在这里插入图片描述
  3. 把他作为资源文件
    在这里插入图片描述
  4. 新建包
    在这里插入图片描述
  5. 新建类UserServlet ,重写Servlet的service方法
package controller;
import com.xxx.service.UserService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/user")
public class UserServlet  extends HttpServlet { 

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 

// super.service(req, resp);
System.out.println("UserServlet test...");
//调用maven_service 模块方法
UserService.testService();
}
}
  1. 创建完成
    在这里插入图片描述

7 运行项目

1. WEB配置插件

1. 配置Tomcat插件

  1. 在maven_controller的pom.xml里面的build里面添加tomcat7插件
<build>
<finalName>maven_controller</finalName>
<!--修改3:删除 <pluginManagement> </pluginManagement> 全部内容-->
<!--插件-->
<plugins>
<!--重点看这 :tomcat 插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<!--配置-->
<configuration>
<!--<port>8080</port>-->
<path>/web</path>
<uriEncoding>UTF-8</uriEncoding>
<server>tomcat7</server>
</configuration>
</plugin>
</plugins>
</build>

2. 配置运行插件

  1. 在maven_controller的pom.xml里面的build里面添加maven-war-plugin插件,install打包时不会报错
 <!-- web项目需要加入当前代码-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
</plugin>

3. 更新

添加插件后,一定要更新,否则可能加载不到报错
在这里插入图片描述

2.添加命令tomcat运行命令

在这里插入图片描述

3.添加项目和子模块install 命令

1.添加项目和模块的install命令

在这里插入图片描述

2. install运行顺序

从底层开始:dao->service->controller->parent。install运行成功如下图
在这里插入图片描述

3. 项目运行结果

在这里插入图片描述

4. 访问生成的项目路径

当前路径为:http://localhost:8080/web
在这里插入图片描述

5. 访问WebServlet 路径

当前路径为: http://localhost:8080/web/user
在这里插入图片描述

6. 控制台打印结果

@WebServlet注解的使用可以直接访问/web/user, 控制台打印出信息,访问成功
在这里插入图片描述

七、 Mavent打包

web项目打包成功是一个war包
jar项目打包成功是一个jar包

1. 建立对应的目录结构

1. 添加java源文件夹

  1. 新建一个webapp项目,修改配置文件

  2. 选择项目的main文件夹,右键new,选择Directory,创建添加Java和Resources目录,并且标记为源文件
    在这里插入图片描述

  3. 添加对应的文件目录,及添加不同环境下对应的配置文件。(本地环境、测试环境、正式环境)

在这里插入图片描述

  1. 配置代码。db.properties (学习中,本配置模拟写的,不是真正的配置)。其他环境配置文件和这个一样
    在这里插入图片描述
    bean.xml随便配置的。(学习中,本配置模拟写的,不是真正的配置)。其他环境配置文件和这个一样
    在这里插入图片描述

2. 添加Profile配置

1. 在pom.xml添加配置

 <!--打包环境配置 开发环境 测试环境 正式环境-->
<profiles>
<!--开发环境 本地环境-->
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<!--未指定环境时,默认打包dev环境,这个配置放到哪里,就代表打包那个配置-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!--测试环境-->
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<!--正式环境-->
<profile>
<id>product</id>
<properties>
<env>product</env>
</properties>
</profile>
</profiles>

2. env配置说明

在这里插入图片描述

3. 设置资源文件配置

 <!--设置资源文件配置-->
<!--对于项目资源文件的配置放在build中-->
<resources>
<resource>
<!--可以去取properties文件的值-->
<directory>src/main/resources/${ 
env}</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include> <include>**/*.properties</include>
<include>**/*.tld</include> </includes> <!--不进行过滤--> <filtering>false</filtering> </resource> </resources> 

完整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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxx</groupId>
<artifactId>maven_package</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>maven_package Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<!--依赖-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>maven_package</finalName>
<!--设置资源文件配置-->
<!--对于项目资源文件的配置放在build中-->
<resources>
<resource>
<!--可以去取properties文件的值-->
<directory>src/main/resources/${ 
env}</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include> <include>**/*.properties</include>
<include>**/*.tld</include> </includes> <!--不进行过滤--> <filtering>false</filtering> </resource> </resources> <!--打包必须代码--> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.1</version> </plugin> </plugins> </build> <!--打包环境配置 开发环境 测试环境 正式环境--> <profiles> <!--开发环境 本地环境--> <profile> <id>dev</id> <properties> <env>dev</env> </properties> <!--未指定环境时,默认打包dev环境,这个配置放到哪里,就代表打包那个配置--> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <!--测试环境--> <profile> <id>test</id> <properties> <env>test</env> </properties> </profile> <!--正式环境--> <profile> <id>product</id> <properties> <env>product</env> </properties> </profile> </profiles> </project> 

4. 执行打包操作

1.创建打包命令

1. 创建普通打包命令

在这里插入图片描述

2. 指定打包文件打包命令

打包dev(开发、本地环境)代码: clean compile package -Pdev -Dmaven.test.skip=true
打包test(测试环境)代码: clean compile package -Ptest -Dmaven.test.skip=true
打包dev(正式环境)代码: clean compile package -Pproduct -Dmaven.test.skip=true
clean:清空缓存
compile:编译
package:打包
-Pdev:指定环境为dev
-Dmaven.test.skip=true:跳过测试文件

在这里插入图片描述

2. 打包成功,返回打包路径

在这里插入图片描述

3. 解压查看包

解压后返回的确实是打包的配置
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/196061.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号