Maven versions plugin: reference a rule.xml from a maven dependency?
Asked Answered
L

2

10

I am using the mvn versions:display-dependency-updates versions:display-plugin-updates goals to check for dependencies or plugins updates.

My maven project is a multi module one, which looks like this:

moduleA
 |- moduleB1
 |    |- moduleC  
 |- moduleB2
 |- build-config/rules.xml

Since there is some unwanted updates, like betas I don't want, I've made a filter (which works). I use it like that:

<profile>
  <id>maven-version-plugin-1</id>
  <activation>
    <property>
      <name>version.rules.uri</name>
    </property>
  </activation>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>versions-maven-plugin</artifactId>
        <configuration>
          <rulesUri>${version.rules.uri}</rulesUri>
        </configuration>
      </plugin>
    </plugins>
  </build>
</profile>

I am forced to use a profile and a property version.rules.uri because it must refer to an existing file (by default it points to ./build-config/rules.xml, but it is also in my settings.xml with an absolute path).

I'd like to avoid that by:

  • publishing an independent build-config project
  • referencing this project using some uri: m2:myGroupId:myArtifactId:version:scope:jar/rules.xml

Now the question: is there an implementation of Maven Wagon Plugin (which is used by maven versions plugin) that allow for reading a repository entry such as a jar ?

Leukocyte answered 30/9, 2014 at 12:20 Comment(0)
H
4

Based upon the documentation for the plugin this is possible:

You can provide your ruleset xml file also within a jar, if you want to distribute your ruleset xml as Maven artifact. Therefore you have to declare the containing jar as direct dependency of the versions-maven-plugin and to use classpath as protocol.

I just tried it out and got it to work.

Create a new folder for the new version-rules artifact, as so:

version-rules
  |- files
       \- version-rules.xml
  \- pom.xml

The pom.xml is pretty basic:

    ...
    <artifactId>my-version-rules</artifactId>
    <packaging>jar</packaging>

    <build>
        <defaultGoal>package</defaultGoal>
        <resources>
            <resource>
                <directory>files</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    ...

run a mvn install to install this artifact.

Then, in the other pom, you configure the versions plugin as follows:

    ...
    <build>
        ...
        <pluginManagement>
            ...
            <plugins>
                ...
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>versions-maven-plugin</artifactId>
                    <version>2.7</version>
                    <configuration>
                        <rulesUri>classpath:///version-rules.xml</rulesUri>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>com.mycompany</groupId>
                            <artifactId>my-version-rules</artifactId>
                            <version>1.0-SNAPSHOT</version>
                        </dependency>
                    </dependencies>
                </plugin>
                ...
            </plugins>
            ...
        </pluginManagement>
        ...
    </build>
    ...
Heracles answered 6/5, 2019 at 10:31 Comment(4)
I'll retry it but at the time of writing my question my problem was to be able to use a version XML file in a dependency in the same reactor than the one used - in fact, it is more use the version.xml from some absolute path where the parent is (because otherwise, you need a dependency). The previous answer might work better though.Leukocyte
The easiest way to read a jar file from the (local) maven repository, as you seem to want to do, is simply to declare it as a dependency. That way, maven will handle the "where is this jar and how do I get it" for you. ;-)Heracles
This worked but with a problem for the reactor (you can't reference your own dependency during the build, without compiling first).Leukocyte
I put the my-version-rules of this example in our Nexus repository. The project with the versions-maven-plugin could not find it. To solve this, I had to duplicate the repository specified in the ~/.m2/settings.xml as a pluginRepository.Chandigarh
R
12

This works for me:

<rulesUri>file:///${session.executionRootDirectory}/maven-version-rules.xml</rulesUri>

For the meaning of the variable ${session.executionRootDirectory}, see Finding the root directory of a multi module maven reactor project.

Retaliate answered 16/11, 2015 at 2:55 Comment(0)
H
4

Based upon the documentation for the plugin this is possible:

You can provide your ruleset xml file also within a jar, if you want to distribute your ruleset xml as Maven artifact. Therefore you have to declare the containing jar as direct dependency of the versions-maven-plugin and to use classpath as protocol.

I just tried it out and got it to work.

Create a new folder for the new version-rules artifact, as so:

version-rules
  |- files
       \- version-rules.xml
  \- pom.xml

The pom.xml is pretty basic:

    ...
    <artifactId>my-version-rules</artifactId>
    <packaging>jar</packaging>

    <build>
        <defaultGoal>package</defaultGoal>
        <resources>
            <resource>
                <directory>files</directory>
                <filtering>false</filtering>
                <includes>
                    <include>**/*</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    ...

run a mvn install to install this artifact.

Then, in the other pom, you configure the versions plugin as follows:

    ...
    <build>
        ...
        <pluginManagement>
            ...
            <plugins>
                ...
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>versions-maven-plugin</artifactId>
                    <version>2.7</version>
                    <configuration>
                        <rulesUri>classpath:///version-rules.xml</rulesUri>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>com.mycompany</groupId>
                            <artifactId>my-version-rules</artifactId>
                            <version>1.0-SNAPSHOT</version>
                        </dependency>
                    </dependencies>
                </plugin>
                ...
            </plugins>
            ...
        </pluginManagement>
        ...
    </build>
    ...
Heracles answered 6/5, 2019 at 10:31 Comment(4)
I'll retry it but at the time of writing my question my problem was to be able to use a version XML file in a dependency in the same reactor than the one used - in fact, it is more use the version.xml from some absolute path where the parent is (because otherwise, you need a dependency). The previous answer might work better though.Leukocyte
The easiest way to read a jar file from the (local) maven repository, as you seem to want to do, is simply to declare it as a dependency. That way, maven will handle the "where is this jar and how do I get it" for you. ;-)Heracles
This worked but with a problem for the reactor (you can't reference your own dependency during the build, without compiling first).Leukocyte
I put the my-version-rules of this example in our Nexus repository. The project with the versions-maven-plugin could not find it. To solve this, I had to duplicate the repository specified in the ~/.m2/settings.xml as a pluginRepository.Chandigarh

© 2022 - 2024 — McMap. All rights reserved.