Search code examples
mavenmaven-3maven-assembly-plugin

Maven: how to specify which assembly plugin execution runs


I have a pom with multiple assembly executions. When I run, e.g. mvn package, it runs all the executions. How can I tell it to only run the foo execution?

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>foo/id>
                    <phase>package</phase>
                    <goals><goal>single</goal></goals>
                    <configuration>...</configuration>
                </execution>
                <execution>
                    <id>bar</id>
                    <phase>package</phase>
                    <goals><goal>single</goal></goals>
                    <configuration>...</configuration>
                </execution>

What I have above is, in my mind, similar to the following Makefile:

all: foo bar

foo:
    ... build foo ...

bar:
    ... build bar ...

I can run a make all or simply make to build everything, or I can run make foo or make bar to build individual targets. How can I achieve this with Maven?


Solution

  • You need to use profiles, here is a pom.xml example:

    <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>com.mycompany</groupId>
        <artifactId>FooBar</artifactId>
        <version>1.0</version>
        <packaging>jar</packaging>
    
        <profiles>
    
            <profile>
                <id>Foo</id>
                <build>
                    <plugins>
                        <plugin>
                            <artifactId>maven-assembly-plugin</artifactId>
                            <executions>
                            <execution>
                                <id>foo/id>
                                <phase>package</phase>
                                <goals><goal>single</goal></goals>
                                <!-- configuration>...</configuration -->
                            </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
    
            <profile>
                <id>Bar</id>
                <build>
                    <plugins>
                        <plugin>
                            <artifactId>maven-assembly-plugin</artifactId>
                            <executions>
                            <execution>
                                <id>Bar</id>
                                <phase>package</phase>
                                <goals><goal>single</goal></goals>
                                <!-- configuration>...</configuration -->
                            </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
    
        </profiles>
    
    </project>
    

    And you would invoke maven like this:

    mvn package -P Foo  // Only Foo
    mvn package -P Bar  // Only Bar
    mvn package -P Foo,Bar // All (Foo and Bar)