Continuous Integration:
In an enterprise project, it is important
to continually check the non regression of the product realized. Like unit
tests, acceptance tests are part of the test harness to implement a project.
Below are the usual set of tasks.
- Build
- Unit Test
- Run Code Quality Checks
- Deploy
- Run Acceptance Test
In my current project, we have chosen
below tools for Continuous Integration strategy:
- Maven to build and unit test
- Sonar to perform code quality checks
- Nexus as Maven repository
- Shell Scripting to deploy
In today's post, we will go over how to
use Maven and Nexus to build and publish binaries.
Maven to Build and Unit Test:
Building java projects with maven is
really easy. You just need to have maven-compiler-plugin in your pom.xml. Java
sources will be compiled without doing any more work If you follow standard
maven guidelines for your project folder structure.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
</plugin>
Unit testing is possible
with maven-surefire-plugin. Maven-surefire-plugin can run tests with testing
frameworks like Junit, TestNG etc.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkMode>pertest</forkMode>
<childDelegation>false</childDelegation>
<useFile>true</useFile>
<failIfNoTests>false</failIfNoTests>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
Maven to Publish artifacts to Nexus:
All maven projects have artifacts that are
generated by the build. An artifact can be a jar file, war file, zip file, ear
file and a pom file. All these artifacts need to be stored in a repository for
versioning purposes.
Your project’s pom.xml will have details
of Nexus as maven repository in distribtionManagement section. Make sure your
maven settings file has authentication details to publish to Nexus repository.
Maven deploy goal needs to be executed to deploy to Nexus repository.
Pom.xml:
<project>
...
<distributionManagement>
<repository>
<id>releases</id>
<uniqueVersion>false</uniqueVersion>
<name>Company Releases</name> <url>http://localhost:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<uniqueVersion>false</uniqueVersion>
<name>Company Snapshots</name> <url>http://localhost:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
...
</project>
Settings.xml:
</settings>
...
<servers>
<server>
<id>snapshots</id>
<username>deploy</username>
<password>deploypwd</password>
</server>
<server>
<id>releases</id>
<username>deploy</username>
<password>deploypwd</password>
</server>
</servers>
...
</settings>
NB: to prepare your release, and create a new tag on the source
code repository:
mvn release: prepare -Dresume = false (alternatively mvn release: clean release:
prepare) and to deploy the new tag in the staging repository:
mvn release:perform
Continuous delivery:
Enjoy !! :-)