5

我需要设置 Maven 插件。我已经下载了 JAR。谁能告诉我下一步我该怎么做才能将插件与 Maven 集成或设置?我应该将 JAR 复制到父目录中还是需要编辑任何文件?

插件是:

  • Java2HTML
  • 依赖
  • 格纹风格
  • 三叶草
  • 科贝图拉
  • 艾玛
  • 查找错误
  • JavaNCSS
  • PMD
  • 质量保证实验室
  • 雷达
  • 声纳
4

3 回答 3

13

如果 Maven 可以访问中央存储库,它将下载大多数插件(有些插件未托管在中央存储库上,要访问那些您需要在 pom 或设置中定义额外存储库的插件)。如果在您的 POM 中配置了依赖项,Maven 将在您运行相关目标时自动尝试下载它们。对于您列出的依赖项,这是mvn site

您列出的大多数 jar 都是报告,因此应在 POM 的报告部分中声明,例如(我还将声明版本以确保您获得预期的插件):

<reporting>
  <plugins>
    <plugin>
      <artifactId>maven-checkstyle-plugin</artifactId>
    </plugin>
    <plugin>
      <artifactId>maven-pmd-plugin</artifactId>
      <configuration>
        <linkXref>true</linkXref>
      </configuration>
    </plugin>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>cobertura-maven-plugin</artifactId>
      <version>2.1</version>
      <configuration>
        <formats>
          <format>html</format>
          <format>xml</format>
        </formats>
        <outputDirectory>target/site/cobertura</outputDirectory>
      </configuration>
    </plugin>
    <plugin>
      <artifactId>maven-surefire-report-plugin</artifactId>
      <configuration>
        <outputDirectory>${basedir}/target/surefire-reports</outputDirectory>
      </configuration>
    </plugin>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>jdepend-maven-plugin</artifactId>
    </plugin>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>findbugs-maven-plugin</artifactId>
      <configuration>
        <xmlOutput>true</xmlOutput>
        <effort>Max</effort>
      </configuration>
    </plugin>
  </plugins>
</reporting>

Maven 插件执行模型的一些背景知识: 当你运行mvn site时,这是“从最新版本的站点插件运行站点目标”的简写,即相当于mvn site:site的简写mvn org.apache.maven.plugins:maven-site-plugin:LATEST:site

Maven 将尝试联系中央存储库,从 maven-metadata.xml 确定最新版本,然后在执行之前下载它(以及它的任何也缺少的依赖项)。

如果您使用代理,您可能会在构建日志中看到如下错误消息:

[INFO] The plugin 'org.apache.maven.plugins:maven-site-plugin' does not exist or no valid version could be found

为了解决这个问题,您可以在 Maven settings.xml(在 [MVN_HOME]/conf/settings.xml)中声明代理设置。它们被默认注释掉,但看起来像这样:

<proxy>
  <id>optional</id>
  <active>true</active>
  <protocol>http</protocol>
  <username>proxyuser</username>
  <password>proxypass</password>
  <host>proxy.host.net</host>
  <port>80</port>
  <nonProxyHosts>local.net,some.host.com</nonProxyHosts>
</proxy>

将用户名、密码、主机和端口值替换为与您的环境相关的值,Maven 将能够下载所需的依赖项。

有关使用 Maven 的更多详细信息,请查看 Sonatype 的Maven:权威指南,它是在线免费的。

于 2009-07-20T08:52:33.073 回答
1

西拉科夫是对的;Maven 会在使用时自动下载并安装插件。

您可以直接运行它们(对于一次性作业),也可以在 pom.xml 中配置它们 - 这也允许您进行配置,并将其设置为自动运行,例如,生成源代码或报告测试覆盖率. 这样做的一个主要优点是您可以在共享的父 pom 中定义一组插件配置,并在所有项目中重用相同的配置,同时仍然能够在必要时覆盖每个子项目中继承的配置 - 这是在大型项目中使用 Maven 的最大优势之一。

每个插件都有自己的配置参数,标准配置参数记录在http://maven.apache.org/plugins/。另一个很好的资源是 O'Reilly Maven 书,位于http://www.sonatype.com/books/maven-book/reference/

cobertura 的示例配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <outputDirectory>${project.build.directory}/pmd</outputDirectory>
        <targetDirectory>${project.build.directory}</targetDirectory>
        <aggregate>true</aggregate>
        <!--  CPD minimum tokens to report on (5 to 10 duplicate lines)    --> 
        <minimumTokens>100</minimumTokens> 
        <minimumPriority>3</minimumPriority>
        <!--  Exclude mock classes     --> 
        <excludes>
        <exclude>**/Mock.*</exclude> 
        <exclude>**/Dummy.*</exclude> 
        <exclude>**/*Mock.java</exclude> 
        <exclude>**/*Dummy.java</exclude> 
        </excludes>
        <includeTests>true</includeTests> 
        <targetJdk>1.5</targetJdk>
        <rulesets>
            <ruleset>pmd_ruleset.xml</ruleset>  
        </rulesets>
    </configuration>
</plugin>
于 2009-07-20T08:34:45.263 回答
0

您无需手动下载插件。我不是 100% 确定,但是如果你想使用例如 checkstyle 插件,你需要使用 checkstyle 参数表单命令行启动 maven

就像是:

mvn checkstyle:checkstyle

或者

mvn checkstyle:check

edit1:但您也可以将 jar 放入具有特定文件夹结构的本地 m2 存储库中以访问它们。

edit2:您可以将所有插件放入您自己的存储库中,然后您需要告诉 maven(使用 pom)您要使用哪些存储库。每个插件都必须在 pom.xml 中描述。

于 2009-07-20T08:12:40.707 回答