构建工具-Maven详解

李捷
2023-12-01

博客地址

概览

Home Page: https://maven.apache.org/
Source Code: https://gitbox.apache.org/repos/asf/maven.git
Issue Tracking: https://issues.apache.org/jira/browse/MNG
https://maven.apache.org/plugins/
Maven仓库:https://mvnrepository.com/

构建工具发展史:
Make -> Ant -> Maven -> Gradle

  • Eclipse + Ant 组合的时候,引入库都是下载jar包或aar包放到lib目录下,然后添加引用。!!!不能实时更新。
  • Android Studio + Gradle 组合中gradle中提供了可以从远端拉取jar包和aar包引入本地。

Maven仓库分为两类:本地仓库远程仓库

  • 本地仓库就是本地存放jar包的位置,Maven的本地仓库在最开始的时候并不会创建,而是第一次启动Maven时,在当前用户的文件夹下建立一个**.m2**文件,其中存放Maven本地仓库的所有jar包。
  • 远程仓库又分为:中央仓库Nexus私服其他公共仓库

安装与配置

https://maven.apache.org/download.html
配置环境变量:

  • MAVEN_HOME——安装路径
  • PATH——%MAVEN_HOME%\bin;
  • 【mvn -v】命令行输入,查看配置是否成功。

配置Maven:

  1. 打开maven的安装目录,找到文件\conf\settings.xml
  2. 标签localRepository,配置本地仓库存储路径。
  3. 标签mirror,配置Maven镜像。
<!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
    -->
<localRepository>E:\mavenRepository</localRepository>

Maven镜像:
https://maven.aliyun.com/

<mirror>
  <id>alimaven</id>
  <mirrorOf>central</mirrorOf>
  <name>aliyun maven</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>
<mirror>
  <id>repo2</id>
  <mirrorOf>central</mirrorOf>
  <name>repo2 maven</name>
  <url>http://repo2.maven.org/maven2</url>
</mirror>

POM

概览

http://maven.apache.org/pom.html
POM stands for “Project Object Model”.It is an XML representation of a Maven project held in a file named pom.xml.

<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">
  <!--当前仅有的可以被Maven 2 & 3同时支持的POM版本-->                    
  <modelVersion>4.0.0</modelVersion>
 
  <!-- The Basics -->
  <groupId>...</groupId>
  <artifactId>...</artifactId>
  <version>...</version>
  <!--The current core packaging values are: pom, jar, maven-plugin, ejb, war, ear, rar.-->
  <packaging>...</packaging>
  <dependencies>...</dependencies>
  <parent>...</parent>
  <dependencyManagement>...</dependencyManagement>
  <modules>...</modules>
  <properties>...</properties>
 
  <!-- Build Settings -->
  <build>...</build>
  <reporting>...</reporting>
 
  <!-- More Project Information -->
  <name>...</name>
  <description>...</description>
  <url>...</url>
  <inceptionYear>...</inceptionYear>
  <licenses>...</licenses>
  <organization>...</organization>
  <developers>...</developers>
  <contributors>...</contributors>
 
  <!-- Environment Settings -->
  <issueManagement>...</issueManagement>
  <ciManagement>...</ciManagement>
  <mailingLists>...</mailingLists>
  <scm>...</scm>
  <prerequisites>...</prerequisites>
  <repositories>...</repositories>
  <pluginRepositories>...</pluginRepositories>
  <distributionManagement>...</distributionManagement>
  <profiles>...</profiles>
</project>
<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <exclusions>
        <exclusion>
          <groupId>org.apache.maven</groupId>
          <artifactId>maven-core</artifactId>
        </exclusion>
      </exclusions>
      <type>jar</type>
      <scope>test</scope>
      <optional>true</optional>
    </dependency>
    ...
</dependencies>

The Super POM

Maven有一个超级pom,所有的pom文件均继承此文件。
路径:
lib\maven-model-builder-3.5.4.jar\org\apache\maven\model\pom-4.0.0.xml

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

<!-- START SNIPPET: superpom -->
<project>
  <modelVersion>4.0.0</modelVersion>

  <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

  <pluginRepositories>
    <pluginRepository>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
    </pluginRepository>
  </pluginRepositories>

  <build>
    <directory>${project.basedir}/target</directory>
    <outputDirectory>${project.build.directory}/classes</outputDirectory>
    <finalName>${project.artifactId}-${project.version}</finalName>
    <testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
    <sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
    <scriptSourceDirectory>${project.basedir}/src/main/scripts</scriptSourceDirectory>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
    <resources>
      <resource>
        <directory>${project.basedir}/src/main/resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>${project.basedir}/src/test/resources</directory>
      </testResource>
    </testResources>
    <pluginManagement>
      <!-- NOTE: These plugins will be removed from future versions of the super POM -->
      <!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453) -->
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.5.3</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

  <reporting>
    <outputDirectory>${project.build.directory}/site</outputDirectory>
  </reporting>

  <profiles>
    <!-- NOTE: The release profile will be removed from future versions of the super POM -->
    <profile>
      <id>release-profile</id>

      <activation>
        <property>
          <name>performRelease</name>
          <value>true</value>
        </property>
      </activation>

      <build>
        <plugins>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-sources</id>
                <goals>
                  <goal>jar-no-fork</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-javadoc-plugin</artifactId>
            <executions>
              <execution>
                <id>attach-javadocs</id>
                <goals>
                  <goal>jar</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <inherited>true</inherited>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
              <updateReleaseInfo>true</updateReleaseInfo>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

</project>
<!-- END SNIPPET: superpom -->

Maven服务源

  • JCenter
    托管在Bintray上面的Maven仓库。
  • Maven Central
    托管在Sonatype上面的Maven仓库。
  • JitPack

JCenter优点:

  • 基于CDN分布函数库,JCenter提供了更快的下载速度。
  • JCenter是最大的Java仓库,可以说Maven Central是JCenter的一个子集,托管在Maven Central中的函数库,几乎都托管在JCenter上面。
  • 上传简单,Bintray的用户界面对用户友好。

获取函数库的原理

完整的函数库依赖字符串包含三部分:GROUP_ID:ARTIFACT_ID:VERSION
Gradle根据依赖配置,向Maven Repository服务器查询是否存在该版本的函数库,如果存在,则会根据服务器类型拼接下载请求url。

  • JCenter:
    http://jcenter.bintray.com/…
  • Maven Central:
    https://oss.sonatype.org/content/repositories/releases…

Maven私服

私服是架设在局域网内的远程仓库,目的是代理远程仓库及部署第三方构件。
优点:

  • 节省外网带宽:减少大量对远程仓库的重复请求
  • 加速maven构建
  • 部署第三方构件:构件无法从远程仓库获取,如公司内部生成的私有构件。

Sonatype Nexus

  1. https://www.sonatype.com/download-oss-sonatype
  2. 自定义参数etc\nexus-default.properties
  3. 启动nexus

默认端口:8081
http://127.0.0.1:8081/
http://localhost:8081/
默认配置文件:C:\swTools\nexus-3.13.0-01\etc\nexus-default.properties
默认登录:admin/admin123

常用命令:
【nexus.exe/install】安装nexus为系统服务
【nexus.exe/uninstall】卸载nexus为系统服务
【nexus.exe/start】启动nexus服务
【nexus.exe/stop】停止nexus服务
【nexus.exe/run】启动服务

Apache Archiva

JFrog Artifactory

历史版本

https://maven.apache.org/docs/history.html

 类似资料: