在Maven 2中通过命令行设置插件版本

49

我生成了默认的快速开始Maven示例,并输入mvn checkstyle:checkstyle,它总是尝试使用最新的SNAPSHOT版本。可能这在我的Nexus服务器上是错误的,但我如何在Maven 2的命令行上设置插件的版本,例如将checkstyle的版本设置为2.5而不是2.6-SNAPSHOT

C:\HelloWorld>mvn checkstyle:checkstyle
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'checkstyle'.
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Error building POM (may not be this project's POM).

Project ID: org.apache.maven.plugins:maven-checkstyle-plugin

Reason: Error getting POM for 'org.apache.maven.plugins:maven-checkstyle-plugin' from  the repository: Failed to resolve artifact, possibly due to a repository list that is not appropriately equipped for this artifact's metadata.
  org.apache.maven.plugins:maven-checkstyle-plugin:pom:2.6-SNAPSHOT

from the specified remote repositories:
  nexus (http://localhost:9081/nexus/content/groups/public)

for project org.apache.maven.plugins:maven-checkstyle-plugin

我猜可能是mvn checkstyle:2.5:checkstyle,但不幸的是它没有生效。

如果我在pom.xml中设置构建依赖,肯定可以生效,但我想看看命令行怎么用。


如果我输入完整的构件,它就可以工作 mvn org.apache.maven.plugins:maven-checkstyle-plugin:2.5:checkstyle - Larry Cai
4个回答

81

如果你不想运行本地存储库中安装的插件的最新版本,你需要设置版本号。为此,您需要指定形式为完全限定目标的目标:

mvn groupID:artifactID:version:goal

所以在你的情况下:

mvn org.apache.maven.plugins:maven-checkstyle-plugin:2.5:checkstyle

3
“在本地仓库安装的插件中运行最新版本”似乎不正确。我的本地仓库中有maven-dependency-plugin的版本是2.82.103.0.0,但Maven默认运行的是2.8版本。即使我删除了2.8版本,在下一次运行时Maven也会重新下载它。 - Gerold Broser

3

我使用较新版本的Maven(3.3.9)的经验略有不同。如果我没有在离线模式下运行Maven,则无论我本地存储库中有什么,它都会始终寻找远程存储库。如果它找到了一个更新的版本(基于maven-metadata.xml),它将下载并使用该版本。更重要的是,它可以同时扫描多个存储库:

[INFO] ------------------------------------------------------------------------
[DEBUG] Resolving plugin version for com.phoenixnap.oss:springmvc-raml-plugin
[DEBUG] Could not find metadata com.phoenixnap.oss:springmvc-raml-plugin/maven-metadata.xml in local (/home/yuranos/.m2/repository)
[DEBUG] Using transporter WagonTransporter with priority -1.0 for https://repo.spring.io/libs-milestone
[DEBUG] Using transporter WagonTransporter with priority -1.0 for https://repo.spring.io/milestone
[DEBUG] Using transporter WagonTransporter with priority -1.0 for https://repo.maven.apache.org/maven2
[DEBUG] Using transporter WagonTransporter with priority -1.0 for https://repo.spring.io/snapshot
[DEBUG] Using connector BasicRepositoryConnector with priority 0.0 for https://repo.spring.io/libs-milestone
[DEBUG] Using connector BasicRepositoryConnector with priority 0.0 for https://repo.spring.io/milestone
[DEBUG] Using connector BasicRepositoryConnector with priority 0.0 for https://repo.maven.apache.org/maven2
[DEBUG] Using connector BasicRepositoryConnector with priority 0.0 for https://repo.spring.io/snapshot
Downloading: https://repo.spring.io/libs-milestone/com/phoenixnap/oss/springmvc-raml-plugin/maven-metadata.xml
Downloading: https://repo.spring.io/snapshot/com/phoenixnap/oss/springmvc-raml-plugin/maven-metadata.xml
Downloading: https://repo.maven.apache.org/maven2/com/phoenixnap/oss/springmvc-raml-plugin/maven-metadata.xml
Downloading: https://repo.spring.io/milestone/com/phoenixnap/oss/springmvc-raml-plugin/maven-metadata.xml
[DEBUG] Writing tracking file /home/yuranos/.m2/repository/com/phoenixnap/oss/springmvc-raml-plugin/resolver-status.properties
[DEBUG] Writing tracking file /home/yuranos/.m2/repository/com/phoenixnap/oss/springmvc-raml-plugin/resolver-status.properties
Downloaded: https://repo.maven.apache.org/maven2/com/phoenixnap/oss/springmvc-raml-plugin/maven-metadata.xml (2 KB at 1.0 KB/sec)
[DEBUG] Writing tracking file /home/yuranos/.m2/repository/com/phoenixnap/oss/springmvc-raml-plugin/resolver-status.properties
Downloaded: https://repo.spring.io/libs-milestone/com/phoenixnap/oss/springmvc-raml-plugin/maven-metadata.xml (2 KB at 0.9 KB/sec)
[DEBUG] Writing tracking file /home/yuranos/.m2/repository/com/phoenixnap/oss/springmvc-raml-plugin/resolver-status.properties
[DEBUG] Could not find metadata com.phoenixnap.oss:springmvc-raml-plugin/maven-metadata.xml in spring-snapshots (https://repo.spring.io/snapshot)
[DEBUG] Could not find metadata com.phoenixnap.oss:springmvc-raml-plugin/maven-metadata.xml in spring-milestones-libs (https://repo.spring.io/milestone)

此外,Maven似乎知道如何比较多个元数据文件并选择最新版本的构件。根据我在resolver-status.properties中看到的,Maven利用Aether解析依赖关系。
    #NOTE: This is an Aether internal implementation file, its format can be changed without prior notice.
#Mon Feb 19 23:41:24 EET 2018
maven-metadata-spring-milestones.xml.lastUpdated=1519076484366
maven-metadata-spring-snapshots.xml.error=
maven-metadata-central.xml.lastUpdated=1519076484205
maven-metadata-spring-snapshots.xml.lastUpdated=1519076484107
maven-metadata-spring-milestones-libs.xml.lastUpdated=1519076484105
maven-metadata-spring-milestones-libs.xml.error=

3
mvn org.apache.maven.plugins:maven-site-plugin:3.7.1:site

欢迎来到 Stack Overflow,并感谢您的回答。最好的答案会对代码或命令进行一些解释。您可以[编辑]此答案以改进它。 - O. Jones

0

你也可以使用:

mvn {你的groupId}:{你的artifactId}:{你的version}:{你的goal}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接