简体   繁体   English

使用相同的描述符Maven程序集插件针对不同格式自定义文件夹名称

[英]Customize folder name using same descriptor maven assembly plugin for different format

I am new to maven world and I am having some problems with maven assembly plugin. 我是Maven世界的新手,我在使用Maven程序集插件时遇到了一些问题。 I want to use the same descriptor file to generate different format with custom name for "dir" format. 我想使用相同的描述符文件来生成具有“ dir”格式自定义名称的不同格式。

Following is my maven assembly plugin configuration :- 以下是我的Maven程序集插件配置:

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <id>assembly</id>
                <goals>
                    <goal>single</goal>
                </goals>
                <phase>install</phase>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/descriptor.xml</descriptor>
                    </descriptors>
                    <finalName>service</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                    <outputDirectory>target</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>

This is the descriptor file :- 这是描述符文件:

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>assembly</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <baseDirectory>.</baseDirectory>
    <files>
        <file>
            <source>target/service.jar</source>
            <outputDirectory>.</outputDirectory>
            <filtered>false</filtered>
        </file>
        <file>
            <source>src/main/deploy/version.txt</source>
            <outputDirectory>.</outputDirectory>
            <filtered>true</filtered>
        </file>
        <file>
            <source>src/main/deploy/start.sh</source>
            <outputDirectory>.</outputDirectory>
            <filtered>true</filtered>
        </file>
        <file>
            <source>src/main/deploy/stop.sh</source>
            <outputDirectory>.</outputDirectory>
            <filtered>true</filtered>
        </file>
    </files>
</assembly>

I want to use the same descriptor file(basically same files to combine) into a service.zip and also create another folder in target named deploy if I add 我想将相同的描述符文件(基本上是要合并的相同文件)合并到service.zip中,并且还要在目标中创建另一个名为deploy的文件夹

<format>dir</format>

then it will create a folder named service under target and will have all the listed files. 然后它将在target下创建一个名为service的文件夹,并将包含所有列出的文件。 I want a way to customize the name of directory to "deploy". 我想要一种将目录名称自定义为“部署”的方法。 I tried checking with the documentation but could not find anything helpful unless I create another descriptor which is not the right way since my new descriptor will have same configuration as the previous one with just format as "dir". 我尝试检查文档,但找不到任何有用的方法,除非创建另一个描述符,否则这是不正确的方法,因为我的新描述符将具有与上一个相同的配置,只是格式为“ dir”。

Any help will be appreciated thanks in advance. 任何帮助将不胜感激,谢谢。

You can achieve your requirement by using different maven profiles . 您可以使用不同的Maven配置文件来满足您的要求。 In

In your assembly descriptor, you use a property instead of a fixed string: 在程序集描述符中,使用属性而不是固定字符串:

<formats>
    <format>${distributionFormat}</format>
</formats>

distributionFormat is defined in pom.xml . distributionFormatpom.xml定义。 The name of the property is your choice. 属性的名称是您的选择。

In your pom.xml , you define distributionFormat in two different profiles. pom.xml ,您可以在两个不同的配置文件中定义distributionFormat There you also define a name for the assembly ( assemblyName in my example). 在那里,你也为组装定义一个名字( assemblyName在我的例子)。

<project>
    ...
    ...
    <profiles>
         <profile>
              <id>distAsZip</id>
              <activeByDefault>true</activeByDefault>
              <assemblyName>service</assemblyName>
              <distributionFormat>zip</distributionFormat>
         </profile>
         <profile>
              <id>distAsDir</id>
              <activeByDefault>false</activeByDefault>
              <assemblyName>deploy</assemblyName>
              <distributionFormat>dir</distributionFormat>
         </profile>
    </profiles>
    ...
    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
            <execution>
                <id>assembly</id>
                <goals>
                    <goal>single</goal>
                </goals>
                <phase>install</phase>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/descriptor.xml</descriptor>
                    </descriptors>
                    <finalName>${assemblyName}</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                    <outputDirectory>target</outputDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
 ...
</project>

Now you can execute maven and define the profile that you want to use: 现在,您可以执行maven并定义要使用的配置文件:
mvn clean package -PdistAsDir or mvn clean package -PdistAsZip mvn clean package -PdistAsDirmvn clean package -PdistAsZip

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM