首页 > Java > java教程 > 正文

掌握 Maven:超越构建管理

王林
发布: 2024-07-17 04:44:19
原创
278 人浏览过

Mastering Maven: Beyond Build Management

Maven 被广泛认为是一个强大的构建自动化工具,但它的作用远不止于此。它是一个全面的项目管理工具,可以简化整个构建过程、依赖关系管理和文档生成。在这篇文章中,我们将探索 Maven 的各个方面并详细了解其功能。

Maven 的主要特性
1.构建一代
2.依赖管理
3.文档

当您执行 mvn build 或 mvn deploy 等命令时,Maven 会查找包含所有配置的 pom.xml 文件,并采取相应的操作。让我们更深入地了解 pom.xml 结构及其意义。

POM 文件

``` 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">
4.0.0

<!-- Basic project information -->
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<!-- Properties -->
<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring.version>5.2.8.RELEASE</spring.version>
</properties>

<!-- Dependencies -->
<dependencies>
    <!-- Spring Core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- Spring Context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <!-- JUnit for testing -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<!-- Build configuration -->
<build>
    <plugins>
        <!-- Compiler plugin -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>${maven.compiler.source}</source>
                <target>${maven.compiler.target}</target>
            </configuration>
        </plugin>
        <!-- Surefire plugin for running tests -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
    </plugins>
</build>

<!-- Repositories -->
<repositories>
    <repository>
        <id>central</id>
        <url>https://repo.maven.apache.org/maven2</url>
    </repository>
</repositories>

<!-- Distribution management for deployment -->
<distributionManagement>
    <repository>
        <id>releases</id>
        <url>http://repo.mycompany.com/releases</url>
    </repository>
    <snapshotRepository>
        <id>snapshots</id>
        <url>http://repo.mycompany.com/snapshots</url>
    </snapshotRepository>
</distributionManagement>
登录后复制

让我们解码 POM 文件:

**pom.xml 的文件遵循特定的 XML 模式(在 xsi:schemaLocation: 中定义),确保它遵循 Maven 验证的正确结构。这是一个例子:


<project 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">



登录后复制

关键要素:

  • 父 POM:
    Spring Boot 中的每个 POM 文件都有一个父 POM。如果未定义父级,则超级 POM 将成为父级。

  • GroupId、ArtifactId 和版本:
    这些元素唯一标识 Maven Central 中的项目。


<groupId>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>


登录后复制
  • 属性: 定义可在整个 pom.xml 中引用的键值对。

<properties>
    <java.version>1.8</java.version>
</properties>

-

登录后复制
  • 存储库: 指定下载依赖项的位置,通常是从 Maven Central。

<repositories>
    <repository>
        <id>central</id>
        <url>https://repo.maven.apache.org/maven2</url>
    </repository>
</repositories>


登录后复制

依赖项:列出项目的依赖项。


<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.3.4.RELEASE</version>
    </dependency>
</dependencies>


登录后复制
  • 构建配置: 定义构建过程和阶段。 为此,我们需要了解 maven 的构建生命周期。

Maven 构建生命周期

Maven 遵循由几个阶段组成的特定构建生命周期:

  1. 验证:
    验证项目结构。

  2. 编译:
    将 Java 代码转换为字节码(.class 文件)并将其放置在 target/ 文件夹中。

  3. 测试:
    运行位于 test/ 目录中的测试用例。

  4. 套餐:
    从编译的字节码生成 JAR/WAR 文件并将它们存储在 target/ 文件夹中。

  5. 验证:
    检查包裹的完整性。

  6. 安装:
    在本地存储库中安装软件包。

  7. 部署:

    将包上传到远程存储库。

自定义构建过程:
Maven 提供了通过元素在每个阶段添加特定目标的灵活性。您还可以创建和使用插件来扩展 Maven 的功能。


<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>



登录后复制

*使用 maven 安装:*
完成测试、打包和验证后,它会安装我们在编译阶段创建的 jar 文件。
这里它创建了一个 m2 文件夹,其中保存了所有依赖项。我们可以从

下的 settings.xml 更改此文件夹位置

使用 Maven 部署
部署配置在元素内部指定。


<distributionManagement>
    <repository>
        <id>internal.repo</id>
        <url>http://repo.mycompany.com/maven2</url>
    </repository>
</distributionManagement>



登录后复制

提示:我们可以在 .m2/repository 文件夹内的 settings.xml 中提供凭据。

及时了解 Maven 并掌握其细微差别可以显着增强项目管理和构建自动化技能。请记住,Maven 不仅仅是一个构建工具,它还是项目的指挥中心。深入研究、探索新插件并不断尝试。我们与 Maven 的接触越多,我们的开发过程就会变得越强大。不断突破界限,让 Maven 处理剩下的事情 — 毕竟,这就像拥有一把瑞士军刀来满足您的项目管理需求!

非常感谢您的阅读,非常感谢您的宝贵反馈。
如果您遇到任何很酷的插件,也请在评论中告诉我。
别忘了点赞、分享和订阅。

以上是掌握 Maven:超越构建管理的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!