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

发布于 2024-11-29 16:19:06 字数 1204 浏览 2 评论 0原文

我有一个具有多个程序集执行的 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 实现这个目标?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟火散人牵绊 2024-12-06 16:19:06

您需要使用 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)

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)
ぃ弥猫深巷。 2024-12-06 16:19:06

我的 Maven 有点生疏,但我认为你可以通过几种方法来做到这一点:

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

2)将您的执行分为不同的阶段/目标,并仅运行您想要的阶段/目标。

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.

墨小沫ゞ 2024-12-06 16:19:06

如果您不希望“bar”运行,则不要将其绑定到生命周期阶段。插件执行仅在绑定到某个阶段并且该阶段作为构建的一部分执行时运行。正如 TheCoolah 所建议的,配置文件是管理何时将执行绑定到生命周期阶段以及何时不绑定的一种方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文