Archive for the ‘maven’ Category

Some mvn tips

Posted: December 22, 2011 in maven

Hi all, as you may know TurmericSOA has a multi-module project structure, as example take a look at Rate Limiter module under turmeric-security project:

turmeric-security/ratelimiter
|
|---RateLimiterService
|---RateLimiterProvider
|---RateLimiterCounterProvider
|---RateLimiterServiceConsumer
|---RateLimiterServiceImpl
|---RateLimiterErrorLibrary
|---RateLimiterCounterMapProviderImpl
|---RateLimiterCounterCassandraProviderImpl
|---RateLimiterProviderImpl
|readme.txt
|pom.xml

a simple mvn clean package process takes about 9 minutes…no so bad, it could be worst, for example if you build the entire security project….please be patient 🙂

For those kind of projects here I recall you some useful tips building with maven…

  • Use the maven reactor plugin: in short terms it allows to resume your build process where last fails has placed or simlply from where you specify:

In my case I pasted the RateLimiter module structure based on its dependencies, not alphabetically… Now assume you’ve got a fail at RateLimiterCounterMapProviderImpl module, if you run a simple “mvn clean install” you are running a complete rate limiter project build, instead of that try: –resume-from  or -rf

mvn clean package –rf RateLimiterCounterMapProviderImpl

That command just runs RateLimiterCounterMapProviderImpl, RateLimiterCounterCassandraProviderImpl and RateLimiterProviderImpl.

__________________________________________________

Also you can specify a particular module or project to run with the use of –projects or -pl

mvn clean package –pl RateLimiterCounterMapProviderImpl

That build just the indicated project. You can specifiy one or more projects separated by comma.

__________________________________________________

Another option could be, build my project and its dependencies: –also-make or -am

mvn clean package –pl RateLimiterCounterMapProviderImpl -am

That builds RateLimiterService, RateLimiterCounterProvider and RateLimiterCounterMapProviderImpl
__________________________________________________

Or build my project and its dependants: –also-make-dependents or -amd

mvn clean package -pl RateLimiterCounterMapProviderImpl -amd

That builds RateLimiterCounterMapProviderImpl and RateLimiterProviderImpl

__________________________________________________

  • An easy one, working in offline mode: –offline or -o
mvn clean package -o RateLimiterCounterMapProviderImpl

That will not try to download the artifacts from remote repos, you’re an isolated developer 🙂

__________________________________________________

Sometimes I use my own Nexus repo (see Continuous Integration in Local post), so I need to change some
configurations in my maven setting files, to be honest I use two files: –settings or -s

mvn -s ~/.m2/settingsnexus.xml clean package

__________________________________________________
If you are familiar with maven I’m pretty sure you’ve hit your head on the table more than once and you finally decided to manually delete the your .m2 repo. The –update-snapshots or -U option could helps in those annoying times

mvn -U clean package

This will force to download the artifacts despite mvn does not consider them obsoletes.
__________________________________________________

  • Dependencies and versions
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.5</version>
<scope>test</scope>
</dependency>

That is a common dependency section in a pom.xml file. But you can specify the version in different ways:
1.8.5 => “Soft” requirement on 1.8.5 as a recommendation
[1.8.5] => “Hard” requirement on 1.8.5
[1.8.1,1.8.5] => Any between 1.8.1 and 1.8.5
[1.8.1,1.8.5) => Any between 1.8.1 and 1.8.4.99….
[1.8.5,) => Any above or equals to 1.8.5
(,1.8.1],[1.8.5,)=> Any below or equals to 1.8.1 OR any above or equals 1.8.5.
__________________________________________________

  • What-if fails

By default maven works with –fail-fast or -ff option. That is the multi-module build stops as soon a module fails.

mvn clean package       -> -ff option by default

…the output:

[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] turmeric-ratelimiter-parent ....................... SUCCESS [2.177s]
[INFO] Rate Limiter :: Rate Limiter Service .............. SUCCESS [17.947s]
[INFO] Rate Limiter :: Rate Limiter Provider :: Interface  FAILURE [0.361s]
[INFO] Rate Limiter :: Rate Limiter CounterMap Provider :: Interface  SKIPPED
[INFO] Rate Limiter :: Error Library ..................... SKIPPED
[INFO] Rate Limiter :: Rate Limiter Service :: Consumer .. SKIPPED
[INFO] Rate Limiter :: Rate Limiter Service :: Impl ...... SKIPPED
[INFO] Rate Limiter :: Rate Limiter CounterMap Provider :: Impl  SKIPPED
[INFO] Rate Limiter :: Rate Limiter CounterCassandra Provider :: Impl  SKIPPED
[INFO] Rate Limiter :: Rate Limiter Provider :: Impl ..... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

__________________________________________________________
You can use the –fail-at-end or -fae option to continue the build despite some modules has failed.

mvn -fae clean package -Dmaven.test.skip=true   -> just to avoid my tests during the build process

…the output:

