pom.xml - 打包能够添加外部依赖

博客围绕Maven展开,但具体内容缺失。Maven是开发工具领域重要工具,可用于项目构建、依赖管理等。
<?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>org.example</groupId>
    <artifactId>sparkLearning</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>sparkLearning</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>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <spark.version>3.1.2</spark.version>
        <hadoop.version>3.2.2</hadoop.version>
        <!-- 指定主类 -->
        <main.name>com.example.learnSpark01</main.name>
        <maven.plugins.name>2.4.1</maven.plugins.name>

    </properties>


    <!-- (((((((((((((((((((((((((((((((((((((( 引入依赖 ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) -->
    <dependencies>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>${maven.plugins.name}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>

        <!-- ******************************************** Hadoop Java ************************************************* -->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
        </dependency>

        <!-- ******************************************** Spark Java ************************************************* -->
        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-core_2.12</artifactId>
            <version>${spark.version}</version>
        </dependency>


    </dependencies>


    <build>
        <plugins>
            <!-- Maven Assembly Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>${maven.plugins.name}</version>
                <configuration>
                    <!-- get all project dependencies -->
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <!-- MainClass in mainfest make a executable jar -->
                    <archive>
                        <manifest>
                            <mainClass>${main.name}</mainClass>
                        </manifest>
                    </archive>

                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <!-- bind to the packaging phase -->
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>

 

### 如何在 Maven `pom.xml` 中配置打包所需的依赖项 在 Maven 项目中,`pom.xml` 文件用于描述项目的构建、管理和依赖关系。为了正确配置打包所需的依赖项,可以通过 `<dependencies>` 和 `<build>` 部分实现。 #### 1. 添加所需依赖到 `<dependencies>` Maven 使用 `<dependency>` 元素来声明项目运行时所需要的库或框架。以下是常见的依赖配置方式: ```xml <dependencies> <!-- Spring Core Dependency --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.10</version> <scope>compile</scope> </dependency> <!-- JUnit Testing Framework --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies> ``` 上述代码片段展示了如何添加两个不同的依赖:Spring Core 库和 JUnit 测试框架[^1]。注意每个依赖都包含以下字段: - **`<groupId>`**: 定义组织或公司名称。 - **`<artifactId>`**: 表示具体的模块或组件名。 - **`<version>`**: 明确指定版本号。 - **`<scope>`**: 可选参数,默认为 `compile`,其他常见值有 `provided`, `runtime`, `test` 等[^2]。 #### 2. 设置打包类型 (`packaging`) Maven 支持多种类型的包(如 jar, war),需通过根标签中的 `<packaging>` 属性设定默认的打包形式。如果未显式定义,则会采用默认值 `jar`。 ```xml <packaging>war</packaging> ``` 此例子表明该项目将以 WAR 文件的形式被打包并部署至应用服务器上[^3]。 #### 3. 自定义构建阶段 (Build Section) 有时仅靠简单的依赖不足以满足复杂需求,可能还需要引入额外插件完成特定任务比如编译源码或者生成文档等操作。下面是一个典型的应用场景——利用 maven-shade-plugin 执行 fat-jar 构建流程的例子: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 这里我们指定了一个名为 `maven-shade-plugin` 的工具,在 package 生命周期期间自动将所有类路径上的资源合并成单一可执行 JAR 文件。 --- #### 总结 综上所述,要成功地在 Maven 工程里加入必要的外部支持以及调整其行为模式,就需要合理运用好三个关键区域:<dependencies>, <build>, 还有可能涉及更深层次的内容像 profiles 或者 properties 板块等等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Han_Lin_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值