Coding paradigm: line by line all my opinions are belong to me

Maven2 project auto-release [@GitHub] example

What’s about?

So, I finally got my head around maven-release-plugin maven plugin. And, yes, it’s awesome. I have not tried any kind of different/difficult scenarios yet, just plain basic ones.

So, what I wanted to achieve in the first place?

  • G1 - maven artifact auto-versioning on build success

1.1-SNAPSHOT on master branch/TRUNK gets promoted to 1.1 maven release, at the same time master/TRUNK becomes 1.2-SNAPSHOT

  • G2 - some kind of tagging/branching on build success

G1 support on CVS level via tags/branches

  • G3 - maven artifact deployment (from CVS repository to maven repository)
  • G4 - ability to launch it from CI tool

Visually that could be depicted in the following way:

Branching [source]

All the goals accomplished. Yada-ya-da!

Ok, enough reading. Gimme the code See test-maven-release Github project

How-to?

  • G1, G2 - are accomplished by release:prepare goal. Input needed: scm info

Input example:

<scm>
  <connection>
     scm:git:git://github.com/leonardinius/test-maven-release.git
  </connection>
  <developerConnection>
     scm:git:ssh://git@github.com/leonardinius/test-maven-release.git
  </developerConnection>
  <url>
     https://github.com/leonardinius/test-maven-release
  </url>
</scm>

Execution sample:


prusax:test-maven-release leonidmaslov$ mvn release:clean release:prepare
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'release'.
[INFO] ------------------------------------------------------------------------
[INFO] Building test-maven-release
[INFO]    task-segment: [release:clean, release:prepare] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [release:clean]
[INFO] Cleaning up after release...
[INFO] [release:prepare]
[INFO] Verifying that there are no local modifications...
[INFO] Executing: /bin/sh -c cd /Users/leonidmaslov/Documents/workspace/test-maven-release && git status
[INFO] Working directory: /Users/leonidmaslov/Documents/workspace/test-maven-release
[INFO] Checking dependencies and plugins for snapshots ...
What is the release version for "test-maven-release"? (leonardinius-test:test-maven-release) 1.0: : 
What is SCM release tag or label for "test-maven-release"? (leonardinius-test:test-maven-release) test-maven-release-1.0: : myTag
What is the new development version for "test-maven-release"? (leonardinius-test:test-maven-release) 1.1-SNAPSHOT: : 
[INFO] Transforming 'test-maven-release'...
[INFO] Not generating release POMs
[INFO] Executing goals 'clean verify'...
[WARNING] Maven will be executed in interactive mode, but no input stream has been configured for this MavenInvoker instance.
[INFO] [INFO] Scanning for projects...
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [INFO] Building test-maven-release
[INFO] [INFO]    task-segment: [clean, verify]
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [INFO] [clean:clean]
[INFO] [INFO] [resources:resources]
[INFO] [WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] [INFO] skip non existing resourceDirectory /Users/leonidmaslov/Documents/workspace/test-maven-release/src/main/resources
[INFO] [INFO] [compiler:compile]
[INFO] [INFO] Compiling 1 source file to /Users/leonidmaslov/Documents/workspace/test-maven-release/target/classes
[INFO] [INFO] [resources:testResources]
[INFO] [WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] [INFO] Copying 1 resource
[INFO] [INFO] [compiler:testCompile]
[INFO] [INFO] Compiling 1 source file to /Users/leonidmaslov/Documents/workspace/test-maven-release/target/test-classes
[INFO] [INFO] [surefire:test]
[INFO] [INFO] Surefire report directory: /Users/leonidmaslov/Documents/workspace/test-maven-release/target/surefire-reports
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running me.doautomate.ExampleServletTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.024 sec
[INFO] 
[INFO] Results :
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] [INFO] [jar:jar]
[INFO] [INFO] Building jar: /Users/leonidmaslov/Documents/workspace/test-maven-release/target/test-maven-release-1.0.jar
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [INFO] BUILD SUCCESSFUL
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] [INFO] Total time: 3 seconds
[INFO] [INFO] Finished at: Thu Feb 24 22:02:49 CET 2011
[INFO] [INFO] Final Memory: 22M/81M
[INFO] [INFO] ------------------------------------------------------------------------
[INFO] Checking in modified POMs...
[INFO] Executing: /bin/sh -c cd /Users/leonidmaslov/Documents/workspace/test-maven-release && git add pom.xml
[INFO] Working directory: /Users/leonidmaslov/Documents/workspace/test-maven-release
[INFO] Executing: /bin/sh -c cd /Users/leonidmaslov/Documents/workspace/test-maven-release && git status
[INFO] Working directory: /Users/leonidmaslov/Documents/workspace/test-maven-release
[INFO] Executing: /bin/sh -c cd /Users/leonidmaslov/Documents/workspace/test-maven-release && git commit --verbose -F /var/folders/IA/IA2V6NfrE84m+Fcb-LJguU+++TI/-Tmp-/maven-scm-811668125.commit pom.xml
[INFO] Working directory: /Users/leonidmaslov/Documents/workspace/test-maven-release
[INFO] Executing: /bin/sh -c cd /Users/leonidmaslov/Documents/workspace/test-maven-release && git push
[INFO] Working directory: /Users/leonidmaslov/Documents/workspace/test-maven-release
[INFO] Tagging release with the label myTag...
[INFO] Executing: /bin/sh -c cd /Users/leonidmaslov/Documents/workspace/test-maven-release && git tag -F /var/folders/IA/IA2V6NfrE84m+Fcb-LJguU+++TI/-Tmp-/maven-scm-1676785108.commit myTag
[INFO] Working directory: /Users/leonidmaslov/Documents/workspace/test-maven-release
[INFO] Executing: /bin/sh -c cd /Users/leonidmaslov/Documents/workspace/test-maven-release && git push origin myTag
[INFO] Working directory: /Users/leonidmaslov/Documents/workspace/test-maven-release
[INFO] Executing: /bin/sh -c cd /Users/leonidmaslov/Documents/workspace/test-maven-release && git ls-files
[INFO] Working directory: /Users/leonidmaslov/Documents/workspace/test-maven-release
[INFO] Transforming 'test-maven-release'...
[INFO] Not removing release POMs
[INFO] Checking in modified POMs...
[INFO] Executing: /bin/sh -c cd /Users/leonidmaslov/Documents/workspace/test-maven-release && git add pom.xml
[INFO] Working directory: /Users/leonidmaslov/Documents/workspace/test-maven-release
[INFO] Executing: /bin/sh -c cd /Users/leonidmaslov/Documents/workspace/test-maven-release && git status
[INFO] Working directory: /Users/leonidmaslov/Documents/workspace/test-maven-release
[INFO] Executing: /bin/sh -c cd /Users/leonidmaslov/Documents/workspace/test-maven-release && git commit --verbose -F /var/folders/IA/IA2V6NfrE84m+Fcb-LJguU+++TI/-Tmp-/maven-scm-1157159798.commit pom.xml
[INFO] Working directory: /Users/leonidmaslov/Documents/workspace/test-maven-release
[INFO] Executing: /bin/sh -c cd /Users/leonidmaslov/Documents/workspace/test-maven-release && git push
[INFO] Working directory: /Users/leonidmaslov/Documents/workspace/test-maven-release
[INFO] Release preparation complete.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 minute 
[INFO] Finished at: Thu Feb 24 22:02:59 CET 2011
[INFO] Final Memory: 14M/81M
[INFO] ------------------------------------------------------------------------
prusax:test-maven-release leonidmaslov$ 

