Saturday 23 December 2017

Using Ant build.xml for dependency management

Steps:
  1. Right click on the project in project explorer in Eclipse --> new 'Ant Files'. This will generate a template file (.xml and a .properties file). 
  2. Create a Ivy.xml file by right clicking on the project --> new 'Ivy File'.
  3. Now you have 3 files in your project structure. the contents of these files are as follows:
axix_bujava.xml

<?xml version="1.0"?>

<project xmlns:ivy="antlib:org.apache.ivy.ant" default="retrieve-dependencies" basedir=".">

 <echo message="pulling in property files"/>
 <property file="axis_bujava.properties"/>
 
 <echo message="calling the web services generation ant task: axis_bujava"/>
 
 <target name="retrieve-dependencies" description="Retrieve dependencies with Apache IVY">
     <ivy:retrieve type="jar" />
 </target>
 
</project>

ivy.xml
<?xml version="1.0" encoding="ISO-8859-1"?>

<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
    <info
        organisation="com.test"
        module="Test"
        status="integration">
 </info>
 <dependencies>
    <dependency org="commons-validator" name="commons-validator" rev="1.3.1"/>
    
    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
  <dependency org="commons-logging" name="commons-logging" rev="1.2"/>
    
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  <dependency org="org.springframework" name="spring-context" rev="5.0.2.RELEASE"/>
    
 </dependencies>
</ivy-module>


axis_bujava.properties
###############################################################################
# Copyright (c) 2008, 2014 IBM Corporation and others.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
#     IBM Corporation - initial API and implementation
###############################################################################
!-- ScenarioType REQUIRED. Differentiates from "client" scenario.
ScenarioType=service

!-- InitialSelection REQUIRED.  Workspace relative URI to the input WSDL or Java
InitialSelection=/dynamicWebProjectName/src/packagename/myService.java

!--Utility property values - use these to list valid values for the following properties:
!-- Service.RuntimeId, Service.ServerId 
ListRuntimes=true
ListServers=true
!-- Verbose will list informational messages as well as errors if set to true
Verbose=true

!--  Service.RuntimeId - ID of web service runtime 
Service.RuntimeId=org.eclipse.jst.ws.axis.creation.axisWebServiceRT
!-- Service.ServerId -  ID of target server 
Service.ServerId=org.eclipse.jst.server.tomcat.80
!-- Service.TypeId - Java Bean is default for both Top Down and Bottom Up
!-- Service.TypeId=Java Bean

!-- ServiceProjectName - !--type:String - name of Service project - this property can be used to give the service project a different name than the project containing the initial selection.  type: String    
!--ServiceProjectName=serviceProjectName
!-- ServiceEarProjectName - !--type:String - name of Service EAR project - this property can be used to give the service EAR project a different name than the EAR project already associated with the service project.  type: String
!--ServiceEarProjectName=serviceEarProjectName

!-- Mappings
!-- Top down or bottom up scenario. Workspace relative URI to property file containing mappings.
!-- Top Down: Mappings are used to override the default package names of the generated Java template classes.
!-- The content of the properties file must be of the format namespace=package  
!-- Bottom Up: Mappings are used to override the default namespace names in the generated WSDL.
!-- The content of the properties file must be of the format package = namespace 
!--Mappings=/testProj/mappings/mappings.txt

!-- JavaOutput
!-- Top down - String: the workspace relative URI for a Java source folder for the generated Java code - must be existing workspace source folder
!--JavaOutput=/projectName/JavaSource

!-- Methods
!-- Bottom up - space delimited string of method signatures (i.e. method1(parmType1) method2(parmType2)) to be exposed in output WSDL.  If no list is provided, all methods are exposed.
!-- Methods=

!-- Style
!-- Bottom up - String: one of: RPC, DOCUMENT, WRAPPED
!--Style=DOCUMENT

!-- Use
!-- Bottom up - String: one of: LITERAL, ENCODED 
!--Use=LITERAL

!--OverwriteFilesEnabled - set to true to overwrite files that already exist. type:boolean 
!-- note for non-interactive mode it is recommended that this be set to true
!--OverwriteFilesEnabled=true

!--CreateFoldersEnabled - set to true create folders necessary during file generation type:boolean
!-- note for non-interactive mode it is recommended that this be set to true
!--CreateFoldersEnabled=true

!--CheckoutFilesEnabled - set to true to check out files with no warning to the user type:boolean
!-- note for non-interactive mode it is recommended that this be set to true
!--CheckoutFilesEnabled=true


Now right-click on axix_bujava.xml --> Run As --> Ant Build

First Eclipse project using Apache Ivy

For installation of IvDE plugin in eclipse refer to this post.

In this post I'll discuss getting started with first project using Ivy.

Steps:
  • Create a new project in eclipse. (In the usual way)
  • Right click on the project in project explorer and then click on new --> other and search for 'ivy file'. Create a new Ivy file. You will get a template file.
Modify the contents of this file so that it looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?>

<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
    <info
        organisation="com.test"
        module="Test"
        status="integration">
 </info>
 <dependencies>
    <dependency org="commons-validator" name="commons-validator" rev="1.3.1"/>
 </dependencies>
</ivy-module>


  • This says that we have one dependency on "commons-validator". Search your dependencies on the maven repo and add it to your ivy file.
  • Once you have added all the dependencies, right click on the ivy.xml file and click add Ivy Library. This will add all the dependencies to your project transitively.
  • Create a Test class in com.test package in your project which has following contents:

package com.test;

import org.apache.commons.validator.GenericValidator;

public class Test {

 public static void main(String[] args) {
  if (GenericValidator.isEmail("lavishkothari004@gmail.com")) {
   System.out.println("the entered Email is valid.");
  } else {
   System.out.println("the entered email is not valid");
  }
 }

}


Run the project using Ctrl+F11.

What is Ivy? Installing Ivy on Eclipse

What is Ivy?
Apache Ivy is a dependency manager. Or to put it elaborately, Ivy is a dependency manager (basically an Ant extension) to manage external libraries in an agile manner during development.

Benefits of using Ivy:
  • Suppose you want to use some external Java library during development and you end up adding all the resources as external JARs. (In eclipse you can manually add external JARs to your project by right-clicking on the project in project explorer --> build path --> configure build path... --> go to libraries Tab and click on 'Add External JARs'). Note that before adding these external JARs you need to go to the painstaking task of downloading them.
    • And in the case when you decide to use some other version of existing external library, you again need to go and download and manually add the JARs. This is no doubt, a clumsy and error-prone task.
  • Suppose the JARs that you download have transitive dependencies, you will have to go and manually add all the libraries which are missing and repeat this process forever until all the dependencies are resolved transitively. Apache Ivy helps us in resolving transitive dependencies with no pain.
  • Suppose you want to add your project on GitHub and someone else clones the project for reference, he/she will not get all the external libraries that your project is dependent on and will end up adding all the dependencies. This is horrible. Ivy lets you specify the dependencies and downloads it for you automatically at a click. :)
 And this list goes on. See this for more details.

But I think these points are enough to make you feel trying your hands on Ivy.

Installing Ivy plugin on eclipse
This is very simple. Go to Help --> Install new Software --> click on Add button
Name: IvyDE
Click OK.

IvDE will install after this and you will be prompted to restart eclipse.