[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] turmeric-ratelimiter-parent ....................... SUCCESS [1.959s]
[INFO] Rate Limiter :: Rate Limiter Service .............. SUCCESS [29.078s]
[INFO] Rate Limiter :: Rate Limiter Provider :: Interface  FAILURE [0.309s]
[INFO] Rate Limiter :: Rate Limiter CounterMap Provider :: Interface  SUCCESS [13.085s]
[INFO] Rate Limiter :: Error Library ..................... SUCCESS [6.886s]
[INFO] Rate Limiter :: Rate Limiter Service :: Consumer .. SUCCESS [5.550s]
[INFO] Rate Limiter :: Rate Limiter Service :: Impl ...... SKIPPED
[INFO] Rate Limiter :: Rate Limiter CounterMap Provider :: Impl  SUCCESS [7.367s]
[INFO] Rate Limiter :: Rate Limiter CounterCassandra Provider :: Impl  SUCCESS [8.224s]
[INFO] Rate Limiter :: Rate Limiter Provider :: Impl ..... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

__________________________________________________________
…and finally the preferred tool for dishonest developers 🙂
–fail-never or -fn

mvn -fn clean package -Dmaven.test.skip=true   -> just to avoid my tests during the build process

…the output:

[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] turmeric-ratelimiter-parent ....................... SUCCESS [1.959s]
[INFO] Rate Limiter :: Rate Limiter Service .............. SUCCESS [29.078s]
[INFO] Rate Limiter :: Rate Limiter Provider :: Interface  FAILURE [0.309s]
[INFO] Rate Limiter :: Rate Limiter CounterMap Provider :: Interface  SUCCESS [13.085s]
[INFO] Rate Limiter :: Error Library ..................... SUCCESS [6.886s]
[INFO] Rate Limiter :: Rate Limiter Service :: Consumer .. SUCCESS [5.550s]
[INFO] Rate Limiter :: Rate Limiter Service :: Impl ...... SKIPPED
[INFO] Rate Limiter :: Rate Limiter CounterMap Provider :: Impl  SUCCESS [7.367s]
[INFO] Rate Limiter :: Rate Limiter CounterCassandra Provider :: Impl  SUCCESS [8.224s]
[INFO] Rate Limiter :: Rate Limiter Provider :: Impl ..... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO]  + Ignoring failures
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

draw your own conclusions based on the outputs and your needs….

Now enjoy your new free time with the use of these tips….

Advertisements

During last days I’ve suffered some Internet connection troubles, sometimes connections goes, other it turns very slow, similar at the 56kb modems in the early stages of internet. BTW, thanks to Movistar.

Those ones, can become (or not) in serious issues for an isolated developer or web page designer who could upload his advances once a day, or a week…but it turns critical for this developer who works together a multi-country team in an Open Source project.

For instance, in my daily labor I run maven builds, some days several of them…as you may know these maven builds look for and downloads huge quantities of files, artifacts, or dependencies…off course according your configurations, at least, I have to…….So, the need of an enhancement in this process arose me during these days. Does not take much time to decide one of the better and well-known tools that bring you the ability to manage the provisioning artifacts during development, mainly based on the its simply installation and configuration process. Welcome Nexus to my laptop. Nexus Open Source is available under the GNU Affero General Public License version 3 (AGPLv3): http://opensource.org/licenses/agpl-v3.html.

Is not the intention of this post to tell you what Nexus is or does, for that purpose just surf on the adequate and, for sure more complete;  nexus site. Instead of that, I wrote this lines to let you know how to get Nexus working on your computers.

So, firstly some key aspects about nexus:

  • Nexus is a Jetty unzip and run tool
  • You can manage your repositories from different approaches, in my case I set them as Proxy Repos: “When you proxy a remote repository with Nexus Open source you can control all aspects of the connection to a remote repository including security parameters, HTTP proxy settings. You can configure which mirrors Nexus will download artifacts from, and you can control how long Nexus will store artifacts and how it will expire artifacts which are no longer referenced by your build….”
  • Nexus provides a JavaService wrapper for Windows. Just in case u prefer MS W rather than any linux OS

… for a better feature description here you have a copy of the Sonatype Nexus Feature Matrix:

OS: Open Source version  –  Pro: Professional version

Then, fisrt of all, go to download page, choose the last one. In this case, Release 1.9.1.1. There you can choose it from a list of formats, war, tar, zip, etc…I download the nexus-oss-webapp-1.9.1.1-bundle.tar.gz

Decompress this file into a folder that you selected as nexus home. This package contains 2 folders inside: nexus-oss-webapp-1.9.1.1, the nexus distribution itself and the sonatype-work, empty for now, that is the folder where your local repo will resid. You can assign different /home/.. folders for each of them, or you can decompress it in just one. I choose that way, both, nexus-oss-webapp-1.9.1.1 and sonatype-work, under a /home/jose/.nexus/ folder.  In case you are damning the two-folder structure, just think in upgrading the nexus distribution in your environment, your local repo will remain intact!

Now, under your …./.nexus/nexus-oss-webapp-1.9.1.1/bin/jsw/ folder you can see several distribution folders. I use a linux over 64 bits, so I’m going to work with linux-x86-64. Be sure to run the appropriate according your OS.

