java—将特定的依赖项打包到jar中

jv4diomz  于 2021-05-30  发布在  Hadoop
关注(0)|答案(2)|浏览(331)

我有一个项目有很多hadoop依赖项和org.json依赖项。我只想将org.json依赖项打包到jar中,因为打包hadoop依赖项会导致冲突。我使用的是packaging=jar,而使用胖jar不适合我的情况。有没有办法只打包org.json依赖项?my pom.xml dependencies部分:

<dependencies>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-hdfs</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-auth</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-core</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>

    <!-- JSON Parser dependency -->
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
    </dependency>

    <!-- Log4J dependency -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
    </dependency>

</dependencies>
mctunoxg

mctunoxg1#

回答我自己的问题,也许这就是托比ørn ravn andersen的意思是:对于所有的依赖项,我不想被打包并提供。例子:

<dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-core</artifactId>
            <version>2.0.0-mr1-cdh4.0.1</version>
            <scope>provided</scope>
        </dependency>

把它 Package 成一个肥jar:

<!-- Maven Assembly Plugin -->
        <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.myorg.MyAppMain</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>

编辑:如果运行时依赖项也被排除在类路径之外,那么项目就不能在我的ide上运行。

xvw2m8pv

xvw2m8pv2#

你可以使用maven shade插件,详细说明什么去了什么不去了;如果不想,在使用shade时不必重命名任何内容。

相关问题