文章目录

  • 一、什么是 Maven
  • 二、Maven 的优缺点
  • 三、Maven 的应用场景
  • 四、Maven 环境搭建
  • 五、Maven 构建


一、什么是 Maven

Maven是一个开源的项目管理和构建工具,用于帮助开发人员自动化构建、测试和部署Java项目。它提供了一种标准化的项目结构和一套约定,使得项目的构建和依赖管理变得简单且易于维护。

Maven基于项目对象模型(Project Object Model,POM)来管理项目。POM是一个XML文件,描述了项目的依赖关系、构建配置、插件配置等信息。通过定义POM,Maven可以自动下载所需的依赖库、执行项目构建和测试,并生成可部署的项目输出。

Maven提供了一系列命令和生命周期(Lifecycle)来管理项目的构建过程。开发人员可以使用Maven命令来执行特定的构建任务,如编译代码、运行测试、打包项目等。同时,Maven还支持插件机制,可以通过插件扩展和定制项目的构建过程。
Maven还提供了中央仓库(Central Repository)作为默认的依赖库存储和分享平台。开发人员可以在POM中声明所需的依赖库,Maven会自动从中央仓库下载并管理这些依赖库。

二、Maven 的优缺点

Maven 的优点

  1. 依赖管理:Maven 提供了集中化的依赖管理系统。它简化了管理项目依赖的过程,自动从中央仓库下载和管理所需的库文件。
  2. 构建自动化:Maven 自动化了构建过程,使得编译、测试、打包和部署项目更加简单。它采用约定优于配置的方式,减少了手动配置的需求。
  3. 标准化项目结构:Maven 鼓励采用标准化的项目结构,使得项目更易于理解和维护。它定义了一套规范,使得不同的 Maven 项目具有一致的结构和约定。
  4. 插件生态系统:Maven 拥有丰富的插件生态系统,可以通过插件扩展和定制构建过程,满足各种特定需求。

Maven 的缺点

  1. 学习曲线:对于初学者来说,Maven 可能有一定的学习曲线,需要了解其配置文件和命令行工具的使用。
  2. 网络依赖:Maven 在构建过程中依赖于中央仓库和其他远程仓库,如果网络连接不稳定或无法访问这些仓库,可能会导致构建失败。
  3. 构建速度:在项目初始化阶段,Maven 需要下载和安装所需的依赖库,这可能需要一些时间,特别是在首次构建时。
  4. 配置复杂性:对于一些特殊需求或非标准项目结构,Maven 的配置可能会变得复杂,需要更多的配置和定制。

三、Maven 的应用场景

Maven的应用场景非常广泛,适用于各种Java项目和开发环境。以下是一些常见的Maven应用场景:

  1. 项目构建和管理:Maven是一个强大的项目构建工具,可以自动化构建过程,包括编译、测试、打包和部署。它可以管理项目的依赖关系,简化项目的构建和管理。
  2. 依赖管理:Maven提供了一个集中化的依赖管理系统,可以自动下载和管理项目所需的库文件。它可以从中央仓库或自定义仓库获取依赖,确保项目的依赖关系正确和一致。
  3. 多模块项目:Maven支持多模块项目的构建和管理。它可以将一个大型项目拆分成多个模块,每个模块可以独立构建和测试,同时可以通过父子模块关系进行依赖管理。
  4. 发布和分发:Maven可以帮助项目发布和分发,将构建好的项目打包成可执行的文件、库文件或发布到远程仓库。它可以生成标准的发布文件,方便其他开发人员或团队使用。
  5. 文档生成:Maven可以生成项目的文档,包括API文档、用户手册等。它可以集成文档生成工具,如JavaDoc和Doxygen,自动生成项目文档。
  6. 持续集成:Maven可以与持续集成工具(如Jenkins、Travis CI等)集成,实现自动化构建、测试和部署。它可以通过配置持续集成流水线,实现项目的快速迭代和交付。

