简体   繁体   English

版本 Maven 可继承的插件规则

[英]Versions Maven Plugin rules that are inheritable

When running mvn versions:display-dependency-updates for the Version Maven Plugin I see lots of things like this:当运行版本Maven 插件mvn versions:display-dependency-updates时,我看到很多这样的事情:

[INFO]   org.slf4j:slf4j-api ........................... 1.7.36 -> 2.0.0-alpha7

But just because I'm not using the alpha version of a later version doesn't mean I'm not using the latest available release version.但仅仅因为我没有使用更高版本的 alpha 版本并不意味着我没有使用最新的可用发布版本。 Another Stack Overflow answer indicated that I can set up a rules.xml file to ignore versions like *.-alpha* , putting something like this in my POM:另一个Stack Overflow 答案表明我可以设置一个rules.xml文件来忽略*.-alpha*之类的版本,在我的 POM 中放置这样的内容:

<configuration>
  <rulesUri>file:///${project.basedir}/rules.xml</rulesUri>
</configuration>

My question: is this rules.xml file inheritable?我的问题:这个rules.xml文件可以继承吗? If I put it in a separate project in a parent POM of <packaging>pom</packaging> , published to Maven Central, will the child POMs pick it up?如果我将它放在<packaging>pom</packaging>的父 POM 中的单独项目中,发布到 Maven Central,子 POM 会接受它吗? Or will the child projects look for a rules.xml file in the child project directory ?或者子项目会在子项目目录中查找rules.xml文件吗?

I want to configure the versions-maven-plugin in the parent POM (as I do already) and run mvn versions:display-dependency-updates on any child POM or descendant POM.我想在父 POM 中配置versions-maven-plugin (就像我已经做的那样)并在任何子 POM 或后代 POM 上运行mvn versions:display-dependency-updates How can I set up the ignore rules in the parent POM so that these version ignore rules will be picked up when I check for dependency updates in a child POM?如何在父 POM 中设置忽略规则,以便在我检查子 POM 中的依赖项更新时选择这些版本忽略规则? (Is there no way to include the rule within the POM itself?) (有没有办法将规则包含在 POM 本身中?)

Or will the child projects look for a rules.xml file in the child project directory?或者子项目会在子项目目录中寻找rules.xml文件吗?

Yes, if you define the rules.xml file via ${project.basedir} it will resolve to the current local base directory of the child project.是的,如果您通过${project.basedir}定义rules.xml文件,它将解析到子项目的当前本地基目录。 I've verified this with a simple parent-child pom setup.我已经通过简单的父子 pom 设置验证了这一点。 So that will not work, unless you duplicate the rules file in every project.所以这行不通,除非您在每个项目中复制规则文件。

If you wish to include the plugin configuration and ruleset in the parent pom without duplicating the rules file, you have two options:如果您希望在父 pom 中包含插件配置规则集而不复制规则文件,您有两种选择:

If you have your ruleset xml file hosted at, for example, http://www.mycompany.com/maven-version-rules.xml then the following configuration in your corporate pom would ensure that all projects use this rule set.例如,如果您将规则集 xml 文件托管在http://www.mycompany.com/maven-version-rules.xml ,那么您公司 pom 中的以下配置将确保所有项目都使用此规则集。

<configuration>
    <rulesUri>http://www.mycompany.com/maven-version-rules.xml</rulesUri>
</configuration>

or或者

You can provide your ruleset xml file also within a jar, if you want to distribute your ruleset xml as Maven artifact.如果您想将您的规则集 xml 作为 Maven 工件分发,您也可以在 jar 中提供您的规则集 xml 文件。 Therefore you have to declare the containing jar as direct dependency of the versions-maven-plugin and to use classpath as protocol.因此,您必须将包含的 jar 声明为 versions-maven-plugin 的直接依赖项,并将类路径用作协议。

<configuration>
  <rulesUri>classpath:///package/foo/bar/rules.xml</rulesUri>
</configuration>
<dependencies>
    <dependency>
        <groupId>com.mycompany</groupId>
        <artifactId>version-rules</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

Source:资源:

The configuration in the pom only has rudimentary includes and excludes filters. pom 中的配置只有基本的includesexcludes过滤器。 Those will allow you to exclude any dependency as a whole, but not specific update versions.这些将允许您将任何依赖项作为一个整体排除,但不能排除特定的更新版本。 As far as i can tell from the available documentation there is no way to define version rules in any other way.据我从可用文档中可以看出,无法以任何其他方式定义版本规则。

See

Pasting my answer here from Github, because I think it might benefit others.从 Github 在这里粘贴我的答案,因为我认为它可能会使其他人受益。

Provided you have a directory called rules-test in your project containing the rules template file:假设您的项目中有一个名为rules-test的目录,其中包含规则模板文件:

<ruleset comparisonMethod="maven"
         xmlns="http://mojo.codehaus.org/versions-maven-plugin/rule/2.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://mojo.codehaus.org/versions-maven-plugin/rule/2.0.0
         https://www.mojohaus.org/versions-maven-plugin/xsd/rule-2.0.0.xsd">
    <ignoreVersions>
        <ignoreVersion type="regex">${ignoredVersions}</ignoreVersion>
    </ignoreVersions>
</ruleset>

Then, in your main project, create the following profile:然后,在您的主项目中,创建以下配置文件:

<profile>
  <id>rules-test</id>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.3.0</version>
        <executions>
          <execution>
            <phase>validate</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <resources>
                <resource>
                  <directory>rules-test</directory>
                  <filtering>true</filtering>
                </resource>
              </resources>
              <outputDirectory>${project.basedir}</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>versions-maven-plugin</artifactId>
        <version>2.12.0</version>
        <executions>
          <execution>
            <phase>validate</phase>
            <goals>
              <goal>display-dependency-updates</goal>
            </goals>
            <configuration>
              <rulesUri>file://${project.basedir}/compiled-rules.xml</rulesUri>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</profile>

If you then execute the following Maven target:如果您随后执行以下 Maven 目标:

mvn -P rules-test "-DignoredVersions=.*-(M\d*|.*-SNAPSHOT)" clean validate

then you will get a dependencies report using the filter in the -DignoredVersions argument (filtering out both *-M* and *-SNAPSHOT ).然后您将使用-DignoredVersions参数中的过滤器获得依赖关系报告(过滤掉*-M**-SNAPSHOT )。

And if you put your ignoredVerions property in your project instead of passing it as a -D argument, then it will be inheritable!如果你把你的ignoredVerions属性放在你的项目中,而不是将它作为 -D 参数传递,那么它将是可继承的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM