java 集成 Maven 报告插件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1152228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Integrating Maven reporting plugins
提问by Rich Seller
I need to setup Maven plugins. I have downloaded the JARs. Can anyone please tell me what do I do next in order to integrate or setup the plugins with Maven? Should I copy the JARs into the parent directory or do I need to edit any file?
我需要设置 Maven 插件。我已经下载了 JAR。任何人都可以告诉我下一步该怎么做才能将插件与 Maven 集成或设置?我应该将 JAR 复制到父目录中还是需要编辑任何文件?
The plugins are:
插件是:
- Java2HTML
- JDepend
- Checkstyle
- Clover
- Cobertura
- EMMA
- Findbugs
- JavaNCSS
- PMD
- QALab
- Xradar
- Sonar
- Java2HTML
- J依赖
- 格纹
- 三叶草
- 科贝图拉
- 艾玛
- 发现错误
- JavaNCSS
- PMD
- 质量保证实验室
- 雷达
- 声纳
回答by Rich Seller
If Maven has access to the central repository it will download most plugins (some are not hosted on central, to access those you need to define an additional repository in your pom or settings). If the dependencies are configured in your POM, Maven will automatically attempt to download them when you run a relevant goal. For the dependencies you listed this is mvn site.
如果 Maven 可以访问中央存储库,它将下载大多数插件(有些插件没有托管在中央存储库,要访问那些需要在 pom 或设置中定义附加存储库的插件)。如果您的 POM 中配置了依赖项,Maven 将在您运行相关目标时自动尝试下载它们。对于您列出的依赖项,这是mvn site。
The majority of those jars you've listed are reports, so should be declared in the reportingsection of the POM, for example (I would also declare the versions to be sure you're getting the expected plugin):
您列出的大多数 jars 都是报告,因此应该在 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>
Some background on Maven's plugin execution model: When you run mvn site, this is short hand for "run the site goal from the latest version of the site plugin", i.e. it is equivalent to mvn site:site, which is in turn shorthand for mvn org.apache.maven.plugins:maven-site-plugin:LATEST:site
Maven 插件执行模型的一些背景:当你运行mvn site 时,这是“从最新版本的站点插件运行站点目标”的简写,即它相当于mvn site:site,它又是简写mvn org.apache.maven.plugins:maven-site-plugin:LATEST:site
Maven will attempt to contact the central repository, determine the LATEST version from the maven-metadata.xml, then download it (and any of its dependencies that are also missing) before executing it.
Maven 将尝试联系中央存储库,从 maven-metadata.xml 确定最新版本,然后在执行它之前下载它(以及它的任何依赖项)。
If you are behind a proxy you may see an error message in your build log like this:
如果您使用代理,您可能会在构建日志中看到如下错误消息:
[INFO] The plugin 'org.apache.maven.plugins:maven-site-plugin' does not exist or no valid version could be found
To address this you can declare proxy settings in your Maven settings.xml (in [MVN_HOME]/conf/settings.xml). They are commented out by defualt, but look something like this:
为了解决这个问题,您可以在 Maven settings.xml(在 [MVN_HOME]/conf/settings.xml)中声明代理设置。它们被 defualt 注释掉,但看起来像这样:
<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>
Replace the username, password, host, and port values with the relevant for your environment and Maven will be able to download the required dependencies.
将用户名、密码、主机和端口值替换为与您的环境相关的值,Maven 将能够下载所需的依赖项。
For more details on using Maven, check out the Maven: The Definitive Guideby Sonatype, it is online and free.
回答by Andy Lynch
Sirakov is right; Maven will download and install the plugins automatically when they are used.
西拉科夫是对的;使用插件时,Maven 会自动下载并安装插件。
You can either run them directly (for one-off jobs), or configure them in your pom.xml - this also allows you to configure then, and set the to run automatically, for example, to generate source code or report on test coverage. A major advantage of this is that you can define a single set of plugin configs in a shared parent pom, and reuse the same configurations across across all your projects, while still being able to override the inherited configuration in each subproject where necessary - this is one of the biggest advantages of using Maven on larger projects.
您可以直接运行它们(用于一次性作业),也可以在 pom.xml 中配置它们——这也允许您进行配置,并将其设置为自动运行,例如,生成源代码或测试覆盖率报告. 这样做的一个主要优点是您可以在共享的父 pom 中定义一组插件配置,并在所有项目中重用相同的配置,同时仍然能够在必要时覆盖每个子项目中继承的配置 - 这是在大型项目中使用 Maven 的最大优势之一。
Each plugin has its own configuration parameters, the standard ones are documented at http://maven.apache.org/plugins/. Another good resource is the O'Reilly Maven book, online at http://www.sonatype.com/books/maven-book/reference/
每个插件都有自己的配置参数,标准参数记录在http://maven.apache.org/plugins/ 中。另一个很好的资源是 O'Reilly Maven 书籍,位于http://www.sonatype.com/books/maven-book/reference/
An example configuration for cobertura:
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>
回答by cupakob
You don't need to download the plugins manually. I'm not 100% sure, but if you want to use for example the checkstyle plugin, you need to start maven with checkstyle parameter form command line
您无需手动下载插件。我不是 100% 确定,但是如果您想使用例如 checkstyle 插件,则需要使用 checkstyle 参数表单命令行启动 maven
something like:
就像是:
mvn checkstyle:checkstyle
or
或者
mvn checkstyle:check
edit1: But you can also put the jars into the local m2 repository with the specific folder structure to access them.
edit1:但是您也可以将jars放入具有特定文件夹结构的本地m2存储库中以访问它们。
edit2: you can put all your plugins into your own repository and then you need to tell maven (using the pom), which repositories you want to use. Every plugin must be described in the pom.
edit2:你可以把你所有的插件放到你自己的仓库中,然后你需要告诉maven(使用pom),你想使用哪些仓库。每个插件都必须在 pom.xml 文件中描述。

