Friday, September 13, 2013

Migrating from Ant to Maven

Sample steps to migrate a java webapp from using Ant to Maven as the build tool.

Install maven


Learn about maven
Look up some maven tutorials and documentation e.g.


Reorganise source files
  • Create a skeleton maven directory structure for your code using an appropriate maven archetype command e.g run commands similar to the following on the command line.

For a war
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.mycompany -DartifactId=mywebapp


For a jar
mvn archetype:create -DgroupId=com.mycompany -DartifactId=myjar


  • Copy java source files directories from the ant project to the appropriate maven structure location e.g. C:\maven-migration\mywar\src\main\java\
  • Copy non-java files that are in the classes directory to the appropriate maven structure location e.g. C:\maven-migration\mywar\src\main\resources
  • Copy other web resources (everything else that is not within the classes directory) to the appropriate maven structure location e.g. C:\maven-migration\mywar\src\main\webapp
  • Delete the WEB-INF\lib directory under the maven webapp directory, if that was the location of libraries.


Create dependencies for libraries
  • From the ant project’s WEB-INF\lib directory, go through the libraries one at a time, determining if the library is used directly by your code. If so, add a dependency for the library to your maven project’s pom file. Use a maven central search website to determine dependency details like artefact id etc. e.g. http://www.maven-repository.com/. Mark or identify these as resolved e.g. on a piece of paper listing all the libraries.
  • Next, go through the remaining libraries and figure out which libraries are a dependency of one of the libraries already added. You may need to review the poms of the added libraries. You can see the library poms from the maven central search website. An IDE can also help to identify indirect dependencies e.g. Eclipse has a Dependency Hierarchy feature that shows these. Mark the indirect dependencies you’ve identified as resolved.
  • From the remaining libraries, identify those which are not used by your code directly but are required at runtime or for testing. Add these dependencies to your maven project pom with a runtime or test scope as appropriate. Mark these dependencies and the indirect dependencies that they bring, as resolved.
  • You’ll likely be left with non-maven libraries that aren’t built using maven or which aren’t available on a public maven repository. You’ll need to download packages for these libraries and see if they contain an indication of the libraries that they depend on. You’ll then need to create a pom for these libraries and deploy them to some maven repository e.g. an in-project one, see https://devcenter.heroku.com/articles/local-maven-dependencies
  • Once you have all the libraries matched, refine the dependencies in your pom e.g. to define exclusions for indirect dependencies that you don’t use. You can also override versions of libraries included.
  • Define other aspects of the pom similar to what is found in your ant project’s build.xml file e.g. configuration of the maven-compiler-plugin to generate java 1.6 compatible classes







Thursday, September 12, 2013

Installing Sonatype Nexus Open Source

Sonatype Nexus Open Source (Nexus OSS) is a repository manager for maven repositories.

Prerequisites
Maven

Versions used
Nexus OSS 2.0.5-04
Maven 3.0.4

Steps
  • Download the nexus oss zip package from http://sonatype.org/nexus
  • Extract the zip file to a folder created for nexus e.g. C:\nexus
  • Installation is complete

Running nexus
  • Open a command prompt window and navigate to the the NEXUS_HOME\bin directory e.g. C:\nexus\nexus-2.0.5-04\bin\
  • Run the command nexus.bat console
  • Open a browser and navigate to localhost:8081/nexus. The nexus interface should be displayed.
  • To install nexus as a service, run the command nexus.bat install
  • The nexus service name is nexus-webapp. Now you can start/stop nexus like any other service e.g. using the command net start nexus-webapp. To uninstall the service, run the command nexus.bat uninstall
  • On the nexus home page, click on the Log In link. The default login username/password is admin/admin123. There is also a default deployment user with username/password deployment/deployment123. You can change any of these passwords by expanding the Security section on the left hand pane, selecting the Users option, right-click on the desired user and selecting the Set Password context menu.

Configuring maven to use nexus
  • You can set maven to look for artefacts from nexus instead of directly from the internet e.g. the maven central repository. A Nexus proxy repository acts as a local cache for a remote repository.
  • Create or edit the USER_HOME_DIR\.m2\settings.xml file e.g. C:\Users\ANDY\.m2\settings.xml, to configure nexus as the mirror for all repositories e.g.

  
    
      
      nexus
      external:*
      http://localhost:8081/nexus/content/groups/public
    
  
  
    
      nexus 
      
      
      
        
          central
          http://central
          true
          true
        
      
     
        
          central
          http://central
          true
          true
        
      
    
  
  
    
    nexus
  

Installing Maven 3.0.4

Apache Maven is an open source build automation tool for java projects.

Prerequisites
JDK

Versions used
Maven 3.0.4
JDK 1.6.0_39

Steps

  • Download the maven binary zip package from http://maven.apache.org
  • Extract the zip file to some folder e.g. C:\. A folder structure like C:\apache-maven-3.0.4 will be created
  • Add the environment variable MAVEN_HOME with the value of the maven home directory e.g. C:\apache-maven-3.0.4\
  • Add the %MAVEN_HOME%\bin location to the PATH environment variable
  • The setup is complete. Open a command prompt window and type the command mvn -version to confirm.
  • The local maven repository is located in <USER_HOME_DIR>\.m2\repository e.g. C:\Users\TOM\.m2\repository
  • Note that maven requires internet access to download dependencies and may do so from time to time depending on the command and project given. Once a dependency is downloaded once, it is put in the local repository and will not be downloaded again.