简体   繁体   English

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

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

I have 2 projects, project A and project B. Project A is dependent on project B. 我有两个项目,项目A和项目B。项目A依赖于项目B。

pom.xml of project A: 项目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>

Next, I compile my project and it creates a jar with all the dependencies that I don't need. 接下来,我编译我的项目,它会创建一个jar,其中包含我不需要的所有依赖项。 Created extra folders that do not have to be included in the jar file. 创建的额外文件夹不必包含在jar文件中。 I think these dependencies drag along with the dependency from project B. Using the dep.xml file, I indicate which folders you do not need to include in the jar file. 我认为这些依赖关系与项目B的依赖关系一起拖延。使用dep.xml文件,我指示不需要在jar文件中包括哪些文件夹。

dep.xml : 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>

This method is just awful. 这种方法太糟糕了。 Please tell me whether it is possible to add only classes from the dependency to the jar file and not to pull all these other folders. 请告诉我是否可以仅将依赖项中的类添加到jar文件中,而不能提取所有其他文件夹。

You can try using scope provided: 您可以尝试使用提供的范围:

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

You should take into account that, as maven documentation states: 正如maven文档所述,您应该考虑到这一点:

indicates you expect the JDK or a container to provide the dependency at runtime 表示您希望JDK或容器在运行时提供依赖项

You are creating a jar with dependencies, probably to run it standalone. 您正在创建具有依赖项的jar,可能要独立运行它。

So if A needs B, and B has dependencies on its own, A probably needs those as well to run. 因此,如果A需要B,并且B自身具有依赖关系,则A也可能也需要这些依赖才能运行。 It is therefore sensible to pack all the dependencies of B into the runnable jar. 因此,将B的所有依赖关系打包到可运行的jar中是明智的。

If B has unnecessary dependencies, these should be removed from B. 如果B有不必要的依赖关系,则应将其从B中删除。

If B has dependencies, that for some reason are not required when A uses B, you should add a proper exclusion to the dependency in the POM. 如果B具有依赖关系,则在A使用B时出于某种原因并不需要它,则应在POM中为依赖关系添加适当的排除。

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

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