Maven:如何指定运行哪个程序集插件

时间:2011-08-16 23:00:05

标签: maven maven-3 maven-assembly-plugin

我有一个有多个程序集执行的pom。当我跑步时,例如mvn package,它运行所有执行。我怎么能告诉它只运行foo执行?

<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>

在我看来,我的上述内容类似于以下Makefile

all: foo bar

foo:
    ... build foo ...

bar:
    ... build bar ...

我可以运行make all或只是make来构建所有内容,或者我可以运行make foomake bar来构建单个目标。我怎样才能用Maven实现这个目标?

3 个答案:

答案 0 :(得分:29)

您需要使用profiles,这是一个pom.xml示例:

<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>

你会像这样调用maven:

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

答案 1 :(得分:8)

My Maven有点生疏,但我认为你可以通过以下两种方式做到这一点:

1)使用配置文件。使用“maven -PprofileName”在命令行中指定配置文件。

2)将你的执行放在不同的阶段/目标中,只运行你想要的那些。

答案 2 :(得分:2)

如果您不想运行“bar”,则不要将其绑定到生命周期阶段。插件执行仅在绑定到阶段时运行,并且该阶段作为构建的一部分执行。正如TheCoolah所建议的那样,配置文件是管理执行何时受生命周期阶段约束的一种方式。