为春季启动应用程序生成几个战争

时间:2015-12-29 08:59:59

标签: maven spring-boot

似乎spring boot会重新打包由maven包阶段生成的包,然后重新打包战争以使其可执行。

现在我想通过一个maven命令为不同的环境生成多个战争,我尝试使用maven-assembly-plugin

1 unzip the war generated by `spring-boot-maven` plugin to a directory
2 Assembly with the files in the directory, and add some other filtered resources
3 create the war

查看此帖子:generate multiple artifacts in maven

虽然它有效但我有多次战争,但java -jar xx.war无法执行任何战争。这些类似乎已损坏。

所以我想知道是否有替代解决方案?

更新我的pom.xml:

<project>
    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.1.RELEASE</version>
    </parent>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>src/main/as-common</source>
                                <source>src/main/as-server</source>
                                <source>src/main/as-app</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <!-- unzip the contents of the war(executeable) generated by spring-boot to a certain directory  -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.8</version>
                        <executions>
                            <execution>
                                <id>extract_spring_war</id>
                                <phase>package</phase>
                                <configuration>
                                    <target>
                                        <echo message="extract war generated by spring-boot-maven-plugin"/>
                                        <delete dir="${basedir}/target/${project.build.finalName}-spring" includeemptydirs="true"/>
                                        <unzip src="${basedir}/target/${project.build.finalName}.war" dest="${basedir}/target/${project.build.finalName}-spring/"/>
                                    </target>
                                </configuration>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>com.soebes.maven.plugins</groupId>
                        <artifactId>iterator-maven-plugin</artifactId>
                        <version>0.3</version>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>iterator</goal>
                                </goals>
                                <configuration>
                                    <items>
                                        <item>test</item>
                                        <item>dep1</item>
                                        <item>dep2</item>
                                    </items>
                                    <pluginExecutors>
                                        <pluginExecutor>
                                            <plugin>
                                                <groupId>org.apache.maven.plugins</groupId>
                                                <artifactId>maven-assembly-plugin</artifactId>
                                                <version>2.6</version>
                                            </plugin>
                                            <goal>single</goal>
                                            <configuration>
                                <archive>
                                    <manifest>
                                        <mainClass>org.springframework.boot.loader.WarLauncher</mainClass>
                                    </manifest>
                                </archive>

                                                <descriptors>
                                                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                                                </descriptors>
                                            </configuration>
                                        </pluginExecutor>
                                    </pluginExecutors>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

assmbly.xml:

<assembly>
    <id>${item}</id>
    <formats>
        <format>war</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <!-- file from the unpacked contents -->
        <fileSet>
            <directory>target/${project.build.finalName}-spring</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**/*</include>
            </includes>
            <filtered>true</filtered>
        </fileSet>

        <!-- add environment awared resources -->
        <fileSet>
            <outputDirectory>/WEB-INF/classes</outputDirectory>
            <directory>${basedir}/src/main/custom/</directory>
            <includes>
                <include>${item}.properties</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

更新

起初我得到了错误:

No main class detected

然后我为maven-assembly-plugin

添加以下内容
<archive>
    <manifest>
        <mainClass>org.springframework.boot.loader.WarLauncher</mainClass>
    </manifest>
</archive>

之后我重新打包战争,当我跑步时出错:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.ClassFormatError: Incompatible magic value 4022320623 in class file org/springframework/boot/loader/WarLauncher

1 个答案:

答案 0 :(得分:1)

当您重新打包war文件时,类文件已损坏。我怀疑是因为您正在过滤所有文件:

<fileSet>
    <directory>target/${project.build.finalName}-spring</directory>
    <outputDirectory>/</outputDirectory>
    <includes>
        <include>**/*</include>
    </includes>
    <filtered>true</filtered>
</fileSet>

您不想过滤二进制文件,因为它们可能恰好包含看起来像${}占位符的数据。您应该更新程序集以避免将过滤应用于类文件。最简单的方法是完全禁用过滤:

<fileSet>
    <directory>target/${project.build.finalName}-spring</directory>
    <outputDirectory>/</outputDirectory>
    <includes>
        <include>**/*</include>
    </includes>
    <filtered>false</filtered>
</fileSet>

如果需要过滤文本文件,则应使用两个fileSet。一个包含二进制文件并禁用过滤,一个包含文本文件并启用过滤。

相关问题