在大会中包括一个解压缩的战争

Dav*_* W. 6 unpack maven maven-assembly-plugin

我有一个建立战争的项目(没问题).并且,该战争需要与一些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>
Run Code Online (Sandbox Code Playgroud)

这是我第一次尝试解压缩:

<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>
Run Code Online (Sandbox Code Playgroud)

这是我最后一次尝试获得解压缩的战争:

<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>
Run Code Online (Sandbox Code Playgroud)

在最后两次尝试的每次尝试中,我只是打包脚本而且战争没有结束.

我知道我可以使用${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>
Run Code Online (Sandbox Code Playgroud)

当我跑这个时,我得到了:

[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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

在拉链里面是战争中的所有罐子都很好并且没有包装,但不是我打开包装的战争.

Dav*_* W. 3

我知道我应该能够将未打包的战争添加到我的程序集中。我看到了这个unpack选项,并且我知道它适用于其他依赖项。但是,看起来我只能通过 a<dependencySet>或 a访问它<moduleSet>。我应该能够将我的项目指定为其自己的模块。我一定做错了什么。


脾泄

这是我讨厌 Maven 的一大原因:Maven 很好地向你隐藏了一些东西,这很好,因为它可以阻止你做你不应该做的事情。我讨厌开发人员构建 Antbuild.xml文件,因为大多数开发人员不了解如何进行构建,并且文件build.xml变得一团乱麻。对于 Maven,这不是问题。只需配置您的项目,Maven 就会为您处理好这一切。

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

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

我想做的事情应该很容易。我只想创建一个程序集,而不是使用打包的 war 文件,我希望将其解压。

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


我最终做了什么

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

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

使用该插件后,我所要做的就是在程序集插件中指定解压 war 的目录,并将其包含在我的 zip 中。

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

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

实际答案

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

该链接的答案和我的尝试之间只有两个区别:

  • 他们包括<useProjectArtifact>true</useProjectArtifact>而我没有。但是,这默认为true. 删除它仍然可以让它工作。
  • 他们/<outputDirectory>. 删除这个没有什么区别。

这意味着它现在与我之前尝试过的相匹配。那么,为什么这次有效呢?

事实证明,我只是通过运行来测试程序集的更改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.
Run Code Online (Sandbox Code Playgroud)

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

然后我花了一些时间尝试只获取我的战争,而不是所有相关的运行时内容。我曾经<includes/>这样做过,但因为我有一场战争而不是一个罐子,所以我必须在我的<include>.

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

<dependencySets>
    <dependencySet>
        <outputDirectory>${project.artifactId}</outputDirectory>
        <unpack>true</unpack>
        <scope>runtime</scope>
        <includes>
            <include>${project.groupId}:${project.artifactId}:*:${project.version}</include>
        </includes>
    </dependencySet>
</dependencySets>
Run Code Online (Sandbox Code Playgroud)

只要我跑mvn package,它就会起作用。

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