在maven插件的fat jar中可选择包含jar。

问题描述 投票:0回答:1

我有一个spark项目,需要2个外部的jar,我想把它们添加到应用jar中。当我创建可执行jar时,jar中不包含任何依赖关系,而当我创建fat jar时,所有的依赖关系都被添加,包括spark等。下面是我使用maven汇编插件创建的pom文件,我想在我的jar中只添加这两个jar。

<dependencies>

    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.11</artifactId>
        <version>2.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-sql_2.11</artifactId>
        <version>2.2.0</version>
    </dependency>

    <!-- Below dependencies need to be added in Application jar -->

    <dependency>
        <groupId>netacuity</groupId>
        <artifactId>common-netacuity-db</artifactId>
        <version>3.1.2</version>
    </dependency>
    <dependency>
        <groupId>netacuity</groupId>
        <artifactId>common</artifactId>
        <version>2.1.1</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <!-- get all project dependencies -->
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <!-- MainClass in mainfest make a executable jar -->
                <archive>
                    <manifest>
                        <mainClass>com....App</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>
java maven apache-spark maven-assembly-plugin
1个回答
4
投票

你可以使用 scope 为此。默认情况下 scopecompile 因此,当你包装时,所有的罐子都会被包括在内。

要包含一个罐子,你可以提供 scope 作为 compile 并保持 default

    <dependency>
        <groupId>netacuity</groupId>
        <artifactId>common-netacuity-db</artifactId>
        <version>3.1.2</version>
        <scope>compile</scope>
    </dependency>

要排除罐子,您可以更改 scopeprovided. 在运行时,这些罐子应该是可用的。

    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-sql_2.11</artifactId>
        <version>2.2.0</version>
        <scope>provided</scope>
    </dependency>
© www.soinside.com 2019 - 2024. All rights reserved.