Younes
Younes

Reputation: 1671

Maven: how to use POM elements from within assembly descriptor

I need to assemble several sets of resources. These sets of resources are related to each other. So, I decided to put them all under the same project and use the assembly plugin to achieve my goal.

I have ended with a POM and a descriptor file for each set of resources.

Let's assume that my project is as follows:

The content of descriptor1.xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<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>set1</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <fileSets>
        <fileSet>
            <outputDirectory>/</outputDirectory>
            <directory>${project.basedir}/src/main/resources/set1</directory>
        </fileSet>
    </fileSets>
</assembly>

The content of descriptor2.xml and descriptor3.xml is similar to the content of descriptor1.xml except that set1 (in "/assembly/id" and "/assembly/fieldSets/fieldSet/directory") is replaced by set2 and set3 respectively.

The content of pom.xml is as follows:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sample</groupId>
    <artifactId>sample.assembler</artifactId>
    <version>0.0.1</version>
    <packaging>pom</packaging>

    <properties>
        <maven-assembly-plugin.version>2.4</maven-assembly-plugin.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>${maven-assembly-plugin.version}</version>
                <executions>
                    <execution>
                        <id>set1</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>descriptor1.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                    <execution>
                        <id>set2</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>descriptor2.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                    <execution>
                        <id>set3</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>descriptor3.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

The above config gives the expected result. However, there are a lot of descriptor files to maintain.

I have read in the documentation that descriptor files are interpolated using project properties, POM element values, user properties, ... before being used.

My question is : is there a way to reference the id of the current execution (some thing like project.build.execution.id)? In such a case, all my three descriptors with be replaced by only one file.

Thank you in advance.

Upvotes: 2

Views: 4810

Answers (1)

Jan Zyka
Jan Zyka

Reputation: 17898

I don't think you can achieve what you want within single run.

But you can create a profiles where earch of the profile will define different properties and you can run your build three times with different profiles to get 3 different file sets.

I do use such approach for generating configuration for different environments. I activate the environment profile and the output is the environment configuration. The configuration is generated by single descriptor which is driven by the profile properties.

EDIT:

Here is a workaround using per-execution propeties which may solve your problem. In the moment I'm not entirely sure if these filters will be used for filtering resources only or you can reference them in the assembly itself for defining lets say <finalName> ...

See: http://olafsblog.sysbsb.de/per-assembly-filtering-with-the-maven-assembly-plugin/

Upvotes: 1

Related Questions