maven-shade-plugin 不会包含依赖项

maven-shade-plugin won't include dependencies

我有一个恼人的问题。我正在尝试使用 maven-shade-plugin 将带有嵌入式 tomcat 服务器的 spring mvc 应用程序打包到一个 jar 中。我已经尝试过类似的任务。试图在我的 pom 文件中使用类似的配置,因为曾经为我工作过。不幸的是,在打包之后,jar 只包含我创建的 类。清单确实提到了所有依赖项。我认为这可能是 Maven 的问题,但在另一个项目上尝试过,插件似乎工作正常。以下是我的 POM 的相关片段:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.wojto</groupId>
  <artifactId>wmcase</artifactId>
  <packaging>jar</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>WMcase Maven Webapp</name>

    <properties>
        <springframework.version>5.2.3.RELEASE</springframework.version>
        <springsecurity.version>5.1.4.RELEASE</springsecurity.version>
        <tomcat.version>9.0.30</tomcat.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

(...)

    <build>
        <finalName>wmcase</finalName>

        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.2.1</version>
                    <configuration>
                        <createDependencyReducedPom>true</createDependencyReducedPom>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>

                    </configuration>

                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <shadedArtifactAttached>true</shadedArtifactAttached>
                                <transformers>
                                    <transformer
                                            implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                                    <transformer
                                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <manifestEntries>
                                            <Main-Class>com.wojto.wmcase.application.Application</Main-Class>
                                        </manifestEntries>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix>lib/</classpathPrefix>
                                <mainClass>com.wojto.wmcase.application.Application</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

    </build>
</project>

删除标签 <pluginManagement></pluginManagement> 并保留其他标签,这样就可以了。

技巧很简单,因为 pluginManagement 顾名思义就是管理插件,通常是版本,有时是配置。这意味着 pluginManagement 中定义的插件永远不会被执行。

如果你想要执行的插件使用<plugins></plugins>

另请参阅文档:http://maven.apache.org/pom.html