带有 Maven Shade 插件的 Spring Boot - 未映射控制器(404 错误)

Posted

技术标签:

【中文标题】带有 Maven Shade 插件的 Spring Boot - 未映射控制器(404 错误)【英文标题】:Spring Boot With Maven Shade Plugin - Controllers not mapped (404 Error) 【发布时间】:2016-02-14 17:23:37 【问题描述】:

对于我的带有嵌入式 tomcat 的 Spring boot 应用程序,由于某些限制,我需要取消 spring-boot-maven-plugin 并需要使用 maven-shade-plugin。使用 maven package 命令,我可以成功创建 jar 文件。但是,我所有的控制器都停止工作并给我 404 错误。放置在我的资源文件夹中的静态内容也不再提供。总是出现 404 错误。

我的 pom.xml

<dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.2.7.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.7</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies> 

<build>
    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>$java.version</source>
                <target>$java.version</target>
            </configuration>
        </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>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>

                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>abc.MyMainClass</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>


    </plugins>
</build>

我照常运行应用程序:

@SpringBootApplication
public class MyMainClass

    public static void main(String[] args) 
        SpringApplication.run(MyMainClass.class, args);
       

我还必须创建以下 EmbeddedServletContainerFactory bean,否则会引发错误。

@Bean
public EmbeddedServletContainerFactory servletContainer() 
    TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
    return tomcat;

最后,我只有一个示例控制器,

@RequestMapping(value = "/car", method = RequestMethod.GET)
@ResponseBody
public Car test() 
    return new Car();

当我使用 spring-boot-maven 插件时,同样的代码可以正常工作。在我开始使用 maven-shade-plugin 后不久,tomcat 就成功启动了。但是,所有控制器都给我 404 错误。如果我做错了什么,请指导我。

【问题讨论】:

如果有人可以提供一个带有 maven shade 插件的 Spring Boot 工作示例,那将会很有帮助。谢谢。 【参考方案1】:

如果你想从 spring-boot-maven 插件迁移到 maven-shade 插件,这非常简单。我在 pom.xml 中添加了以下插件信息并解决了问题。

(https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-parent/1.2.6.RELEASE/spring-boot-starter-parent-1.2.6.RELEASE.pom)

依赖管理:

    <dependencyManagement>
<dependencies>
    <dependency>
        <!-- Import dependency management from Spring Boot -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>1.2.7.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
</dependencies>

构建插件:

<build>


    <plugins>


        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>$java.version</source>
                <target>$java.version</target>
            </configuration>
        </plugin>


        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>1.2.7.RELEASE</version>
                </dependency>
            </dependencies>
            <configuration>
                <keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
                <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>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer
                                implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
                                <resource>META-INF/spring.factories</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>sg.butterfly.emenu.api.config.EmenuApp</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

</build>

【讨论】:

【参考方案2】:

除了现有的,尝试使用AppendingTransformer,例如:

<transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>abc.MyMainClass</mainClass>
                            </transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                 <resource>META-INF/spring.handlers</resource>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                         <resource>META-INF/spring.schemas</resource>
                                </transformer>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                 <resource>META-INF/spring.tooling</resource>
                                </transformer>

参考文档:https://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#AppendingTransformer

【讨论】:

感谢您的快速回复。我试过了,但这不起作用。似乎我从 spring-boot-maven 插件迁移到 maven-shade-plugin 的方式是错误的。需要弄清楚如何正确配置它。

以上是关于带有 Maven Shade 插件的 Spring Boot - 未映射控制器(404 错误)的主要内容,如果未能解决你的问题,请参考以下文章

使用maven-shade-plugin打包maven项目,项目使用spring框架,打包成jar时报错,跪求大佬,弄了好久没出?

maven-shade-plugin插件

maven插件: shade, assembly

maven shade插件小记

maven-shade-plugin插件未生效原因分析

idea打包jar的多种方式,用IDEA自带的打包形式,用IDEA自带的打包形式 用Maven插件maven-shade-plugin打包,用Maven插件maven-assembly-plugin打