</li><li>G3 - is accomplished by release:perform plugin. The plugin checkouts the tag/branch from the CVS system, builds it and deploys to maven repository. NB: relies on release:prepare being called first (needs release.properties file). Input needed: scm info, distribution management Input example:

<scm>
    <connection>
      scm:git:git://github.com/leonardinius/test-maven-release.git
    </connection>
    <developerConnection>
      scm:git:ssh://git@github.com/leonardinius/test-maven-release.git
    </developerConnection>
    <url>https://github.com/leonardinius/test-maven-release</url>
</scm>

<distributionManagement>
    <site>
        <id>site-id</id>
        <name>site-name</name>
        <url>scp://host/path/to/repository/</url>
    </site>
    <repository>
        <id>repo-main</id>
        <name>Repository Name</name>
        <url>file://tmp/</url>
    </repository>
    <!-- use the following if you ARE using a snapshot version. -->
    <snapshotRepository>
        <id>repo-snapshot</id>
        <name>Repository Name</name>
        <url>file://tmp/</url>
    </snapshotRepository>
</distributionManagement>

</li><li>G4 is accomplished by launching maven in the batch mode. Example:

mvn clean --batch-mode  release:prepare release:perform

Conclusion

Adding automated maven project versioning is not so hard and could be easily automated.


NB. If you've found typos or errors, please suggest a correction or edit on github.
comments powered by Disqus