1. Trang chủ
  2. » Công Nghệ Thông Tin

Java, Eclipse, Maven & JSF tutorial pot

60 440 1
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Java, Eclipse, Maven & JSF tutorial pot
Tác giả K. Arulkumaran, A. Sivayini
Trường học Lulu.com
Chuyên ngành Computer Science
Thể loại tutorial
Định dạng
Số trang 60
Dung lượng 3,39 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Java, Eclipse, Maven & JSF tutorial Maven 2 is a powerful tool that promotes convention over configuration and you need to integrate it into one of the popular integrated development e

Trang 1

The author has made every effort in the preparation of this book to ensure the accuracy of the information However, information in this book is sold without warranty either expressed or implied The author will not be held liable for any damages caused or alleged to be caused either directly or indirectly by this book

Java, Eclipse, Maven & JSF tutorial

Maven 2 is a powerful tool that promotes convention over configuration and you need to

integrate it into one of the popular integrated development environments (IDEs) called

eclipse to make your work easier, thus increasing your productivity and project quality This

tutorial provides an example of how to make Maven and Eclipse collaborate Also covers the

popular JSF Web framework

Trang 2

Table Of Contents

Notations 3

Tutorial 1 – Java, Maven and Eclipse 4

Tutorial 2 – Java Web, Maven and Eclipse 16

Tutorial 3 – JSF, Maven and Eclipse 28

Appendix 58

Trang 3

Notations Command prompt:

Eclipse:

File Explorer or Windows Explorer:

Internet Explorer:

Trang 4

Tutorial 1 – Java, Maven and Eclipse

This tutorial will guide you through building a simple Java application from scratch using popular tools like eclipse and maven This tutorial will be handy for those who are new to maven/eclipse/Java

Install the following programs In this tutorial I have installed them under c:/java

ƒ Java 1.5 (JDK1.5 & JRE1.5)

site: http://java.sun.com/javase/downloads/index_jdk5.jsp Also set the environment

properties JAVA_HOME (e.g C:\java\jdk1.5.0) and add to the path (e.g

%JAVA_HOME%\bin) If installed correctly you should be able to test it by opening a

command prompt and typing

C:\>java –version

Results in:

java version "1.5.0_11"

Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_11-b03)

Java HotSpot(TM) Client VM (build 1.5.0_11-b03, mixed mode, sharing)

ƒ Maven 2.0.7 Site: http://maven.apache.org/download.html Set the environment variables M2_HOME (e.g C:\java\maven-2.0.7) and add to the path (e.g

%M2_HOME%\bin) If installed correctly you should be able to test it by opening a

command prompt and typing:

C:\>mvn version

Results in:

Maven version: 2.0.7

Java version: 1.5.0_11

OS name: "windows xp" version: "5.1" arch: "x86"

Open the “settings.xml” file under C:\java\maven-2.0.7\conf folder and set your

local repository as follows:

<localRepository>C:/java/.m2/repository</localRepository>

Also if your internet access is through a proxy server then configure your proxy server

in “settings.xml” for example:

ƒ Eclipse 3.3.0 (Europa) Site: http://www.eclipse.org/downloads/

So far we have installed the programs under c:/java Now we need to create a folder for

our tutorials as c:/tutorials Also create an eclipse workspace

Trang 5

“C:\java\eclipse-tutorial-workspace” where metadata get stored It is the best practice to separate IDE specific (i.e

C:\java\eclipse-tutorial-workspace) files from the projects (i.e c:/tutorials) specific files

C:/java folder

C:/tutorials folder

Trang 6

ƒ Now let’s have maven into play One of the powers of maven is its principle of “convention over configuration” You can run the following command to create a maven project structure

C:\tutorials>mvn archetype:create -DgroupId=com.mytutorial -DartifactId=simple

The above command results in some directories & files created under c:/tutorials E.g

C:\tutorials\simple Æ project called “simple”

C:\tutorials\simple\src Æ “src” folder under which you have “main” & “test” etc C:\tutorials\simple\pom.xml Æ the basic maven Project Object Model file

Important (optional step): The plug-ins attached to lifecycle phases should be downloaded and stored automatically (i.e without having to declare them in pom.xml)exactly at the moment

they are needed For example, if you call mvn compile from the command line The plugins that

you call from the command line are downloaded and stored in the local repository only when you call them (i.e exactly when you type the command in the command line) even if they are

configured in your pom.xml You can also explicitly define the plug-ins and their version numbers

in your pom.xml file as shown below in bold directly under the root element (i.e <project>)

Note: Maven is a plug-in execution framework You can look for available plug-ins at

http://maven.apache.org/plugins/ You can add this plug-ins as required So if you have any issue(s) in automatically downloading (e.g due to metadata files getting corrupted) or you want to override any plug-in configuration then use the above mentioned optional-step Refer appendix for sample pom.xml with other plugins added

Note: You need to have an internet connection so that maven can download the plug-ins from

its central repository at http://repo1.maven.org/ (especially http://repo1.maven.org/maven2/ ) The above mentioned plug-ins are based on their groupId under

http://repo1.maven.org/maven2/org/apache/maven/plugins/

Note: You can find the documentation on plug-ins at mave2 home site

http://maven.apache.org/plugins/index.html

Trang 7

The pom.xml file with plug-in details (plug-in-details are optional)

ƒ Open up eclipse with workspace: C:\java\eclipse-tutorial-workspace Close the welcome

screen and you should get an empty workbench

Trang 8

ƒ Eclipse needs to know the path to the local maven repository Therefore the classpath variable

M2_REPO has to be set Exit out of eclipse and execute the following command in a

command prompt The following command will be making use of the maven-eclipse-plugin C:\tutorials>mvn -Declipse.workspace=C:\java\eclipse-tutorial-workspace eclipse:add-

maven-repo

Results in:

[INFO] Scanning for projects

[INFO] Searching repository for plugin with prefix: 'eclipse'

[INFO] -

[INFO] Building Maven Default Project

[INFO] task-segment: [eclipse:add-maven-repo] (aggregator-style)

[INFO] Total time: 1 second

[INFO] Finished at: Wed Aug 01 13:38:15 EST 2007

[INFO] Final Memory: 3M/6M

[INFO] -

After executing this command, you open up your eclipse and check if this variable

M2_REPO has been added Important: If you had eclipse open while executing the above mvn command you would not see this class path variable M2_REPO (e.g

C:/java/.m2/repository) In eclipse under Window Æ Preferences

Trang 9

Note: Alternatively you can manually add this directly into eclipse without having to

run the mvn command shown above

ƒ If you have a simple java project which is made up of only one module, using eclipse is very

simple Now let us put the eclipse plug-in to the test of generating eclipse metadata (i.e

.project, classpath files) To generate the eclipse metadata files from your pom.xml you

execute the following command:

C:\tutorials> cd simple

C:\tutorials\simple>mvn eclipse:eclipse

After running this command if you check your local maven repository folder

C:\java\.m2\repository should have some dependency jars downloaded from the maven

repository site http://repo1.maven.org/maven2 and installed into your local repository

C:\java\.m2\repository Also under your project C:\tutorials\simple, you should have some

eclipse related files created like project and classpath

Note: You can find your downloaded plug-ins in your local repository

“C:/java/.m2/repository” which you set earlier in your settings.xml file under

%M2_HOME%\conf (i.e C:\java\maven-2.0.7\conf)

ƒ Now you get back to your eclipse and import this “simple” project as shown below File Æ

Import

Trang 10

Click “Next”

Click “Finish” Now you should have the project in eclipse

Trang 11

The “mvn eclipse:eclipse” command also has automatically added build dependencies into

your eclipse as shown below by using the M2_REPO class variable, which we set earlier

using “mvn -Declipse.workspace=C:\java\eclipse-tutorial-workspace

eclipse:add-maven-repo” command Isn’t that cool You can add any additional dependencies into your

“pom.xml” file and then run “mvn eclipse:clean eclipse:eclipse” command to get all your

eclipse build path dependencies set automatically This also means that you can add your

.project & classpath files under C:\tutorials\simple to your source control ignore list and all

you need is your pom.xml file to rebuild (mvn eclipse:clean eclipse:eclipse) it

Trang 12

Also note the source and target folders as shown below:

ƒ Open the “App.java” file under “src/main/java” which just prints “Hello World” as shown

below Also note the “AppTest.java” under “src/test/java” for JUnit testing the “App.java”

ƒ You can run this file inside eclipse by right clicking on “App.java” and selecting Run As Æ

Java Application You should see “Hello World” printed at the console in bottom right hand

corner

Trang 13

The “App.java” will run and print the results in the “Console” window as shown below

Also note that your class files are generated under “C:\tutorials\simple\target”in an explorer window Now delete all the files under “C:\tutorials\simple\target” for the next step

ƒ Now, let’s build it as a jar using maven and try to run it outside eclipse (i.e in a command

prompt)

C:\tutorials\simple>mvn clean package

Trang 14

Now you can see the packaged “simple-1.0-SNAPSHOT.jar” jar file under

“C:\tutorials\simple\target”, in addition to all the class files The jar file was built based on

your pom.xml file Now you can run the java application in the command line as follows:

C:\tutorials\simple>java -cp \target\simple-1.0-SNAPSHOT.jar com.mytutorial.App

Results in:

Hello World!

-cp Æ stands for classpath I.e where to find your class or classes

com.mytutorial.App Æ Fully qualified class name

Note: Lot of beginners make the mistake of not specifying the classpath “-cp” To know the

syntax of java type C:\tutorials\simple>java –help in a command prompt

This tutorial has given a basic idea of building and running a java application using eclipse

and maven Now if you want your application to be shared by other applications you can

install them into your maven repository “C:\java\.m2\repository” by:

C:\tutorials\simple>mvn install

Now if you go and check under “C:\java\.m2\repository” you should find your package under

C:\java\.m2\repository\com\mytutorial\simple\1.0-SNAPSHOT

com.tutorial Æ is the groupid

simple Æ is the artefact

1.0-SNAPSHOT Æ is the package version

Trang 15

Open up and check your “pom.xml” (plug-in details are optional) under

“c:\tutorials\simple” as shown below:

References & useful sites:

ƒ Maven home Æ http://maven.apache.org/

ƒ Maven book Æ http://mavenbook.xwiki.com/xwiki/bin/view/Main/WebHome,

http://www.devzuz.com/web/guest/products/resources

ƒ Java World Articles Æ

http://www.javaworld.com/javaworld/jw-12-2005/jw-1205-maven.html, http://www.javaworld.com/javaworld/jw-02-2006/jw-0227-maven.html,

ƒ Maven 2 repository Æ http://repo1.maven.org/maven2/

Please feel free to email any errors to java-interview@hotmail.com Also stay tuned at

http://www.lulu.com/java-success for more tutorials and Java/J2EE interview

resources

Trang 16

Tutorial 2 – Java Web, Maven and Eclipse

Now let’s create a simple Web project using maven and eclipse Download

and install the Tomcat Server site: http://tomcat.apache.org/download-60.cgi

under C:/java

ƒ Create a skeleton Maven based Web project structure (remember: maven is all about

“convention over configuration” i.e consistency) using the following archetype plug-in

command Remember that heart of maven is plug-ins and you can look at the syntax for

different plug-ins at http://maven.apache.org/plugins/index.html

C:\tutorials>mvn archetype:create -DgroupId=com.mytutorial -DartifactId=simpleWeb

-DarchetypeArtifactId=maven-archetype-webapp

Trang 17

The above command should have created the following structure in your file system under

c:/tutorials

ƒ Now, you need to open up your pom.xml file under C:\tutorials\simpleWeb in a note pad

and add the following lines in bold to give the WTP (Web Tools Platform) support

<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">

<configuration>

<source>1.5</source>

Trang 18

<configuration>

<downloadSources>false</downloadSources> <wtpversion>1.5</wtpversion>

Trang 19

Click “Finish” Now you should have the simpleWeb project in your workspace

Trang 20

Note: Please note that the project simpleWeb has a world icon next to it along with “J” for java,

to indicate that it is a web project You could also check this by right clicking on simpleWeb

project then selecting properties Æ Project Facets, which says Java version 5.0 & Dynamic Web

Module Version 2.4

Important: If you see any errors against simpleWeb then try running the following command in a

command prompt and then refresh your simpleWeb project within eclipse by right clicking and then selecting “Refresh” or F5

C:\tutorials\simpleWeb>mvn eclipse:clean eclipse:eclipse

Note: The above command will clean (i.e delete) it first and then rebuild the eclipse files This

means that you don’t have to check-in these eclipse related files and folders into your source

control You can add these project, classpath and settings files/folder under

“c:/tutorials/simpleWeb” to the cvs or subversion ignore list

ƒ Now let’s set up the Tomcat to run inside eclipse by Window Æ Preferences

Click “Add”

Select “Apache Tomcat V6” and click “Next”

Trang 21

Select the “Tomcat Installation Directory” and click “Finish” and then “Ok”

ƒ Deploy the package into Tomcat Server by: right clicking on the server and then selecting

“Add and Remove Projects”

Trang 22

ƒ Start the Tomcat Server by right clicking on the server inside the server tab and then selecting

“Start”

Trang 23

ƒ Now finally to run the index.jsp, right click on it and select “Run As” and then “Run On

Server”

Trang 24

ƒ Select the server on the next dialog and now you should see:

ƒ Alternatively you can type the following URL Æ http://localhost:8080/simpleWeb/index.jsp

in a an instance of the Internet browser outside eclipse and you should see:

Trang 25

ƒ Now stop the server inside eclipse by right clicking on it and then selecting stop

ƒ Now let’s see how to package this war file outside eclipse and deploy it to Tomcat server To package it using maven run:

C:\tutorials\simpleWeb>mvn package

You can open up a file explorer and check for the presence of the packaged “war” file as shown below

Trang 26

ƒ Now copy your “simpleWeb.war” file into the Tomcat’s “webapps” folder as shown below:

ƒ Now you can start your Tomcat server as a service or from a command prompt as shown

below:

C:\java\Tomcat6.0\bin>tomcat6.exe

Trang 27

ƒ After the server has started , you can type the following URL Æ

http://localhost:8080/simpleWeb/index.jsp in an instance of the Internet browser outside

eclipse and you should see:

Note: It is also worth opening the “simpleWeb.war” file and noting down the packaging structure

for a “war” (i.e Web ARchive file) file

References & useful sites:

ƒ Maven home Æ http://maven.apache.org/

ƒ Maven book Æ http://mavenbook.xwiki.com/xwiki/bin/view/Main/WebHome,

http://www.devzuz.com/web/guest/products/resources

ƒ Java World Articles Æ

http://www.javaworld.com/javaworld/jw-12-2005/jw-1205-maven.html, http://www.javaworld.com/javaworld/jw-02-2006/jw-0227-maven.html,

ƒ Maven 2 repository Æ http://repo1.maven.org/maven2/

Please feel free to email any errors to java-interview@hotmail.com Also stay tuned at

http://www.lulu.com/java-success for more tutorials and Java/J2EE interview

resources

Trang 28

Tutorial 3 – JSF, Maven and Eclipse

This tutorial is a continuation of the Tutorial 2 We will be using JSF to extend the

simpleWeb project

ƒ Open your maven’s pom.xml inside eclipse by double clicking on it and add all the

dependencies for Sun’s JSF RI, Apache commons etc marked in bold By adding these

dependencies you tell maven to download these dependency “jar” files for you Maven

identifies these jar files based on groupid:artifactid:version

Trang 29

After adding remember to save your pom.xml file in eclipse

Note: Now, you may ask how did I find out the groupid:artifactid:version combination You can do

this by looking at the dependency jar’s pom.xml files in the maven repository at

http://repo1.maven.org/ For example let’s take the jsf-api & jsf-impl jar files, which can be found at:

Now, if you drill into this, you will find the “pom” files, and if you open it you should be able to find its coordinates in terms of groupid:artifactid:version Let’s try this for jsf-api

Trang 30

The groupid:artifactid:version forms the coordinates to identify a particular jar in the maven

repository or your local repository C:\java\.m2\repository

Note: If a particular dependency jar in your pom.xml file is not found in the local repository

c:\java\.m2\repository, maven 2 will download this from its repository at

http://repo1.maven.org/maven2 Isn’t this a better way to manage your dependency jars rather than

you having to manually download it and check all this into your source control and maintain all the versions etc?

Ngày đăng: 24/03/2014, 05:21

TỪ KHÓA LIÊN QUAN