Then, let’s create a linux link for nexus path:

ln -s .nexus/nexus-oss-webapp-1.9.1.1/bin/jsw/linux-x86-64/ nexus

You can run nexus as a service or just manually, but you need to remember to run it prior to start you development day. I preferred to set as service., and these were the steps:

  1. Copy either ${NEXUS_HOME}/bin/jsw/linux-ppc-64/nexus, ${NEXUS_HOME}/bin/jsw/linux-x86-32/nexus, or ${NEXUS_HOME}/bin/jsw/linux-x86-64/nexus to /etc/init.d/nexus.
  2. Make the /etc/init.d/nexus script executable – chmod 755 /etc/init.d/nexus
  3. Edit this script changing the following variables:
    • Change APP_NAME to “nexus”
    • Change APP_LONG_NAME to “Sonatype Nexus”
    • Add a variable NEXUS_HOME which points to your Nexus installation directory
    • Add a variable PLATFORM which contains either linux-x86-32, linux-x86-64, or linux-ppc-64
    • Change WRAPPER_CMD to ${NEXUS_HOME}/bin/jsw/${PLATFORM}/wrapper
    • Change WRAPPER_CONF to ${NEXUS_HOME}/bin/jsw/conf/wrapper.conf
    • Change PIDDIR to /var/run.
    • Add a JAVA_HOME variable which points to your local Java installation
    • Add a ${JAVA_HOME}/bin to the PATH

another useful setting is edit plexus.properties in ${NEXUS_HOME}/conf. You’ll see an element named webapp-context-path. Change this value from “/nexus” to “/”

cd /etc/init.d

update-rc.d nexus defaults

service nexus start

and finally…..

Starting Sonatype Nexus...

Sonatype Nexus will be running in your local and you can access to the admin console through you browser at. The defaults credentials are admin/admin123

So, once we have nexus already deployed we need to add some custom parameters and configuration in order to be useful during our maven deploys.

During these days I’m working with a project from eBayOpenSource community, so, let’s assign those repositories.

Basically for Turmeric we need to add two repositories:

Release repository:

and Snapshot Repository:

And, finally we need to update our .m2 settings file in order to run our maven build integrated with nexus. For a better HOW-TO explanation let me post my maven’s settings.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<settings>
<!-- If you want to deploy content (SNAPSHOT or RELEASE) to the
Nexus instance at http://ph-0146.eva.ebay.com/nexus/
You'll need an account, and then you can enable this section.

To get an account, create an INFRA issue requesting the account.
Include your svn userid in the request.
<servers>
<server>
<id>ebayopensource-repo</id>
<username>{your.username}</username>
<password>{your.password}</password>
</server>
</servers>
-->

<!-- Search Order for Maven Plugins not configured in a project -->
<pluginGroups>
<pluginGroup>com.ebayopensource.plugins.maven</pluginGroup>
<pluginGroup>com.ebay.ebox.plugins</pluginGroup>
<pluginGroup>org.mortbay.jetty</pluginGroup>
<pluginGroup>org.codehaus.mojo</pluginGroup>
<pluginGroup>org.sonatype.maven.plugins</pluginGroup>
</pluginGroups>

<profiles>
<profile>
<id>turmeric</id>

<pluginRepositories>
<pluginRepository>
<id>ebayopensource-releases</id>
<name>eBayOpenSource Release Repository</name>
<url>http://127.0.0.1:8081/nexus/content/repositories/ebayopensource/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>ebayopensource-snapshots</id>
<name>eBayOpenSource Snapshots Repository</name>
<url>http://127.0.0.1:8081/nexus/content/repositories/ebayopensource-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:30</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>

<repositories>
<repository>
<id>ebayopensource-releases</id>
<name>eBayOpenSource Release Repository</name>
<url>http://127.0.0.1:8081/nexus/content/repositories/ebayopensource/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>ebayopensource-snapshots</id>
<name>eBayOpenSource Snapshots Repository</name>
<url>http://127.0.0.1:8081/nexus/content/repositories/ebayopensource-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:30</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>turmeric</activeProfile>
</activeProfiles>

<mirrors>
<mirror>
<id>Nexus</id>
<mirrorOf>central,eviware</mirrorOf>
<name>Nexus Public Mirror.</name>
<url>http://127.0.0.1:8081/nexus/content/groups/public</url>

</mirrors>

</settings>

Take care about the repositories id that you set, ie: ebayopensource-snapshots. These need to be the same as you defined in Nexus repo.

And….that’s all!!! now let’s do a maven build.
mvn clean package

During this process maven should attempt to download all your artifacts from http://localhost:8081&#8230;.. Of course first time you run your build, your nexus repo is empty so, the consuming time will not be decreased, but, try with your second build.

NOTE: If after your first maven build, maven is still getting some artifacts from any other external repository just review your Nexus Repositories looking for a wrong setup or just for a missing custom repository.

now, run your builds and enjoy your new Free Time.