使用Maven程序集插件创建WAR文件

时间:2014-09-04 09:36:36

标签: java maven maven-assembly-plugin

是否可以使用 maven程序集插件创建war文件,或者此插件仅用于创建jar文件?

在Maven程序集插件中配置什么以创建war文件?

我不想使用maven war插件,因为我需要更多的war文件可能性(例如我的特定war文件的两个不同的输出目录,我的意思是目标路径和我maven中的另一条路径项目)。如果我用这个插件创建一个jar文件,使用maven程序集插件就没问题了。

1 个答案:

答案 0 :(得分:0)

在maven-assembly-plugin中,您定义.war文件的名称(我将其称为ROOT,因为它是主要应用程序)以及应该执行插件的阶段。 appendAssemblyId属性定义是否应在文件名后附加“ -war”(例如“ ROOT-war.war”)。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.1</version>
    <configuration>
        <descriptors>
            <descriptor>war-assembly.xml</descriptor>
        </descriptors>
        <finalName>ROOT</finalName>
        <appendAssemblyId>false</appendAssemblyId>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

需要在单独的xml文件中配置生成的.war文件的结构,该文件称为“ web-assembly.xml”(您可以指定任何名称)。在此描述符文件中,需要指定文件和目录以及.war文件中的相应输出目录。

<assembly>
    <id>war</id>
    <formats>
        <format>war</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>WEB-INF/lib</outputDirectory>
            <excludes>
                 <exclude>*:war</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/classes</directory>
            <outputDirectory>WEB-INF/classes</outputDirectory>
            <includes>
                <include>/**/*</include>
            </includes>
        </fileSet>
        ...
    </fileSets>
</assembly>

maven-assembly-plugin为您提供了很大的灵活性来构造输出工件。但是您还需要分别复制依赖项,类文件和资源,这可能非常复杂。 maven-war-plugin为您完成了所有这一切。

相关问题