我正在尝试使用maven-shade-plugin生成Uber Jar,我想从阴影jar中排除一些资源,并包含一些指定的工件。但是以下排除资源与它捆绑在一起。

<dependencies>
    <dependency>
        <groupId>com.sample.auto</groupId>
        <artifactId>sample</artifactId>
        <version>1.5.0</version>
    </dependency>
    <dependency>
        <groupId>com.sample.manual</groupId>
        <artifactId>sample-manual</artifactId>
        <version>1.5.0</version>
    </dependency>
</dependencies>

<profiles>
    <profile>
        <id>Distribute</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.3</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    </transformer>
                                </transformers>
                                <filters>
                                    <filter>
                                        <artifact>*:*</artifact>
                                        <excludes>
                                            <exclude>META-INF/*.SF</exclude>
                                            <exclude>META-INF/*.DSA</exclude>
                                            <exclude>META-INF/*.RSA</exclude>
                                            <exclude>META-INF/LICENSE</exclude>
                                            <exclude>LICENSE</exclude>
                                            <exclude>com/myproject/auto/**</exclude>
                                            <exclude>org/**</exclude>
                                            <exclude>/*.png</exclude>
                                            <exclude>/*.html</exclude>
                                            <exclude>/*.jpeg</exclude>
                                            <exclude>com/google/common/**</exclude>
                                        </excludes>
                                    </filter>
                                </filters>
                                <shadedArtifactAttached>true</shadedArtifactAttached>
                                <shadedClassifierName>jar-with-dependencies</shadedClassifierName>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>


我还需要仅将com.sample.auto相关工件添加到我的uber jar中。请让我知道我在哪里弄错了。

最佳答案

理想情况下,查看代码时对工件的过滤器应为:

<filters>
    <filter>
        <artifact>*:*</artifact>
        <excludes>
            <exclude>META-INF/*.SF</exclude>
            <exclude>META-INF/*.DSA</exclude>
            <exclude>META-INF/*.RSA</exclude>
            <exclude>META-INF/LICENSE</exclude>
            <exclude>LICENSE</exclude> <!--if this is same as above, not required-->
            <exclude>/*.png</exclude>
            <exclude>/*.html</exclude>
            <exclude>/*.jpeg</exclude>
            <exclude>com.myproject.auto:**</exclude> <!--to exclude all the artifacts from com.myproject.auto group-->
            <exclude>org:**</exclude><!--exclude all artifacts under group org-->
            <exclude>com.google.common:**</exclude>
        </excludes>
    </filter>
</filters>


或者,如果您希望从相关工件中细化类,则可以应用多个过滤器,例如

<filters>
    <filter>
        <artifact>*:*</artifact>
        <excludes>
            <exclude>META-INF/*.SF</exclude>
            <exclude>META-INF/*.DSA</exclude>
            <exclude>META-INF/*.RSA</exclude>
            <exclude>META-INF/LICENSE</exclude>
            <exclude>LICENSE</exclude> <!--if this is same as above, not required-->
            <exclude>/*.png</exclude>
            <exclude>/*.html</exclude>
            <exclude>/*.jpeg</exclude>
        </excludes>
    </filter>
    <filter>
        <artifact>com.myproject.auto:*<artifact>
        <excludes>
            <exclude>com/myproject/auto/**</exclude> <!--to exclude a set of classes from the project structure-->
       </excludes>
    </filter>
    <filter>
        <artifact>org:*<artifact>
        <excludes>
            <exclude>org/**</exclude> <!--though the artifact naming needs to be refined for this-->
       </excludes>
    </filter>
    <filter>
        <artifact>com.google.common:**<artifact>
        <excludes>
            <exclude>com/google/common/**</exclude> <!--exclude classes from the respective artifacts-->
       </excludes>
    </filter>
</filters>


您自己链接的Apache文档上有一个非常有用的examples to exclude-include configurations

关于java - 如何从Maven Shade插件中删除特定文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46040071/

10-09 09:18