【问题标题】:Including an unpacked War in the Assembly在大会中包括一场解开的战争
【发布时间】:2015-02-16 05:05:19
【问题描述】:

我有一个构建战争的项目(没问题)。而且,这场战争需要用一些 shell 脚本打包。因为该战争包含因站点而异的属性文件,所以我们不能简单地按原样安装战争,而是将属性文件放入其中。这就是 shell 脚本的作用。

我想在我的程序集中将我的战争打包为未打包的战争。我在程序集描述符中看到了<unpacked>,但我无法让它工作。

这是我的第一个bin.xml,我只是按原样打包了战争。这工作正常:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}/src/assembly/scripts</directory>
            <includes>
                <include>deploy.sh</include>
                <include>lock_build.sh</include>
                <include>description.sh</include>
                <include>url-encode.pl</include>
            </includes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/${project.artifactId}-${project.version}</directory>
            <outputDirectory>${project.artifactId}</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

这是我第一次尝试解压:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}/src/assembly/scripts</directory>
            <includes>
                <include>deploy.sh</include>
                <include>lock_build.sh</include>
                <include>description.sh</include>
                <include>url-encode.pl</include>
            </includes>
            <outputDirectory>/</outputDirectory>
       </fileSet>
    </fileSets>
    <moduleSets>
        <moduleSet>
            <includes>
                <include>{$project.groupId}:${project.artifactId}:war</include>
            </includes>
            <binaries>
                <includes>
                    <include>${project.build.directory}-${project.artifactId}-${project.version}.${project.packaging}</include>
                </includes>
                <outputDirectory>${project.artifactId}</outputDirectory>
                <unpack>true</unpack>
            </binaries>
        </moduleSet>
    </moduleSets>
</assembly>

这是我最后一次尝试解开的战争:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}/src/assembly/scripts</directory>
            <includes>
                <include>deploy.sh</include>
                <include>lock_build.sh</include>
                <include>description.sh</include>
                <include>url-encode.pl</include>
            </includes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
    <moduleSets>
        <moduleSet>
            <binaries>
                <attachmentClassifier>war</attachmentClassifier>
                <outputDirectory>${project.artifactId}</outputDirectory>
                <unpack>true</unpack>
                <includeDependencies>true</includeDependencies>
                <dependencySets>
                    <dependencySet/>
                </dependencySets>
            </binaries>
        </moduleSet>
    </moduleSets>
</assembly>

在最后两次尝试中,我只是打包脚本,战争还没有结束。

我知道我可以使用 ${project.build.directory}/${project.artifactId}-${project.version} 目录,该目录包含战争中几乎所有的文件,但它不包含我的 MANIFEST.MF 条目,其中包含将战争链接回特定 Jenkins 构建和 Subversion 修订的信息.

我需要做什么才能在我的程序集中包含一个未打包的战争?


又一次尝试

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}/src/assembly/scripts</directory>
            <includes>
                <include>deploy.sh</include>
                <include>lock_build.sh</include>
                <include>description.sh</include>
                <include>url-encode.pl</include>
            </includes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>${project.artifactId}</outputDirectory>
            <unpack>true</unpack>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

当我运行这个时,我得到:

[INFO] ------------------------------------------------------------------------
[INFO] Building My Project 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-assembly-plugin:2.5.2:single (default-cli) @ myproj ---
[INFO] Reading assembly descriptor: src/assembly/bin.xml
[WARNING] Cannot include project artifact: \
          com.vegicorp:myproj:war:1.0.0; \
          it doesn't have an associated file or directory.
[INFO] Building zip: ~/workdir/trunk/myproj/target/archive/myproj.zip
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

拉链里面是战争中的所有罐子都很好,没有包装,但不是我的没有包装的战争。

【问题讨论】:

  • 您可以从 ..\target\maven-archiver 文件夹中获取 pom.properties 文件...可以解决 manifest.mf 条目的需要
  • 不幸的是,我需要 MANIFEST.MF 条目。我在 MANIFEST.MF 中嵌入了 Jenkins 构建和 Subversion 信息。这使我们能够准确地查看特定机器上安装的内容。我们遇到的许多开发问题都证明是安装了错误版本的war 或jar。 “嘿,这是 Build #103。那个版本没有被批准安装。你应该安装 Build #105。”
  • 使用 -X 选项运行 mvn 以获得更多输出可能很有用
  • -X 选项不是很有帮助。它只是列出了程序集插件运行的事实,并没有提供更多细节。我发现 -X-e 选项在 Maven 中不是很有用。例如,当我的程序集出现错误时,他们都无法向我提供失败的行或信息。

标签: maven maven-assembly-plugin unpack


【解决方案1】:

我知道我应该能够将解压后的战争添加到我的程序集中。我看到 unpack 选项,我知道它适用于其他依赖项。但是,看起来我只能通过&lt;dependencySet&gt;&lt;moduleSet&gt; 访问它。我应该能够将我的项目指定为它自己的模块。一定是我做错了什么。


脾虚

这是我讨厌 Maven 的一大原因:Maven 很好地隐藏了你的东西,这很好,因为它可以防止你做你不应该做的事情。我讨厌开发人员构建 Ant build.xml 文件,因为大多数开发人员不了解如何进行构建,而 build.xml 变得难以阅读。使用 Maven,这不是问题。只需配置您的项目,Maven 会为您处理这些。

