【问题标题】:Maven, assembly plugin, multi-module project and module dependenciesMaven、程序集插件、多模块项目和模块依赖
【发布时间】:2011-03-09 12:20:52
【问题描述】:

我正在构建一个多模块 Maven 项目,其主要产品是 WAR 文件,并且我正在尝试将该文件与相关的发布相关工件(README、发布说明、许可证文件等)组装到用于分发的 ZIP。但是当我尝试构建分发文件时,它失败了。我究竟做错了什么?请注意,在要求foo-distribution 模块构建分发文件时,我已经检查了foo-bar.1.0.war 工件是否存在;我知道将所有内容都放在foo 的 POM 中是行不通的。 (我有一个解决方法;我可以使用相对路径直接指向相对于 foo-distribution 模块根目录分发的文件的位置。这不是我喜欢的解决方案,因为它的代码味道很差。)

另请注意,我总是从根项目构建,并且我在分发构建器中使用 assembly:single(如 Maven 文档中所建议的那样)。

项目布局

foo
+ src
| + README.txt
+ foo-bar
| + target
|   + foo-bar.1.0.war
+ foo-distribution
  + src
    + assemble
      + dist.xml

foofoo-distribution 具有 pom 包装,foo-bar 具有 war 包装。 (实际代码中还有其他模块,但在foo-distribution模块之前也构造正确。)

foo POM

<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>
    <groupId>foobar</groupId>
    <artifactId>foo</artifactId>
    <packaging>pom</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <modules>
        <module>foo-bar</module>
        <module>foo-distribution</module>
    </modules>
</project>

foo-distribution POM

<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <artifactId>foo</artifactId>
        <groupId>foobar</groupId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>..</relativePath>
    </parent>
    <groupId>foobar</groupId>
    <artifactId>foo-distribution</artifactId>
    <packaging>pom</packaging>

    <dependencies>
        <dependency>
            <groupId>foobar</groupId>
            <artifactId>foo-bar</artifactId>
            <version>1.0</version>
            <type>war</type>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2-beta-5</version>
                <configuration>
                    <finalName>foobar</finalName>
                    <descriptors>
                        <descriptor>src/assemble/dist.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-t2server-distribution</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

dist.xml

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0">
    <id>distribution</id>
    <formats>
        <format>zip</format>
    </formats>
    <moduleSets>
        <moduleSet>
            <includes>
                <include>foobar:foo</include>
            </includes>
            <sources>
                <fileSets>
                    <fileSet>
                        <directory>src</directory>
                        <includes>
                            <include>*.txt</include>
                        </includes>
                    </fileSet>
                </fileSets>
            </sources>
        </moduleSet>
        <moduleSet>
            <includes>
                <include>foobar:foo-bar</include>
            </includes>
            <binaries />
        </moduleSet>
    </moduleSets>
</assembly>

解决方法 dist.xml

这是我上面提到的解决方法。它产生的正是我想要的工件。

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0">
    <id>distribution</id>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}/../src</directory>
            <includes>
                <include>*.txt</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}/../foo-bar/target</directory>
            <includes>
                <include>*.war</include>
            </includes>
            <outputDirectory>.</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

【问题讨论】:

    标签: maven-2 maven-assembly-plugin


    【解决方案1】:

    你应该在你的程序集描述符中使用一个dependencySet

    <assembly>
      <id>dist</id>
      <formats>
          <format>zip</format>
      </formats>
      <includeBaseDirectory>true</includeBaseDirectory>
      <dependencySets>
          <dependencySet>
              <useTransitiveDependencies>false</useTransitiveDependencies>
              <useProjectArtifact>false</useProjectArtifact>
              <unpack>false</unpack>
              <scope>runtime</scope>
              <fileMode>0644</fileMode>
          </dependencySet>
      </dependencySets>
      <fileSets>
          <fileSet>
            with all supplemental files you need
          </fileSet>
      </fileSets>
    

    而不是通过文件系统(相对路径)引用文件。另一方面,程序集插件中确实已经存在源描述符(查看文档)。

    【讨论】:

    • 那行不通。除非我输入相对路径,否则它无法从父模块中获取文件。
    【解决方案2】:

    正在构建的模块的顺序是什么,我遇到了类似的问题,并且在子模块中声明父级有助于构建顺序。不确定使用是否对此有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 2011-10-14
      • 1970-01-01
      相关资源
      最近更新 更多