【问题标题】:Maven execute a plugin twice, once in an "always active" profile, and again in a conditionally run profile?Maven 两次执行插件,一次在“始终处于活动状态”的配置文件中,再次在有条件运行的配置文件中?
【发布时间】:2012-09-03 19:06:24
【问题描述】:

我试图在“始终处于活动状态”的 Maven 配置文件中运行一次插件,然后在有条件执行的配置文件中再次运行。当条件配置文件运行时,“永远在线”配置文件中的插件不会执行。但是,当仅使用“始终活动”配置文件执行 maven 时,插件运行得很好。

这是我的 pom.xml 的示例

<profile>
        <id>default</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <property>
                <name>!doNoEverSetThisPropertyThisProfileShouldAlwaysBeActive</name>
            </property>
        </activation>
        <build>
            <plugins>                    
                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>antCopyResources</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                    ...
                </plugin>
            </plugins>
         </build>
</profile>

<profile>
        <id>prod</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>prodTokenReplace</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                    ...
                </plugin>
            </plugins>
        </build>
</profile>

例如,如果我像这样调用 maven:

mvn clean compile

默认配置文件中的 antrun 插件运行良好。

但是,如果我像这样调用 maven:

mvn -P prod clean compile

只有 prod 中的 antrun 插件运行。

mvn -P prod help:active-profiles

Active Profiles for Project 'projectname':

The following profiles are active:

 - default (source: pom)
 - prod (source: pom)

【问题讨论】:

  • 你能负担得起在不同阶段运行它吗?

标签: maven maven-3


【解决方案1】:

我知道这是一个迟到的答案,但它可能对其他人有所帮助。

我有一个类似的情况,我通过将执行的通用(activeByDefault)部分放在 profiles 部分之外和主要的 build 部分中解决了。 p>

这样,构建将始终运行主构建的 antrun,并根据条件运行相关配置文件中的 antrun。

根据您最初的示例:

<build>
      <plugins>                    
           <plugin>
               <artifactId>maven-antrun-plugin</artifactId>
               <executions>
                   <execution>
                       <id>antCopyResources</id>
                       <phase>process-resources</phase>
                       <goals>
                           <goal>run</goal>
                       </goals>
                  </execution>
              </executions>
              ...
         </plugin>
     </plugins>
</build>

<profiles>
   <profile>
        <id>prod</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>prodTokenReplace</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                    ...
                </plugin>
            </plugins>
        </build>
   </profile>
<profiles>

我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    您可以保持默认状态,并将 ant-run 插件配置从默认值复制到 prod 中,这样您最终会在 prod 中得到两个插件配置,例如:

    <profile>
        <id>prod</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>antCopyResources</id>
                            <phase>process-resources</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                    ...
                </plugin>
                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>prodTokenReplace</id>
                            <phase>compile</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                    ...
                </plugin>
            </plugins>
        </build>
    </profile>
    

    【讨论】:

    • 我可能不得不这样做,但这似乎完全没有必要。我将我的 pom 精简了很多,基本上每个配置文件都是特定环境(开发、质量保证、产品等)的构建配置文件。除了选择的构建配置文件之外,我默认拥有的所有插件都应该运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 2013-01-21
    • 2019-08-03
    相关资源
    最近更新 更多