在Java Maven项目中仅导入一个依赖项

时间:2016-05-08 15:19:01

标签: java maven

我需要从我的Java项目中创建一个jar,我需要它还包含其中一个依赖项的类文件,但不是全部。我使用了包含所有文件的maven-assembly-plugin并创建了一个巨大的jar,但我只需要

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk</artifactId>
    <version>1.10.45</version>
</dependency>

我也尝试过maven shade插件但没有结果:我最终会包含所有依赖项或排除所有依赖项

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <artifactSet>
                    <excludes>
                        <exclude>org.apache.spark:*</exclude>
                        <exclude>com.google.code.gson:*</exclude>
                        <exclude>edu.stanford.nlp:*</exclude>
                        <exclude>log4j:*</exclude>
                        <exclude>com.optimaize.languagedetector:*</exclude>
                        <exclude>info.debatty:*</exclude>
                    </excludes>
                </artifactSet>
            </configuration>
        </execution>
    </executions>
</plugin>

1 个答案:

答案 0 :(得分:0)

使用'provided'作为您不希望包含在最终jar文件中的依赖项的范围标记。

<dependency>
    <groupId>GROUP_ID</groupId>
    <artifactId>ARTIFACT_ID</artifactId>
    <version>VERSION</version>
    <scope>provided</scope>
</dependency>

使用maven-assembly-plugin构建jar

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>MAIN_CLASS</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>