Maven执行一次插件两次,一次处于“始终处于活动状态”的配置文件,再次处于有条件运行的配置文件中?

时间:2012-09-10 21:29:49

标签: maven maven-3

我正在尝试在“始终处于活动状态”的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)

2 个答案:

答案 0 :(得分:1)

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

我有一个类似的案例,我通过将执行的common(activeByDefault)部分放在配置文件部分以及主构建部分中来解决。

这样,构建将始终运行主构建的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>

我希望这会有所帮助。

答案 1 :(得分:0)

您可以按原样保留默认值,并将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>
相关问题