簡體   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