【问题标题】:How to generate a jar with maven containing specified classes from a project and specified classes from its dependencies?如何使用 maven 生成一个 jar,其中包含项目中的指定类和其依赖项中的指定类?
【发布时间】:2018-12-14 13:39:17
【问题描述】:

我想生成一个 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 和版本。

【问题讨论】:

    标签: java maven jar


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2015-07-12
      • 1970-01-01
      • 2015-01-16
      • 2023-03-16
      • 2014-02-09
      • 1970-01-01
      • 2020-06-02
      • 2010-12-16
      • 1970-01-01
      相关资源
      最近更新 更多