Maven适用于各种Java项目和开发环境,特别是需要构建自动化、依赖管理和多模块项目管理的场景。它提供了丰富的功能和插件生态系统,可以帮助开发人员提高开发效率和项目质量。

四、Maven 环境搭建

要搭建 Maven 环境,你需要按照以下步骤进行操作:

  1. 下载并安装 Java 开发工具包(JDK):Maven 需要在你的系统上安装 Java。你可以从 Oracle 官网下载最新版本的 JDK,并按照安装说明进行安装。
  2. 下载并安装 Apache Maven:你可以从 Maven 官网下载最新版本的 Maven 压缩包。解压缩该压缩包到你选择的目录。
  3. 配置环境变量:将 Maven 的安装目录添加到系统的环境变量中。在 Windows 系统中,你可以在“系统属性 -> 高级系统设置 -> 环境变量”中设置。将 Maven 的 bin 目录路径添加到 PATH 变量中。
  4. 验证安装:在命令行中输入 mvn -v 命令,如果正确显示了 Maven 的版本信息,则表示安装成功。
  5. 配置 Maven 的 settings.xml 文件(可选):你可以编辑 Maven 的 settings.xml 文件来配置一些个性化的设置,比如镜像源、代理等。
  6. maven javac版本设置_java


  7. maven javac版本设置_maven javac版本设置_02

  8. setting.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 http://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>
  -->
<localRepository>D:\software\apache-maven-3.2.3\.m2</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>


      <mirror>
          <id>central</id>
          <name>central</name>
          <url>file://D:\software\apache-maven-3.2.3\.m2</url>
          <mirrorOf>*</mirrorOf>
      </mirror>

      <mirror>
          <id>alimaven</id>
          <name>aliyun maven</name>
          <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
          <mirrorOf>*</mirrorOf>
      </mirror>
      <mirror>
          <id>alimaven</id>
          <mirrorOf>*</mirrorOf>
          <name>aliyun maven</name>
          <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
      </mirror>

   
   <mirror>
       <id>aliyun-central</id>
       <mirrorOf>*</mirrorOf>
       <name>aliyun central</name>
       <url>https://maven.aliyun.com/repository/central</url>
   </mirror>

 
 <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>
        <id>nexusProfile</id>
        <repositories>
            <repository>
                <id>nccloud-snapshots</id>
                <name>nccloud Snapshots Repository</name>
                <url>http://maven.yonyou.com/nexus/content/repositories/nccloud-snapshots/</url>
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
    </profile>






    <!-- 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>
<activeProfile>nexusProfile</activeProfile>
</activeProfiles>
  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
</settings>

五、Maven 构建

以下是使用 Maven 进行构建的一般步骤:

  1. 创建 Maven 项目:使用 Maven 的命令行工具或集成开发环境(IDE)创建一个新的 Maven 项目。这将创建一个基本的项目结构,包括 pom.xml 文件和 src 目录。
  2. 配置 pom.xml 文件:pom.xml 是 Maven 项目的核心配置文件,定义了项目的依赖、插件和构建目标等信息。你可以在 pom.xml 文件中添加和管理项目的依赖项,指定构建过程中需要执行的插件,以及设置其他构建相关的配置。
  3. 定义项目依赖:在 pom.xml 文件中使用 <dependencies> 元素定义项目的依赖项。你可以指定依赖的坐标(groupId、artifactId 和版本号),Maven 将自动下载并管理这些依赖项。
  4. 执行构建命令:使用 Maven 的命令行工具或 IDE 提供的 Maven 插件执行构建命令。常见的构建命令包括 clean、compile、test、package 等。例如,使用命令 mvn clean package 将清理项目目录并打包构建项目。
  5. 运行测试:Maven 提供了内置的测试框架,可以执行单元测试和集成测试。你可以在项目中编写测试用例,并使用 Maven 的命令 mvn test 运行测试。
  6. 部署项目:使用 Maven 的命令 mvn deploy 可以将构建的项目部署到远程仓库或服务器。你可以在 pom.xml 文件中配置部署目标的详细信息。