Search code examples
javamavenmvn-repo

mvn command for updating version in dependency and one project that uses the dependency


I'm trying to figure out the best practice for updating the version in a POM file that is used by several other Java projects. I'm new to Java and Maven, but not OOP and semver.

I took over a few git repos with Java code using a maven build system. I need to add new features to a "core" repo that is used by several other repos, even though I only need one of the repos to benefit from the new features. My hope is to leave the other repos untouched. If the other repos get redeployed by some devops process, I want them to use the version of "core" without any changes I added to "core".

I think I should update the version in the POM file of the "core" repo before storing its artifact in Nexus so the other repos won't download the version of "core" containing my changes. What mvn command should I use?

Right now, the "core" library declares its version like:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company.team</groupId>
    <artifactId>foo-core</artifactId>
    <version>1.0.4-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>foo-core</name>
    <url>http://maven.apache.org</url>

    <blah/>
    <blah/>

</project>

The projects that use "core" have a POM that looks like:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.company.team</groupId>
    <artifactId>bar</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <description>Bar</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>


    <dependencies>
        <dependency>
            <groupId>com.company.team</groupId>
            <artifactId>foo-core</artifactId>
            <version>1.0.4-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

I'm not sure when <parent> is a useful tag, but I notice it is not being used to connect the application repos to the "core" repo.

I imagine I want to update the POM

  1. for the core
  2. and for the one project that uses the core, but not the other projects that also use the core.

I interpret the fact that the version -SNAPSHOT is used in references to "core" by all the other projects to tell me that no "release" was created for the "core", at least at the current version. Can I skip ever making a release for 1.0.4 if I want to the version of "core" container my features to be 1.0.5-SNAPSHOT?

What are the commands I need to execute in both the "core" repo and the repo for the one project I want to use the new build? I think some commands simply modify the POM, but other commands will publish the build to Nexus.

Or can I just type a new number in the POM and run a mvn command to push the build to Nexus with the new version? If I simply change the value with a text editor, I plan to leave the word -SNAPSHOT in the incremented version of "core" as well as the reference to "core" in the one app I want to use the new features. Then I will just need to publish core to Nexus.

--- UPDATE ---

I learned that this command will bump the version number in "core". It seems unnecessarily complex since you still need to type in the full version value when prompted by the CLI (I typed "1.0.5-SNAPSHOT").

mvn versions:set nestSnapshot

And I guess this is how you update the app to use the latest version of the dependency, but I think it only works if you succeeded to publish the dependency with mvn deploy.

mvn versions:use-latest-releases -Dincludes=com.company.team:foo-core -DallowSnapshots=true

Solution

  • You can just go to the two POM files you mentioned and change the version number in the file.

    I would first change the version of the core, then run a build mvn clean install (if this done on your local machine) or mvn clean deploy (if you want to sent it to your company repository) and then change the version number of core in the other project.

    Note that SNAPSHOT versions are for development. When you want to release something, create a release version, e.g. through the Maven release plugin.