SCM (Software Configuration Management), which is also referred to as version control, is the core of the project. If your organization uses an SCM system, there is an easy way to place information into the POM file. The following code snippet declares a local SVN repository:
[…] <scm> <connection> scm:svn:http://127.0.0.1/svn/my-project </connection> <developerConnection> scm:svn:http://127.0.0.1/svn/myproject </developerConnection> <tag>HEAD</tag> <url>http://127.0.0.1/websvn/my-project</url> </scm> […]
The Maven SCM plugin or site generation uses these SCM configurations in the POM file to perform some tasks and goals. Eclipse, through the M2Eclipse plugin, will materialize your Maven project from SCM using these configurations.
Maven SCM Plugin is a vendor-independent plugin to gain access to SCM supported by Maven. It allows you to execute the basic versioning functionalities of check-in, check-out, update, tag, branch, add, and remove.
The following code snippet declares the plugin in the pom.xml
file:
[…] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-scm-plugin</artifactId> <version>1.9</version> <configuration> <connectionType>connection</connectionType> </configuration> </plugin> </plugins> </build> […]
The connectionType
tag declares the SCM connection to use.
You can check your configuration is correct by executing the validation goal:
$ mvn scm:validate
The result is as follows:
… [INFO] connectionUrl scm connection string is valid. [INFO] project.scm.connection scm connection string is valid. [INFO] project.scm.developerConnection scm connection string is valid. [INFO] ------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------ …
You can perform an SVN checkout by executing the checkout goal:
$ mvn scm:checkout
The result is as follows:
… [INFO] Executing: svn checkout http://localhost/svn/example/ /ws [INFO] Working directory: C:wsxample arget [DEBUG] Checked out revision 0. [INFO] ------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------ …
Maven Release Plugin is the most important plugin in a CI project; the plugin allows us to tag the source code using the SCM information and change the POM release and snapshot version. It is based on two phases: the prepare phase and the perform/rollback phase. As the first step, the plugin asks the user for a specific version, prepares the release version, and makes the tag of the component. As the second step, Maven deploys (or rolls back) the changes.
The plugin was introduced after Maven 2, but it is strongly recommended to use Maven Version 3.0.4 or later. The plugin makes some assumptions:
SNAPSHOT
)allowTimestampedSnapshots
and ignoreSnapshots
dependencies must be released or set to true
The plugin performs some actions:
SNAPSHOT
suffixTo add the plugin, you have to configure the pom.xml
file, as follows:
[…] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-release-plugin</artifactId> <version>2.5</version> <configuration> <tagBase> http://127.0.0.1/websvn/my-project/tags </tagBase> </configuration> </plugin> </plugins> </build> […]
The next step is to launch Maven, as follows:
$ mvn release:clean release:prepare
Maven will ask for the new released version ID, tag name, and new development release (SNAPSHOT
) ID:
What is SCM release tag or label for "Chp5Release Test"? (com.mycompany.project s:my-first-maven-project) my-first-maven-project-1.0: : … What is SCM release tag or label for "Chp5 Release Test"? (com.mycompany.project s:my-first-maven-project) my-first-maven-project-1.0: : … What is the new development version for "Chp5 Release Test"? (com.mycompany.proj ects:my-first-maven-project) 1.1-SNAPSHOT: : … [INFO] Release preparation complete. [INFO] ----------------------------------------------------- [INFO] BUILD SUCCESS [INFO] -----------------------------------------------------
Then, you can deploy the changes on a remote repository:
$ mvn release:perform
Maven will check out from SCM and release the project (deploy
and site-deploy
) on the remote repository. This step requires the definition of the repository location.
The released software must be deployed on the remote (Nexus) repository in order to be accessible by every developer. Generally speaking, it is a good idea to create different repositories for each team of your organization on the remote server in order to control access to the artifacts; in our case, we use the default Nexus repositories. If your organization doesn't require strict access to some artifacts, the default Nexus installation is what you are looking for.
To publish the released plugin on Nexus, we have to define the release or snapshot repository, and we can call the maven deploy
command:
$ mvn deploy
You can also use the following command if you use Maven Release Plugin:
$ mvn release:perform
In the pom.xml
file, we have to define the repository manager (Nexus) URL:
[…] <properties> <repo-id>nexus_releases</repo-id> <repo-name>Nexus Release</repo-name> <repo-url> http://localhost/nexus/content/repositories/releases/ </repo-url> </properties> […] <distributionManagement> <repository> <id>${repo-id}</id> <name>${repo-name}</name> <url>${repo-url}</url> </repository> </distributionManagement> […]
If the repository requires authentication, we have to define the credentials in the settings.xml
file:
<server> <id>nexus_releases</id> <username>admin</username> <password>admin123</password> </server>
Formally speaking, the example proposed here is not correct, and Maven offers two types of repository declarations: repository
and snapshotRepository
. A snapshotRepository
declaration is used only for the snapshot artifact. A repository
declaration is used for the release and snapshot artifacts if the snapshot repository has not been declared. To conclude, a more correct definition of the repositories is as follows:
[…] <distributionManagement> <repository> <id>${repo-id}</id> <name>${repo-name}</name> <url>${repo-url}</url> </repository> <snapshotRepository> <id>${repo-snapshot-id}</id> <name>${repo-snapshot-name}</name> <url>${repo-snapshot-url}</url> </snapshotRepository> </distributionManagement> […]
In this case, Maven will deploy the release artifact in the release repository and snapshot artifact (version x.x.x-SNAPSHOT
) in the snapshot repository.