14

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?

David Phillips
  • 10,723
  • 6
  • 41
  • 54

3 Answers3

30

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)
mbbce
  • 2,245
  • 1
  • 19
  • 31
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
8

My Maven is a bit rusty but I think you can do this a couple of ways:

1) Use profiles. Specify a profile on the command line with "maven -PprofileName".

2) Put your executions in separate phases/goals and run only the ones you want.

TheCoolah
  • 519
  • 1
  • 4
  • 16
2

If you don't want "bar" to run, then don't bind it to a lifecycle phase. Plugin executions only run when they are bound to a phase and that phase executes as part of a build. As TheCoolah suggested, profiles are one way of managing when executions are bound to lifecycle phases and when not.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199