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.






Thursday, May 28, 2015

Spring MVC with Apache Maven


After a long long time. While you are learning some other stuffs, I am going to catch your eye with Spring framework. As I said in my first post, this is the time to move with Spring which is the one of most popular Java related frameworks. I don't use my time to give you any overview about Spring framework. This can be got using following rich links (Just google). In this post I'm going to create your first Maven based project using Spring Tool Suite. First of all you need to install Maven in your machine.

I'm not going to give you theory parts, because you can google it and find so many resources about these theories. Here I'm going to give you hands-on experience when you are developing. Once you open STS (Spring Tool Suite, I'm using 3.6.3.SR1 version), following interface will be appeared. You can download Spring Tool Suite from http://spring.io/tools/sts/all or you can change your existing Eclipse IDE into Spring tool suite using Eclipse Market Place. 







Before create a Maven project...


First of all you need to  rebuild Maven indexes. Maven consist of local and global repositories. So your first step should be rebuilding these two repositories. How to do it ?

Follow this path

Window -> Show view -> Other -> "type Maven" -> "select Maven Repositories"





Now you can see Maven Repository view.


Before rebuild indexes...



  • Delete existing .m2 folder, this folder can be found in C:\Users\{User name}





  • If you are using a proxy server, please check whether that your proxy allows you to download. If it is not downloading, you have to use other connection to download .
  • Right click on the Global Repository and select Full index enabled. 



Now you are ready to rebuild indexes.



  • Right click on the local repositories and click rebuild indexes.
  • Then right click on the global repositories and click rebuild indexes.
This gets a lot of time to rebuild indexes. Please wait until if finished. If it doesn't give an error, you successfully rebuilt Maven indexes.


You are ready to create your first Maven project


  • Right click on Package Explorer 
  • New -> Other -> "Type maven in text filed" -> Select Maven Project -> Next

Skip archetype selection




Enter a Group Id, Artifact Id and select "war" from Packaging. (You need to select war, because you are going to create a web project)



Your project will appear like this.





STOP !


You need to configure few things first.


  • Change Java version of the project
  • Change Dynamic Web Module version of the project, How to do this ?



Configure the project



  • Right click on the project (Or Alt + Enter) to go to project properties.
  • Select Project Facets 
  • Do the changes (I use Java version 1.8 and Dynamic web module version 3.0)




Then right click on the project -> Maven -> Update project or (Alt + F5)

Now you are ready.


web.xml file


web.xml file is a Deployment Descriptor for the project (Google for more info). In STS you can generate it simply.

Right click on the project -> Java EE Tools -> Generate Deployment Descriptor Stub




Then your web.xml file will appear like this.




 Please check the version of web.xml file. Otherwise your project won't work.



If the version is still 2.5, then delete web.xml file and update the project (F5) and update Maven (Alt + F5). Then again generate web.xml file


Create first JSP page


  • Right click on the project -> New ->Other
  • search for jsp
  • Select jsp and give the file name as "index.jsp"
  • Click finish



You can see there is red marked things looks like errors. Forgot it for a moment.

Just type something in index.jsp file to identify the sample project is working or not. Then right click on the project,

Run As -> Run on server and select your server. (I'm using Apache tomcat 7.0 or 8.0 server) I assume that you know how to configure a server in a IDE like Eclipse. 



When it running, you will able to see your message on your browser that you have put on index.jsp file. If you can see this, you are successfully created simple Maven project. In my next post lets add Spring dependencies to our project.