将依赖命令行参数与maven构建一起使用

时间:2014-08-21 15:24:41

标签: maven findbugs

我在maven生命周期的验证阶段使用findbugs-maven-plugin。即它在mvn clean install上运行。这是我父亲pom.xml中的代码(在多模块项目中)。

 <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>findbugs-maven-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
           <id>findbugs</id>
           <phase>verify</phase>
           <goals>
               <goal>check</goal>
           </goals>
        </execution>
    </executions>
    <configuration>
        <findbugsXmlOutputDirectory>target/findbugs</findbugsXmlOutputDirectory>
        <failOnError>false</failOnError>
    </configuration>
 </plugin>

 <plugin>
    <groupId>org.codehaus.mojo</groupId>
     <artifactId>xml-maven-plugin</artifactId>
     <version>1.0</version>
     <executions>
         <execution>
             <phase>verify</phase>
             <goals>
                <goal>transform</goal>
             </goals>
         </execution>
     </executions>
     <configuration>
        <transformationSets>
           <transformationSet>
              <dir>target/findbugs</dir>
              <outputDir>target/findbugs</outputDir>
              <stylesheet>plain.xsl</stylesheet>
              <fileMappers>
                 <fileMapper implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper">
                 <targetExtension>.html</targetExtension>
                 </fileMapper>
              </fileMappers>
           </transformationSet>
       </transformationSets>
     </configuration>
     <dependencies>
         <dependency>
             <groupId>com.google.code.findbugs</groupId>
             <artifactId>findbugs</artifactId>
             <version>2.0.0</version>
         </dependency>
     </dependencies>
</plugin>

这很好用,每个模块目标都会生成html文件。但是,我想通过在maven构建期间使用findbugs允许的参数(例如onlyAnalyze)来更进一步。我不想在pom.xml中添加配置。

我希望构建过程保持不变,除非我通过某些命令指定我只想分析一个类,例如通过运行:

mvn clean install -Dfindbugs:onlyAnalyze=MyClass

你知道我能做到这一点吗?

1 个答案:

答案 0 :(得分:4)

这是您可以调用独立目标的方法: plugin-prefix:goalgroupId:artifactId:version:goal以确保版本正确。 在您的情况下:findbugs:findbugs

使用-Dkey=value,您可以设置插件参数,如果它们被曝光。 http://mojo.codehaus.org/findbugs-maven-plugin/findbugs-mojo.html没有显示该选项。只是为了比较:http://mojo.codehaus.org/findbugs-maven-plugin/help-mojo.html确实有这样的选择。在这里,它仍被称为Expression ${key},现在它仅User property生成为key

如果您想从命令行设置onlyAnalyze,请要求mojo-team解决此问题,或执行以下操作:

<project>
  <properties>
    <findbugs.onlyAnalyze>false</findbugs.onlyAnalyze> <!-- default value -->
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>findbugs-maven-plugin</artifactId>
        <version>3.0.0</version>
      </plugin>
      <configuration>
        <onlyAnalyze>${findbugs.onlyAnalyze}</onlyAnalyze>
      </configuration>
    </plugins>
  </build>
</project>

现在您可以致电mvn findbugs:findbugs -Dfindbugs.onlyAnalyze=true

相关问题