繁体   English   中英

仅使用Maven依赖项类构建jar文件

[英]Build a jar file with only maven dependency classes

我有两个项目,项目A和项目B。项目A依赖于项目B。

项目A的pom.xml

<dependencies>
    <dependency>
        <groupId>nft.dcs</groupId>
        <artifactId>B</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
</dependencies>

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.1</version>
    <configuration>
        <appendAssemblyId>true</appendAssemblyId>

        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>

    <executions>
        <execution>
            <id>make-vision-plugin</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>src/assembly/dep.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

接下来,我编译我的项目,它会创建一个jar,其中包含我不需要的所有依赖项。 创建的额外文件夹不必包含在jar文件中。 我认为这些依赖关系与项目B的依赖关系一起拖延。使用dep.xml文件,我指示不需要在jar文件中包括哪些文件夹。

dep.xml

<id>with-spec</id>
    <formats>
        <format>jar</format>
    </formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <useProjectArtifact>false</useProjectArtifact>
        <unpack>true</unpack>
        <unpackOptions>
            <excludes>
                <exclude>nft/**</exclude>
                <exclude>com/**</exclude>
                <exclude>impl/**</exclude>
                <exclude>javax/**</exclude>
                <exclude>junit/**</exclude>
                <exclude>org/**</exclude>
                <exclude>oshi/**</exclude>
                <exclude>sound/**</exclude>
            </excludes>
        </unpackOptions>
        <scope>runtime</scope>
    </dependencySet>
</dependencySets>
<fileSets>
    <fileSet>
        <directory>${project.build.outputDirectory}</directory>
        <outputDirectory>/</outputDirectory>
    </fileSet>
</fileSets>

这种方法太糟糕了。 请告诉我是否可以仅将依赖项中的类添加到jar文件中,而不能提取所有其他文件夹。

您可以尝试使用提供的范围:

<dependencies>
    <dependency>
        <groupId>nft.dcs</groupId>
        <artifactId>B</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

正如maven文档所述,您应该考虑到这一点:

表示您希望JDK或容器在运行时提供依赖项

您正在创建具有依赖项的jar,可能要独立运行它。

因此,如果A需要B,并且B自身具有依赖关系,则A也可能也需要这些依赖才能运行。 因此,将B的所有依赖关系打包到可运行的jar中是明智的。

如果B有不必要的依赖关系,则应将其从B中删除。

如果B具有依赖关系,则在A使用B时出于某种原因并不需要它,则应在POM中为依赖关系添加适当的排除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM