Wednesday, April 25, 2012

Building together JAR and WAR in maven

Creating JAR file in maven



To create first project in maven, I used the maven "archetype" mechanism. In maven, archetype is a template of a project.


To create a quick start maven project, execute the following command in Command prompt.
mvn archetype:generate -DgroupId=org.yarlithub.yschool -DartifactId=yschool-mini-mayooapp


Here,
groupId :This is an element that indicates the unique identifier of the Organization that created the project. groudId should follow the package name (reversed DNS of your website)
artifactId :This also is an element that indicates the unique base name of the project.
-D: This command is simply setting Java system properties, this use passing configuration information to Maven.


When executed the above command, a directory named "yschool-mini-mayooapp" has been created for this project. After that the project should be compiled.


Change the current directory into yschool-mini-mayooapp directory using following command in windows.
> cd yschool-mini-mayooapp


If you want to add this JAR into another WAR file you can edit this App.java file
For example: If you want to print the current time use the following code. 
public class App{  
   public static String myDate(){
       return new java.util.Date().toString();
    }
}
Then the application sources are compiled using following commands
>yschool-mini-mayooapp>mvn compile
By executing the mvn install command, Maven finishes by installing the JAR file into the local repository, making this JAR file available to other projects.
>yschool-mini-mayooapp>mvn install


Then to see the out of this project execute the following commands
>yschool-mini-mayooapp>java -cp target/yschool-mini-mayooapp-1.0-SNAPSHOT.jar org.yarlithub.yschool.App




Creating WAR file in maven
Creating a web application in maven is straight forward like above mentioned, but different archetypeArtifactId. Back into the base directory using following commands in windows
>yschool-mini-mayooapp>cd..


After that execute the following command then a web application will be created.
>mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp  -DgroupId=org.yarlithub.yschool  -DartifactId=yschool-mini-mayoowebapp


After creating web application, this project must be compiled. Before compilation, If you want add JAR file into WAR file you should add a dependency into the WAR pom.xml


<dependency>
          <groupId> org.yarlithub.yschool</groupId>
          <artifactId> yschool-mini-mayooapp</artifactId>
          <version>1.0-SNAPSHOT</version>  
</dependency>


Then Edit the index.jsp file in web application



<html>
<body>
<h2>Hello Jaffna!</h2>
<h3>Yarl IT Hub<h3>
<p>Is a community who live the shared dream of making Yarl the next Sillicon Valley. We are a not for profit initiative and 100% apolitical. An initiative by the community for the community!</p>
<h3>Now the Time is: <%= org.yarlithub.yschool.App.myDate() %>
</body>
</html>


To compile this project, go to project folder type "yschool-mini-mayoowebapp" using "cd yschool-mini-mayoowebapp" command, and then type the following commands.
>mvn compile


 Simply executing the command mvn package creates our WAR file
>mvn package


To run this application we simply add the jetty plug-in to pom.xml for this web application. This following code will be added to the pom.xml

<build>
    <finalName>yschool-mini-mayoowebapp</finalName>
          <plugins>
          <plugin>
          <groupId>org.mortbay.jetty</groupId>
          <artifactId>maven-jetty-plugin</artifactId>
          <version>6.1.10</version>
          <!--<configuration>-->
          <!--<scanIntervalSeconds>10</scanIntervalSeconds>-->
          <!--<stopKey>stop</stopKey>-->
          <!--<stopPort>9999</stopPort>-->
          <!--</configuration>-->
          <executions>
          <execution>
                   <id>start-jetty</id>
                   <phase>deploy</phase>
                   <goals>
                   <goal>run</goal>
                   </goals>
                   <configuration>
                   <scanIntervalSeconds>0</scanIntervalSeconds>
                   <daemon>true</daemon>
                   </configuration>
          </execution>
          </executions>
          </plugin>
          </plugins>
  </build>



We can now run our web application by simply typing the command as shown below.
>mvn install jetty:run


We can now open our web browser to localhost:8080/ yschool-mini-mayoowebapp to see our web application.







Monday, April 23, 2012

First Maven Project

Configuration steps for create simple maven project


Download and install  jdk-7u3-windows-i586  and setup the class path

http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u3-download-1501626.html


Download and install apache-maven-3.0.4 and setup the path


Open command line and type "mvn --version" to verify the successful installation


If installation is correct the following message will be shown

Apache Maven 3.0.4 (r1232337; 2012-01-17 00:44:56-0800)
Maven home: C:\Program Files\Apache Software Foundation\apache-maven-3.0.4
Java version: 1.7.0_03, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_03\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "x86", family: "windows"




Setup proxy sever to create a maven project

Open "C:\Program Files\Apache Software Foundation\apache-maven-3.0.4\conf " folder 

Edit the settings.xml file using the following code with appropriate proxy details

<proxies>    
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxy_netuser</username>
      <password> proxy_netuser </password>
      <host>proxy.host.net</host>
      <port>80</port>
     <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>   
  </proxies>


git hub proxy setup


 git config --global http.proxy http://proxyuser:proxypwd@proxy.server.com:8080


Java web


I had an opportunity to enter java web programming through the Yarlithub. That is first entrance to java web. But, I didn't get a chance like this before it. So, i would like to thank to Yarlithub. Before that i heard about java web technologies through software industries visit to Colombo, but it was not clear me.

One day I had a chance to participate to opening ceremony of the Yarlithub at the IIS City Campus. There I heard about the yShool open source project.

The ySchool project is a simple open source School Management System, especially for Sri Lankan Schools that are located in rural areas.

So, I wish to join to the develping team of ySchool project.