Friday, February 17, 2012

Spring Beans and Dependency Injection - Part 2

Getting spring.jar.

In your ivy.xml, add a dependency on spring.jar, so you ivy.xml now looks like this:

 <ivy-module version="2.0">  
<info organisation="org.confucius" module="helloworld"/>
<dependencies>
<dependency org="org.springframework" name="spring" rev="2.5.6"/>
<dependency org="struts" name="struts" rev="1.2.9"/>
<dependency org="org.apache.struts" name="struts-taglib" rev="1.3.10"/>
<dependency org="commons-collections" name="commons-collections" rev="20040616"/>
<dependency org="commons-digester" name="commons-digester" rev="2.1"/>
<dependency org="commons-beanutils" name="commons-beanutils" rev="20030211.134440"/>
<dependency org="commons-logging" name="commons-logging" rev="1.1.1"/>
<dependency org="org.apache.httpcomponents" name="httpcore" rev="4.2-alpha2"/>
<dependency org="org.apache.httpcomponents" name="httpclient" rev="4.2-alpha1"/>
<dependency org="org.apache.commons" name="commons-exec" rev="1.1"/>
<dependency org="com.google.guava" name="guava" rev="r09"/>
<dependency org="org.seleniumhq.selenium" name="selenium-api" rev="2.17.0"/>
<dependency org="org.seleniumhq.selenium" name="selenium-remote-driver" rev="2.17.0"/>
<dependency org="org.seleniumhq.selenium" name="selenium-firefox-driver" rev="2.17.0"/>
<dependency org="org.seleniumhq.selenium" name="selenium-java" rev="2.16.1"/>
<dependency org="junit" name="junit" rev="4.10"/>
<dependency org="org.json" name="json" rev="20090211"/>
<dependency org="javax.servlet" name="servlet-api" rev="2.5"/>
<dependency org="javax.servlet" name="jsp-api" rev="2.0"/>
<dependency org="jstl" name="jstl" rev="1.2"/>
<dependency org="log4j" name="log4j" rev="1.2.16"/>
</dependencies>
</ivy-module>


Run the Ant 'resolve'task to download spring.jar to your /lib directory.

In Eclipse, go to Project-->Properties-->Java Build Path-->Libraries-->Add Jars

and add the spring.jar to your Eclipse Classpath.

We also need to update build.xml to add spring.jar to Ant's classpath.

So your build.xml will now look like this:
 <project name="HelloWorld" xmlns:ivy="antlib:org.apache.ivy.ant" >  

<target name="resolve" description="--> retrieve dependencies with ivy">
<ivy:retrieve />
</target>

<target name="init" depends="resolve">
<mkdir dir="classes"/>
<mkdir dir="target"/>
</target>

<path id="build.classpath">
<pathelement location="lib/spring-2.5.6.jar"/>
<pathelement location="lib/commons-collections-20040616.jar"/>
<pathelement location="lib/commons-digester-2.1.jar"/>
<pathelement location="lib/commons-beanutils-20030211.134440.jar"/>
<pathelement location="lib/struts-taglib-1.3.10.jar"/>
<pathelement location="lib/struts-1.2.9.jar"/>
<pathelement location="lib/commons-logging-1.1.1.jar"/>
<pathelement location="lib/httpcore-4.2-alpha2.jar"/>
<pathelement location="lib/httpclient-4.2-alpha1.jar"/>
<pathelement location="lib/commons-exec-1.1.jar"/>
<pathelement location="lib/guava-r09.jar"/>
<pathelement location="lib/selenium-api-2.17.0.jar"/>
<pathelement location="lib/selenium-firefox-driver-2.17.0.jar"/>
<pathelement location="lib/selenium-java-2.16.1.jar"/>
<pathelement location="lib/selenium-remote-driver-2.17.0.jar"/>
<pathelement location="lib/junit-4.10.jar"/>
<pathelement location="lib/json-20090211.jar"/>
<pathelement location="lib/servlet-api-2.5.jar"/>
<pathelement location="lib/jsp-api-2.0.jar"/>
<pathelement location="lib/log4j-1.2.16.jar"/>
</path>

<target name="compile" depends="init">
<javac srcdir="." destdir="classes">
<classpath refid="build.classpath"/>
</javac>
</target>

<path id="test.classpath">
<pathelement location="classes"/>
<pathelement location="lib/junit-4.10.jar"/>
<pathelement location="lib/selenium-api-2.17.0.jar"/>
<pathelement location="lib/selenium-firefox-driver-2.17.0.jar"/>
<pathelement location="lib/selenium-java-2.16.1.jar"/>
<pathelement location="lib/selenium-remote-driver-2.17.0.jar"/>
<pathelement location="lib/guava-r09.jar"/>
<pathelement location="lib/commons-exec-1.1.jar"/>
<pathelement location="lib/httpclient-4.2-alpha1.jar"/>
<pathelement location="lib/httpcore-4.2-alpha2.jar"/>
<pathelement location="lib/commons-logging-1.1.1.jar"/>
<pathelement location="lib/struts-1.2.9.jar"/>
<pathelement location="lib/struts-taglib-1.3.10.jar"/>
<pathelement location="lib/commons-beanutils-20030211.134440.jar"/>
<pathelement location="lib/commons-digester-2.1.jar"/>
<pathelement location="lib/commons-collections-20040616.jar"/>
<pathelement location="lib/spring-2.5.6.jar"/>
</path>

<target name="test" depends="compile" >
<junit failureproperty="junit.failure">
<test name="org.confucius.TestCalculator"/>
<classpath refid="test.classpath"/>
<formatter type="plain" usefile="false" />
</junit>
<fail if="junit.failure" message="Unit test(s) failed. See reports!"/>
</target>

<target name="dist" depends="test">
<war destfile="target/HelloWorld.war" webxml="web.xml">
<classes dir="classes"/>
<lib dir="lib">
<exclude name="jsp-api*.jar"/>
<exclude name="servlet-api*.jar"/>
</lib>
<fileset dir="web-content"/>
<webinf dir="WEB-INF"/>
</war>
<echo>Build executed at ${TIME_NOW}</echo>
</target>

<tstamp>
<format property="TIME_NOW" pattern="hh:mm:ss aa MM/dd/yyyy"/>
</tstamp>

</project>

No comments: