Tuesday, July 7, 2015

Add Spring dependencies to a Maven project




In my previous post I described how to begin to create Spring MVC web application using Maven on Spring Tool Suite. This post is also related to previous post and here I'm going to move with add Spring nature to the project that I have created in last post. This is very simple.


First of all you need to add maven-compiler-plugin into your pom.xml file. 
  • Go to the pom.xml
  • Press Ctrl + Space before the  </project> tag.
  • Then select Insert plugin from the pop up menu.
  • Then type maven compiler plugin in the search field.
  • Then it will load available plugin list and versions, then you can choose the relevant plugin. 




Once you add this plugin you need to do following changes to the code. This is the final look of the maven compiler plugin.

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.3</version>
      <configuration>
        <target>1.8</target>
        <source>1.8</source>
      </configuration>
    </plugin>
  </plugins>
</build>


I have added additional configuration code with target and source. My Java version related to this project is 1.8 (Look at the previous post to change versions)


Then you need to add Spring dependencies to the pom.xml You can easily get all Spring dependencies from here, spring dependencies. There you can see a set of dependencies. You need to add them within  <dependencies>   </dependencies> tags. In the top of the copied code, you will able to see a code (<properties>) which mentioning org.springframework.version. This version depend on your choice. You can check the latest, stable version from here, http://projects.spring.io/spring-framework/ I have selected 4.1.6 RELEASE 


<properties> tag is very useful when you want to change the version of the spring framework. Because if you don't have the property tag, you will have to change each and every tag separately. But now it can be changed from one line.


Then I have added other few dependencies to avoid following error that you have seen in jsp pages in previous post. superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path 

Finally my pom.xml is looks like this.



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.app.test</groupId>
  <artifactId>Spring-test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <properties>
   <org.springframework.version>4.1.6.RELEASE</org.springframework.version>
  </properties>
  
  <dependencies>
  
 <!-- Servlet -->
 <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.1.0</version>
   <scope> provided</scope >
 </dependency>
   
 <dependency>
   <groupId>javax.servlet.jsp</groupId>
   <artifactId>javax.servlet.jsp-api</artifactId>
   <version> 2.2.1</version >
    <scope> provided</scope >
 </dependency>
   
 <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
 </dependency>
 
 <!-- Spring dependencies -->
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>${org.springframework.version}</version>
 </dependency>
 
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-expression</artifactId>
   <version>${org.springframework.version}</version>
 </dependency>
 
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-beans</artifactId>
   <version>${org.springframework.version}</version>
 </dependency>
  
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-aop</artifactId>
   <version>${org.springframework.version}</version>
 </dependency>
  
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>${org.springframework.version}</version>
 </dependency>
  
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
   <version>${org.springframework.version}</version>
 </dependency>
  
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-tx</artifactId>
   <version>${org.springframework.version}</version>
 </dependency>
  
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</artifactId>
   <version>${org.springframework.version}</version>
 </dependency>
  
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-orm</artifactId>
   <version>${org.springframework.version}</version>
 </dependency>
  
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-oxm</artifactId>
   <version>${org.springframework.version}</version>
 </dependency>
  
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <version>${org.springframework.version}</version>
 </dependency>
  
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>${org.springframework.version}</version>
 </dependency>
  
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc-portlet</artifactId>
   <version>${org.springframework.version}</version>
 </dependency>
  
 <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-test</artifactId>
   <version>${org.springframework.version}</version>
   <scope>test</scope>
 </dependency>  
    
   </dependencies>
  
  <build>
   <plugins>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <version>3.3</version>
     <configuration>
      <target>1.8</target>
      <source>1.8</source>
     </configuration>
    </plugin>
   </plugins>
  </build>

</project>

Now you ready to update the project. Right click on your project, go to Run As -> Maven build... Then type clean install in Goals. Now run the project. (Or you can first clean and then run the project using other options on "Run As") If you got any errors, right click on the project go to Maven -> Update project or Alt + F5. Then run again it. If you are getting any further errors, it may about your server. Please check your server's ports whether they are similar to existing server ports. You can change them on double click on the server.


Now you can run the project, go to Run As and select Run on server. But in this point you cannot test Spring. In next post I'm going to create simple Spring web application.