项目作者: UW-Madison-Library

项目描述 :
A Custom SCM for Maven artifacts
高级语言: Ruby
项目地址: git://github.com/UW-Madison-Library/capistrano-scm-maven.git
创建时间: 2018-08-17T02:17:49Z
项目社区:https://github.com/UW-Madison-Library/capistrano-scm-maven

开源协议:MIT License

下载


Capistrano SCM Maven

A Capistrano SCM plugin is supposed to be a client of a proper Source
Control system, right? Well, what if you’re working in a compiled
language and your source has already been versioned and a compiled
artifact made from it? That’s where this plugin comes in. It complies
with the interface of an SCM plugin (pretty well, I think) but the
“repo” is a tarball that contains your application.

I chose Maven for the name because it is the artifact repository
standard that Maven created (and Artifactory and Nexus both
implement). I doesn’t refer to building Java source with the Maven’s
mvn tool. Thanks to jmpage’s Capistrano SCM Nexus
plugin
for ideas and
reference, which in some cases resulted in outright theivery.

Background

My team is made up of Java developers and Ruby developers. Looking
over the fence from the Java side of things, I was jealous of the Ruby
team’s ability to use Capistrano - a great automated deployment
tool. Some of our Java projects use Python’s Fabric which is very nice
but it’s the only python tool we use. So, in the interest of
standardizing on one tool, especially with an eye for automating
deploys in a similar way in a Gitlab pipeline, I decided to see if
Capistrano would work for Java deployments.

Notes

(differences from other SCM plugins)

  1. Because we’re deploying a binary tarball, the directory structure
    within the artifact is important. In short, it needs to match the
    final expanded directory structure to work
    . I call this structure
    the maven_artifact_style and default it to ‘cap’. For Java projects,
    this means that the executable jar within the tarball should not have
    the version on it so scripts and/or systemd can find it
    consistently. In fact nothing in the tarball should be versioned. This
    doesn’t cause version confusion because the tarball is versioned and
    the extracted version of the executable jar will already be contained
    within a specific release in the normal Capistrano releases
    directory. I discuss below how to use Maven’s Assembly plugin to
    create a tarball artifact compliant with this gem.

  2. When I refer to the term repo in this documentation, I mean the
    artifact repository. But in the plugin framework code, repo means the
    artifact contents.

Features

  • Supports separate release and snapshot repositories. This is needed
    in Artifactory (I don’t know anything about Nexus but supposedly
    should be useful there too).
  • Downloads the artifact from repo only if archive is a snapshot or is
    a release and not present already
  • Supports non-standard repo ports
  • Supports 302 redirects from the repo

Install

Install the gem manually:

  1. gem install capistrano-scm-maven

or add it to your Gemfile when using Bundler:

  1. gem 'capistrano-scm-maven'

Configure

Require the gem in your Capfile:

  1. require 'capistrano_scm_maven'

Configure the gem’s variables

  1. set :maven_endpoint, 'http://agoodno.com:8081/artifactory'
  2. set :maven_repository, 'libs-snapshot'
  3. set :maven_group_id, 'com.agoodno'
  4. set :maven_artifact_version, '0.0.1-SNAPSHOT'
  5. set :maven_artifact_name, 'sample'
  6. set :maven_artifact_style, 'cap'
  7. set :maven_artifact_ext, 'tar.gz'

The configuration above would result in an attempt to retrieve the
artfact at:

  1. `http://agoodno.com:8081/artifactory/libs-snapshot/com/agoodno/sample/0.0.1-SNAPSHOT/sample-0.0.1-SNAPSHOT-cap.tar.gz`

Build Artifact

Add assembly plugin

Add Maven’s Assembly
Plugin
to
your Maven POM file.

pom.xml

  1. ...
  2. <build>
  3. <plugins>
  4. ...
  5. <plugin>
  6. <artifactId>maven-assembly-plugin</artifactId>
  7. <version>3.1.0</version>
  8. <configuration>
  9. <descriptors>
  10. <descriptor>src/assembly/cap.xml</descriptor>
  11. </descriptors>
  12. </configuration>
  13. <executions>
  14. <execution>
  15. <id>make-assembly</id>
  16. <phase>package</phase>
  17. <goals>
  18. <goal>single</goal>
  19. </goals>
  20. </execution>
  21. </executions>
  22. </plugin>
  23. </plugins>
  24. </build>
  25. ...

Create the assembly file

The assembly file referenced in the POM above describes how to build
the tarball with your application’s final directory structure. Setting
includeBaseDirectory to false below removes the versioned
container directory that is created by default. Also, destName is
set to remove the version from the aplication jar. I’ve only added a
README.md in addition to the application jar. See the Assembly
plugin’s documentation to build out your application’s whole
structure.

src/assembly/cap.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
  5. <id>cap</id>
  6. <formats>
  7. <format>tar.gz</format>
  8. </formats>
  9. <includeBaseDirectory>false</includeBaseDirectory>
  10. <files>
  11. <file>
  12. <source>target/${artifactId}-${version}.${packaging}</source>
  13. <destName>${artifactId}.${packaging}</destName>
  14. </file>
  15. <file>
  16. <source>README.md</source>
  17. </file>
  18. </files>
  19. </assembly>

Verify

You can verify that the gem is installed by listing the tasks
available to capistrano with:

  1. cap -T

You should see 3 maven namespaced commands in the list.

Run

Because this gem takes part in the normal capistrano deploy lifecycle,
to run it just run the deploy task as you normally would:

  1. cap staging deploy