但有时 Maven 就像一个带有一堆控制杆和按钮的黑盒子。你坐在那里推拉拉杆和按钮,试图弄清楚如何让它做你想做的事情。我花了太多时间来帮助开发人员配置他们的 Maven 项目。他们想做一些不同的事情,比如使用休眠或从 WSDL 文件构建源代码,但不知道如何让 Maven 做他们想做的事情。

一位开发人员将其描述为无法完全到达您想去的地方的自动驾驶汽车。你甚至可以看到窗外的目的地,但你不知道如何操纵汽车的目的地来让你到达那里。

我想做的事情应该很简单。我只想创建一个程序集,而不是使用打包的战争文件,我希望它解包。

在 Ant 中,这可以在单个任务中完成。在 Maven 中,这是一个神秘的过程。我想我已经很接近了,我可能缺少一个可以让它全部工作的小配置参数,但我已经花了几个小时来解决这个问题。


我最终做了什么

我使用maven-dependency-plugin 解开我的战争。我不确定这是否可行,因为我不希望依赖插件下载战争,但似乎理解当我指定我的战争时,我正在谈论当前的构建,并且没有下载任何内容。 (我什至在我们的 Maven 存储库中都没有战争)。

我还必须将 Maven 从 2.x 升级到 3.x,因为我需要确保 maven-dependency-pluginmaven-assembly-plugin 之前运行。由于两者都在构建的打包阶段运行,Maven 2.x 无法保证插件的运行顺序。

一旦我使用了那个插件,我所要做的就是在我的程序集插件中指定我解压缩战争的目录,它包含在我的 zip 中。

我仍然想知道如何在程序集插件本身中使用&lt;unpack&gt; 实体,而不必使用另一个插件来解压我的战争。如果有人能告诉我如何在程序集文件本身中解包,我会将其标记为正确答案。

pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.9</version>
    <executions>
        <execution>
            <id>unpack</id>
            <phase>package</phase>
            <goals>
                <goal>unpack</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/unwar/${project.artifactId}</outputDirectory>
                <artifactItems>
                    <artifactItem>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>${project.artifactId}</artifactId>
                        <version>${project.version}</version>
                        <type>war</type>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.5.2</version>
    <configuration>
        <finalName>${project.artifactId}</finalName>
        <appendAssemblyId>false</appendAssemblyId>
        <outputDirectory>${project.build.directory}/archive</outputDirectory>
        <descriptors>
            <descriptor>src/assembly/bin.xml</descriptor>
        </descriptors>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

bin.xml(我的程序集)

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}/src/assembly/scripts</directory>
            <fileMode>0755</fileMode>
            <lineEnding>lf</lineEnding>
            <includes>
                <include>deploy.sh</include>
                <include>lock_build.sh</include>
                <include>description.sh</include>
                <include>url-encode.pl</include>
            </includes>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/unwar</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

实际答案

感谢 khmarbaise 的链接(请参阅下面的评论),我复制了该项目的程序集插件,它几乎工作了。就像我的尝试一样,它解压了所有运行时 jar,而不仅仅是我想要的。然而,它也解开了我的战争。

该链接的答案与我的尝试只有两个不同:

  • 他们包括&lt;useProjectArtifact&gt;true&lt;/useProjectArtifact&gt; 而我没有。但是,这默认为true。删除它仍然可以让它工作。
  • 他们在&lt;outputDirectory&gt; 中的目录名称前添加了/。删除它没有任何区别。

这意味着它现在与我之前尝试过的匹配。那么,为什么这次它起作用了。

原来,我只是通过运行mvn assembly:single 来测试程序集更改。毕竟,当我只是想让程序集工作时,为什么要进行整个构建和重新打包。当您只运行 mvn assembly:single 时——即使所有内容都已打包,您也会收到此错误:

[WARNING] Cannot include project artifact: \
      com.vegicorp:myproj:war:1.0.0; \
      it doesn't have an associated file or directory.

你的战争并没有解开。但是,如果您将程序集放入打包阶段,然后运行mvn package,一切都会很顺利。

然后我花了一些时间试图让我的战争而不是所有相关的运行时内容。我使用&lt;includes/&gt; 来执行此操作,但因为我有一个战争而不是一个罐子,我不得不在我的&lt;include&gt; 中包含一个分类器。

最后,我一切正常。我的程序集中有这个:

<dependencySets>
    <dependencySet>
        <outputDirectory>${project.artifactId}</outputDirectory>
        <unpack>true</unpack>
        <scope>runtime</scope>
        <includes>
            <include>${project.groupId}:${project.artifactId}:*:${project.version}</include>
        </includes>
    </dependencySet>
</dependencySets>

只要我运行mvn package,它就可以工作。

这比我使用maven-dependency-plugin 的要好得多。现在,与程序集有关的所有信息都在程序集 XML 文件中。

【讨论】:

  • 感谢您的链接。我尝试了该解决方案,并且所有依赖项都已解压缩,而不仅仅是项目。另外,它甚至没有放入我的MANIFEST.MF 文件中。
  • 实际上,我发现它确实放入了我的MANIFEST.MF,但覆盖了它。我必须做&lt;includes&gt; 过滤才能得到战争。我还发现我的程序集 XML 是正确的,但是我使用 mvn assembly:single 来测试我的程序集而不是 mvn package
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-06
  • 2015-07-31
  • 1970-01-01
  • 2018-04-27
  • 1970-01-01
  • 2017-01-19
相关资源
最近更新 更多