Spring Boot和Maven Shade插件问题

时间:2018-07-27 21:54:37

标签: maven-shade-plugin spring-boot-maven-plugin

im试图在我的spring boot多模块项目中运行以下测试,并注意到maven shade插件避免spring查找依赖项。我注意到删除了以下工作,但是,这将导致应用程序启动失败,因为找不到主类。

<transformer
                                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                            <manifestEntries>
                                                <Main-Class>${application.mainClass}</Main-Class>
                                            </manifestEntries>
                                        </transformer>

下面是我的测试

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class AcceptanceBase extends Specification {

    // Wiremock
    protected static WireMockServer wireMockServer = new WireMockServer(
            options().port(8081).
                    usingFilesUnderDirectory("src/test/resources/wiremock").
                    notifier(new ConsoleNotifier(true)))

    // Rest assured<
    protected static RequestSpecification apiRequestSpec

    def setupSpec() {
        initSpec()
        wireMockServer.start()
    }

    def cleanupSpec() {
        wireMockServer.stop()
    }

    def initSpec() {
        apiRequestSpec = new RequestSpecBuilder()
                .addHeader("X-ApiKey", "5512c8d59932b3da984cc7de")
                .setContentType(ContentType.JSON)
                .setBaseUri("http://localhost:8086/api/flights/itineraries/composition")
                .build()
    }
}

使用maven配置

<plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-shade-plugin</artifactId>
                        <version>${maven-shade-plugin.version}</version>
                        <dependencies>
                            <dependency>
                                <groupId>org.springframework.boot</groupId>
                                <artifactId>spring-boot-maven-plugin</artifactId>
                                <version>${spring-boot.version}</version>
                            </dependency>
                        </dependencies>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>shade</goal>
                                </goals>
                                <configuration>
                                    <createDependencyReducedPom>false</createDependencyReducedPom>
                                    <transformers>
                                        <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.ManifestResourceTransformer">
                                            <manifestEntries>
                                                <Main-Class>${application.mainClass}</Main-Class>
                                            </manifestEntries>
                                        </transformer>
                                        <transformer
                                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                            <resource>META-INF/spring.*</resource>
                                        </transformer>
                                    </transformers>
                                    <filters>
                                        <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>

<plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${maven-surefire-plugin.version}</version>
                    <configuration>
                        <skipTests>${skip.surefire.tests}</skipTests>
                        <useFile>false</useFile>
                        <includes>
                            <!-- By default only files ending in 'Test' will be included, so also include support for Spock style naming convention -->
                            <!-- Oddly enough for Groovy files, *Spec.groovy does not work, but *Spec.java does -->
                            <include>**/*Test.java</include>
                            <include>**/*Spec.java</include>
                        </includes>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>${maven-failsafe-plugin.version}</version>
                    <configuration>
                        <skipTests>${skip.failsafe.tests}</skipTests>
                    </configuration>
                    <executions>
                        <execution>
                            <id>integration-test</id>
                            <goals>
                                <goal>integration-test</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>verify</id>
                            <goals>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

我想了解为什么会发生这种情况以及是否有解决方法。弹簧启动多模块布局可能存在问题,因为应用程序类不在主项目中,而在内部模块中。

api-name
-> api-domain
-> api-interfaces
-> api-application
-> api-services (Application.class is situated here)

0 个答案:

没有答案