【问题标题】:maven pom - define a profile to customize plugin configurationmaven pom - 定义一个配置文件来自定义插件配置
【发布时间】:2016-11-21 14:16:07
【问题描述】:

我在 pom 中有以下插件:

  <plugin>
    <groupId>com.github.klieber</groupId>
    <artifactId>phantomjs-maven-plugin</artifactId>
    <configuration>
      <version>${phantomjs.version}</version>
      <checkSystemPath>false</checkSystemPath>          
      <skip>${skipTests}</skip>
    </configuration> 
    <executions>
      <execution>
        <goals>
          <goal>install</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

我想定义一个新的配置文件来自定义插件配置:

<profile>
  <id>enduserTest</id>

  <properties>
    <tomcat.version>8.0.39</tomcat.version>              
    <skipTests>true</skipTests>
  </properties>      

  <build>
    <defaultGoal>clean verify cargo:run</defaultGoal>
    <plugins>          

  <plugin>
    <groupId>com.github.klieber</groupId>
    <artifactId>phantomjs-maven-plugin</artifactId>
    <configuration>
      <version>${phantomjs.version}</version>
      <checkSystemPath>false</checkSystemPath>          
    </configuration> 
    <executions>
      <execution>
        <goals>
          <goal>install</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

唯一的区别是&lt;skip&gt;${skipTests}&lt;/skip&gt; 线。 现在我想运行mvn -PenduserTest,但配置不会被覆盖。 有什么建议吗?有更好的解决方案吗?这是正确的策略吗?

【问题讨论】:

  • 如果我在配置文件内部和外部定义这些插件,它就不起作用。但如果我只在配置文件中定义这些插件,它就可以正常工作。

标签: maven plugins profile


【解决方案1】:

如果期望的行为是在运行配置文件时跳过测试,那么您的逻辑没有错。验证我测试了这段代码,这是按预期工作的(它使用 -PenduserTest 跳过测试):

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.2</version>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
             <plugin>
            <groupId>com.github.klieber</groupId>
            <artifactId>phantomjs-maven-plugin</artifactId>
            <version>0.7</version>
            <configuration>
              <version>2.1.1</version>
              <checkSystemPath>false</checkSystemPath>          
              <skip>${skipTests}</skip>
            </configuration> 
            <executions>
              <execution>
                <goals>
                  <goal>install</goal>
                </goals>
              </execution>
            </executions>
          </plugin>


            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source />
                    <target />
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
      <id>enduserTest</id>

      <properties>
        <tomcat.version>8.0.39</tomcat.version>              
        <skipTests>true</skipTests>
      </properties>      

      <build>
        <defaultGoal>clean verify cargo:run</defaultGoal>
        <plugins>          

      <plugin>
        <groupId>com.github.klieber</groupId>
        <artifactId>phantomjs-maven-plugin</artifactId>
        <version>0.7</version>
        <configuration>
          <version>0.7</version>
          <checkSystemPath>false</checkSystemPath>          
        </configuration> 
        <executions>
          <execution>
            <goals>
              <goal>install</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      </plugins>
      </build>
      </profile>
    </profiles>

</project>

【讨论】:

  • surefire 插件对我来说非常相似,但我不必在新配置文件中重新定义整个插件。我刚刚从 POM 属性中获取了原始插件定义的配置值,并在配置文件中添加了覆盖,就像你在这里一样。
猜你喜欢
  • 2016-05-07
  • 2018-10-08
  • 2014-07-22
  • 1970-01-01
  • 2012-01-11
  • 2011-06-07
  • 1970-01-01
  • 2016-03-27
  • 1970-01-01
相关资源
最近更新 更多