Thursday, July 16, 2015

Create and Deploy Web project using Maven

1. Download below jar/zip's
a. apache-maven-3.3.3-bin
b. apache-tomcat-8.0.23

2. Set environment variables
a. CATALINA_HOME = C:\Users\raylabs\Downloads\apache-tomcat-8.0.23\apache-tomcat-8.0.23
b.M2_HOME = C:\Users\raylabs\Downloads\apache-maven-3.3.3-bin\apache-maven-3.3.3
c : PATH =C:\Program Files\Java\jdk1.8.0_45\bin;C:\Users\raylabs\Downloads\apache-maven-3.3.3-bin\apache-maven-3.3.3\bin;%CATALINA_HOME%\bin
d. MAVEN_HOME =C:\Users\raylabs\Downloads\apache-maven-3.3.3-bin\apache-maven-3.3.3


3. Create Workspace folder :
D:\projects\jsf

4. Open command prompt and enter the command :  mvn -version

[RAYLABS.D-4654646] ➤ mvn -version
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T17:27:37+05:30)
Maven home: C:\Users\raylabs\Downloads\apache-maven-3.3.3-bin\apache-maven-3.3.3
Java version: 1.8.0_45, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_45\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "dos"


5. Create a project using maven script command :

mvn archetype:generate -DgroupId=com.raylabs.maven -DartifactId=HelloMaven -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false


6. Create eclipse project with the same : 

a. Enter the project folder (HelloMaven)
b. Run the command : mvn eclipse:eclipse

Eclipse .project will be created and can be imported


7. Add user to tomcat-user file 
C:\Users\raylabs\Downloads\apache-tomcat-8.0.23\apache-tomcat-8.0.23\conf\tomcat-users.xml

<role rolename="manager"/>
<role rolename="admin"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="admin" password="admin" roles="admin,manager,manager-gui,manager-script"/>


8.  Add configuration to maven settings.xml 

a. open C:\Users\raylabs\Downloads\apache-maven-3.3.3-bin\apache-maven-3.3.3\conf\settings.xml
 <servers>
<server>
   <id>TomcatServer</id>
   <username>admin</username>
   <password>admin</password>
</server>
  </servers>

9. Import the project to eclipse 



10. Add configuration to pom.xml of the project

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.raylabs.maven</groupId>
  <artifactId>HelloMaven</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>HelloMaven Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>HelloMaven</finalName>
    <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<server>TomcatServer</server>
<path>/HelloMaven</path>
</configuration>
</plugin>
</plugins>
  </build>
</project>

11. Start the tomcat server : startup.bat in bin folder

12. Run the command to deploy the application to tomcat
mvn clean package redeploy : Every time you make changes
mvn tomcat:deploy : First time
mvn tomcat:redeploy : After changes
mvn tomcat:undeploy : Remove the project.