使用IDEA创建spark项目,会用到 pom.xml的配置,现简单做个记录。
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>algorithm_strategy</artifactId>
<version>1.0</version>
</project>
所有 POM 文件都需要 project 元素和三个必需字段:groupId,artifactId,version。
节点 | 描述 |
---|---|
project | 工程的根标签。 |
modelVersion | 模型版本需要设置为 4.0。 |
groupId | 这是工程组的标识。它在一个组织或者项目中通常是唯一的。例如,一个银行组织 com.companyname.project-group 拥有所有的和银行相关的项目。 |
artifactId | 这是工程的标识。它通常是工程的名称。例如,消费者银行。groupId 和 artifactId 一起定义了 artifact 在仓库中的位置。 |
version | 这是工程的版本号。在 artifact 的仓库中,它用来区分不同的版本。例如:org.example:consumer-banking:1.0 org.example:consumer-banking:1.1
|
2. 引用常量定义
接下来可以为pom定义一些常量,在pom中的其它地方可以直接引用
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<scala.binary.version>2.11</scala.binary.version>
<spark.version>2.4.4</spark.version>
<scala.binary.version>2.11</scala.binary.version>
<scala.version>2.11.12</scala.version>
</properties>
3. Maven仓库
运行Maven的时候,Maven所需要的任何构件都是直接从本地仓库获取的。如果本地仓库没有,它会首先尝试从远程仓库下载构件至本地仓库,然后再使用本地仓库的构件。
3.1 本地仓库
Maven缺省的本地仓库地址为${user.home}/.m2/repository
。一个用户会对应的拥有一个本地仓库。
可以自定义本地仓库的位置,修改 /usr/local/apache-maven-3.6.3/conf/settings.xml
。
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
3.2 Maven中央仓库
Maven安装自带了一个远程仓库即Maven中央仓库。具体可在 /usr/local/apache-maven-3.6.3/lib/maven-model-builder-3.6.3.jar
文件里的 pom-4.0.0.xml
中进行查看。它是所有Maven POM的父POM,所有Maven项目继承该配置。中央仓库的id为central,远程url地址为http://repo.maven.apache.org/maven2,它关闭了snapshot版本构件下载的支持。
<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>
我们可以在POM中配置其它的远程仓库,比如下面的中国的中央仓库
<project>
...
<repositories>
<repository>
<id>maven-net-cn</id>
<name>Maven China Mirror</name>
<url>http://maven.net.cn/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>maven-net-cn</id>
<name>Maven China Mirror</name>
<url>http://maven.net.cn/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
...
</project>
我们也可以在<repositories>
下面添加多个<repository>
,每个<repository>
都有它唯一的id
,一个描述性的name
,以及最重要的,远程仓库的url
。此外,<releases><enabled>true</enabled></releases>
告诉Maven可以从这个仓库下载releases
版本的构件,而<snapshots><enabled>false</enabled></snapshots>
告诉Maven不要从这个仓库下载snapshot
版本的构件。禁止从公共仓库下载snapshot
构件是推荐的做法,因为这些构件不稳定,且不受你控制,你应该避免使用。当然,如果你想使用局域网内组织内部的仓库,你可以激活snapshot的支持。至于<pluginRepositories>
,这是配置Maven从什么地方下载插件构件(Maven的所有实际行为都由其插件完成)。该元素的内部配置和<repository>
完全一样。
在工作中我们一般都是使用公司局域网的远程仓库,使用该仓库能大大提高下载速度,继而提高构建速度。比如我们当前的spark项目配置的远程仓库为 (鉴于公司隐私,其中 url
被我替换了下)。
<repositories>
<repository>
<id>Central</id>
<url>http://nexus.org.example/content/repositories/central</url>
</repository>
<repository>
<id>Jcenter</id>
<url>http://nexus.org.example/content/repositories/Jcenter/</url>
</repository>
<repository>
<id>bili-nexus-release-server</id>
<url>http://nexus.org.example/content/repositories/releases</url>
</repository>
<repository>
<id>snapshots</id>
<url>http://nexus.org.example/content/repositories/snapshots</url>
</repository>
</repositories>
4. 项目依赖
由于当前主要基于 XGBoost
,所以主要引入相关的依赖。需要注意 scope
的参数,这里的值为 provided
,是因为当前的集群环境提供了这些依赖,所以打包的时候可以不用一起打包。但是在本地运行时需要将其注释掉 (此时为默认值:compile
)
<dependencies>
<!--scala-->
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<!-- spark -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_${scala.binary.version}</artifactId>
<version>${spark.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_${scala.binary.version}</artifactId>
<version>${spark.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-mllib_${scala.binary.version}</artifactId>
<version>${spark.version}</version>
<scope>provided</scope>
</dependency>
<!-- xgboost -->
<dependency>
<groupId>ml.dmlc</groupId>
<artifactId>xgboost4j-spark_${scala.binary.version}</artifactId>
<version>1.0.0</version>
<!--<scope>provided</scope>-->
</dependency>
</dependencies>
5. 构建插件
打包成 jar
的配置,maven-assembly-plugin
和 maven-jar-plugin
配合使用,最后就只会生成一个 xx-jar-with-dependencies.jar
。
<build>
<!-- <sourceDirectory>src/main/scala</sourceDirectory>-->
<!-- <testSourceDirectory>src/test/scala</testSourceDirectory>-->
<plugins>
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
<args>
<arg>-target:jvm-1.5</arg>
</args>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assemble</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>default-jar</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>