maven - 在大会中进行一场无休止的 war

标签 maven maven-assembly-plugin unpack

我有一个建立 war 的项目(没问题)。而且,那场 war 需要与一些shell脚本一起打包。由于该 war 包含的属性文件因站点而异,因此我们不能简单地按原样安装 war ,而是在其中存储属性文件。这就是shell脚本所做的。
我想将我的 war 打包为一场未包装的 war 。我在Assembly Descriptor中看到了<unpacked>,但我无法使其正常工作。
这是我的第一个bin.xml,我按原样打包了 war 。这工作正常:

<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>
在这最后的两次尝试中,我只打包了脚本, war 没有结束。
我知道我可以使用${project.build.directory}/${project.artifactId}-${project.version}目录,该目录包含 war 中的几乎所有文件,但是它不包含我的MANIFEST.MF条目,其中包括将 war 链接回特定Jenkins构建和Subversion修订版的信息。
我需要怎么做才能将拆开的 war 纳入我的装配中?

另一尝试
<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] ------------------------------------------------------------------------
zipper 里面是 war 中所有精美的 jar ,没有包装,但没有包装。

最佳答案

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

脾排气

这是我讨厌Maven的一件大事:Maven很好地向您隐藏了东西,这很好,因为它可以阻止您执行不应做的事情。当开发人员构建Ant build.xml文件时,我讨厌它,因为大多数开发人员不了解如何进行构建,并且build.xml变得不可读。使用Maven,这不是问题。只需配置您的项目,Maven就会为您解决这个问题。

但是有时Maven就像一个黑盒子,上面有一堆操纵杆和按钮。您坐在那里插入和拉动操纵杆和按钮,试图弄清楚如何使它完成您想做的事情。我花了太多时间来帮助开发人员配置他们的Maven项目。他们想要做一些与众不同的事情,例如使用休眠或从WSDL文件构建源代码,并且不知道如何让Maven来完成他们想要的事情。

一位开发人员将其描述为自动驾驶汽车,无法完全驶向您想要的地方。您甚至可能在窗口外看到目的地,但是您无法弄清楚如何操纵汽车的目的地来到达目的地。

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

在Ant中,这可以在单个任务中完成。在Maven中,这是一个神秘的过程。我想我快要走了,我可能会缺少一个小的配置参数,它将使所有工作正常进行,但是我已经花了数小时来进行此工作。

我最终做了什么

我用maven-dependency-plugin解开了 war 。我不确定这是否行得通,因为我不希望依赖插件下载war,但是似乎可以理解,当我指定war时,我正在谈论当前版本,而没有下载任何内容。 (我的Maven repo 中甚至没有 war )。

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

一旦使用了该插件,我所要做的就是在我的程序集插件中指定解压缩 war 文件的目录,该目录包含在我的zip文件中。

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

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,而不仅仅是我想要的一个。但是,它也使我的 war 更加艰难。

该链接的答案和我的尝试之间只有两个区别:
  • 他们包括<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.
    

    而且您的 war 还没有结束。但是,如果将程序集置于打包阶段,然后运行mvn package,那么一切都将变得很不错。

    然后,我花了一些时间来尝试发动 war ,而不是将所有与之相关的运行时东西都带走。我使用<includes/>来做到这一点,但是因为我有 war 而不是 jar ,所以我不得不在<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>
    

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

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

    关于maven - 在大会中进行一场无休止的 war ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27529159/

    相关文章:

    java - 为什么 "mvn assembly:single"创建一个只有程序集而不是我的代码的 fat jar 子?

    PHP - 将小端十六进制转换为大端十六进制

    utf-8 - 如何在lua中将UTF8字节数组转换为字符串

    java - 如何从 Maven 存储库中的 jar 中删除版本信息

    maven-2 - 生成具有不同/过滤类的多个 jar 的 Maven 最佳实践?

    java - android maven插件: Plugin execution not covered by lifecycle configuration

    java - 如何让 Maven 多模块程序集将 war 文件与 jar 文件放在单独的子目录中?

    python - 使用 struct.pack 和 struct.unpack 的问题

    java - 无法执行目标 org.codehaus.mojo :exec-maven-plugin:1. 6.0:java

    maven - Mule 中的部署后集成测试