简体   繁体   中英

How to go from snapshot to release with maven and jenkins

I am using Maven as my build tool and Jenkins as my build server. I am trying to make it easy to develop and build.

I have the following:

Global pom

<project>
    <!-- ... -->
    <artifactId>global-pom</artifactId>
    <!-- ... -->
</project>

Libraries pom (parent pom is Global), this has a load of common libraries

<project>
    <!-- ... -->
    <version>2.0-SNAPSHOT</version>
    <artifactId>libraries-pom</artifactId>

    <parent>
        <!-- ... -->
        <artifactId>global-pom</artifactId>
        <version>2.0-SNAPSHOT</version>
    </parent>

    <properties>
        <libraries.version>2.0-SNAPSHOT</libraries.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- ... -->
                <artifactId>library-one</artifactId>
                <version>${libraries.version}</version>
            </dependency>
            <dependency>
                <!-- ... -->
                <artifactId>library-two</artifactId>
                <version>${libraries.version}</version>
            </dependency>
            <!-- ... -->
        </dependencies>
    </dependencyManagement>

    <modules>
        <module>library-one</module>
        <module>library-two</module>
        <!-- ... -->
    </modules>
</project>

A common library (parent pom is Libraries) pom will look like this:

<project>
    <!-- ... -->
    <version>2.0-SNAPSHOT</version>
    <artifactId>library-one</artifactId>

    <parent>
        <!-- ... -->
        <artifactId>libraries-pom</artifactId>
        <version>2.0-SNAPSHOT</version>
    </parent>

    <!-- ... -->
</project>

Then a service pom (parent pom is Libraries) that may look like this:

<project>
    <!-- ... -->
    <version>1.0-SNAPSHOT</version>
    <artifactId>service-one</artifactId>

    <parent>
        <!-- ... -->
        <artifactId>libraries-pom</artifactId>
        <version>2.0-SNAPSHOT</version>
    </parent>

    <!-- ... -->
</project>

Now this is all well and good when I am developing locally. I can have all these poms in my Intellij project and if I change something in one of the libraries to be used in one of the projects, then when I build and run locally, it knows to build the library first, etc...

Whenever something is checked in, a SNAPSHOT version automatically builds on Jenkins.

When I come to actually releasing I don't want to be using SNAPSHOT versions, I want to use "real" versions and I am wondering how best to manage this so as to not impact development or releases.

Has anyone got any ideas? Ideally, when I want to release, I'd like to goto my build server and click build and it automatically sort everything out for me - is this possible?

You will need to use the Maven release plugin to perform automated releases of all your artifacts, by issuing some Maven command-line instructions in batch mode.

For that include the following configuration in your parent pom:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-release-plugin</artifactId>
    <version>2.5.1</version>
    <configuration>
      <autoVersionSubmodules>true</autoVersionSubmodules>
      <tagNameFormat>v@{project.version}</tagNameFormat>
      <goals>deploy</goals>
    </configuration>
</plugin>

To make a release, you need to execute two steps. First mvn -B release:prepare and mvn -B release:perform .

By running Maven in batch mode (-B) these steps automatically will make a release, tag, deploy to a remote repository and push the changes to your version control system (assuming git), which must be specified in the pom.

However, to test if the steps are doing what you intend, I would first run them locally in interactive mode (without -B), and including some extra configuration to the plugin:

<localCheckout>true</localCheckout>
<pushChanges>false</pushChanges>

These will prevent a push to git. If something is not right with the release, you can then do mvn release:rollback and clean up the git repo locally, and then fixing the release plugin configuration.

To run that in Jenkins, you need create a new job that will run both prepare and perform commands in batch mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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