如何用maven生成一个jar,其中包含项目中的指定类和依赖项中的指定类?

时间:2018-12-14 13:39:17

标签: java maven jar

我想生成一个jar,其中仅包含我的项目中的几个类及其依赖项之一。

schema of what I would like to achieve

由于依赖项也是我的项目之一,因此我目前设法通过jardesc文件来做到这一点,该文件包含我需要的所有类/包的路径。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jardesc>
    <jar path="C:/temp/myJar-4.0.0.jar"/>
    <options buildIfNeeded="true" compress="true" descriptionLocation="/ProjectB/src/main/resources/exportJar.jardesc" exportErrors="true" exportWarnings="true" includeDirectoryEntries="true" overwrite="true" saveDescription="true" storeRefactorings="false" useSourceFolders="false"/>
    <storedRefactorings deprecationInfo="true" structuralOnly="false"/>
    <selectedProjects/>
    <manifest generateManifest="true" manifestLocation="" manifestVersion="1.0" reuseManifest="false" saveManifest="false" usesManifest="true">
        <sealing sealJar="false">
            <packagesToSeal/>
            <packagesToUnSeal/>
        </sealing>
    </manifest>
    <selectedElements exportClassFiles="true" exportJavaFiles="false" exportOutputFolder="false">
        <javaElement handleIdentifier="=projectA/src\/main\/java&lt;com.stckvrflw.parsingTools"/>
        <javaElement handleIdentifier="=projectA/src\/main\/java&lt;com.stckvrflw.utils{ClassA.java"/>
        <javaElement handleIdentifier="=projectB/src\/main\/java&lt;com.test.myPackage{ClassE.java"/>
        <javaElement handleIdentifier="=projectB/src\/main\/java&lt;com.test.myPackage{ClassF.java"/>
    </selectedElements>
</jardesc>

是否可以在不复制类的情况下使用Maven做到这一点?

jar需要具有自己的artifactId和版本。

1 个答案:

答案 0 :(得分:1)

您可以使用maven-shade-plugin并进行如下配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
    <execution>
        <phase>package</phase>
        <goals>
        <goal>shade</goal>
        </goals>
        <configuration>
        <filters>
            <filter>
            <artifact>junit:junit</artifact>
            <includes>
                <include>junit/framework/**</include>
                <include>org/junit/**</include>
            </includes>
            <excludes>
                <exclude>org/junit/experimental/**</exclude>
                <exclude>org/junit/runners/**</exclude>
            </excludes>
            </filter>
            <filter>
            <artifact>*:*</artifact>
            <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
            </excludes>
            </filter>
        </filters>
        </configuration>
    </execution>
    </executions>
</plugin>

来源:maven-shade-plugin