3

我正在构建一个既可以在本地调试又可以部署到集群的 Apache Spark 应用程序。为此,我必须定义它对 spark-core(Java/scala 库)的依赖关系,以满足以下要求。

包含在编译中(否则编译失败)包含在运行和测试中(用于本地调试和单元测试)排除在组装中(用于部署到具有提供的 spark-core 的集群,这将 jar 大小减少了 70M,我正在使用maven-shade 插件来生成包罗万象的 jar,因为存在一些无法使用 maven-assembly 解决的 jar 地狱问题)

不幸的是,maven 本身并不支持自定义范围。有没有办法使用一些插件来启用它?

4

3 回答 3

1

我们在我们的 maven 构建中正是这样做的:将 Spark 程序集排除在作业程序集中。maven-shade我们在插件配置中添加排除规则。

<configuration>
    <shadedArtifactAttached>true</shadedArtifactAttached>
    <shadedClassifierName>jar-with-dependencies</shadedClassifierName>
        <artifactSet>
            <excludes>
                <exclude>org.apache.spark:spark-assembly</exclude>
            </excludes>
        </artifactSet>
...
</configuration>
于 2014-07-15T10:24:09.877 回答
0

您可以使用范围属性(已提供)进行依赖。

这很像 compile,但表明您希望 JDK 或容器在运行时提供依赖项。例如,在为 Java 企业版构建 Web 应用程序时,您可以将 Servlet API 和相关 Java EE API 的依赖设置为提供的范围,因为 Web 容器提供了这些类。此范围仅在编译和测试类路径上可用,并且不可传递。

参考:http ://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

例如:

<dependency>
  <groupId>group-a</groupId>
  <artifactId>artifact-b</artifactId>
  <version>1.0</version>
  <type>bar</type>
  <scope>provided</scope>
</dependency>
于 2014-07-15T19:25:04.447 回答
0

您应该创建 2 个配置文件。1 用于您在编译范围内使用 spark 的想法(默认),另一个在构建期间使用(具有提供的范围)。

<profiles>
    <profile>
        <id>default-without-spark</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <dependencies>
            <dependency>
                <groupId>org.apache.spark</groupId>
                <artifactId>spark-core_2.11</artifactId>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    </profile>
    <profile>
        <id>dev</id>
        <dependencies>
            <dependency>
                <groupId>org.apache.spark</groupId>
                <artifactId>spark-core_2.11</artifactId>
            </dependency>
        </dependencies>
    </profile>
</profiles>

你会得到你想要的,而不会受到@maasg 解决方案的不利影响(所有 spark 传递依赖项都添加到你的最终 jar 中)

于 2016-10-06T11:31:36.943 回答