Cài đăt và thao tác với Maven trên Eclipse IDE [Giới thiệu về ApacheMaven] Phần 1: Khái niệm về Maven Đầu tiên là khái niệm về Maven, Maven là gì, nó có lợi ích gì, cái này các bạn "Gúc"
Trang 1Maven Mkyong
How to install Maven on Windows
Posted on November 25, 2009 , Last modified : December 13, 2012
By mkyong
Apache Maven is not require to install on Windows as a service component, you just need to download the Maven’s zip file, extract it and configure the Windows environment path variable.Tools Used :
Make sure JDK is installed, and “JAVA_HOME” variable is added in Windows environment
variable, and point to the JDK folder
Trang 22 Download Apache Maven
Visit this Maven official website, choose a version and click on the download link, e.g
Trang 35 Add PATH
Update PATH variable, append “Maven bin folder” path, so that you can run the Maven’s command everywhere
6 Verification
Done, to verify it, in command prompt, type “mvn –version“
Apache Maven 2.2.1 (r801777; 2009-08-07 03:16:01+0800)
Trang 4Java version: 1.6.0_13
Java home: C:\Program Files\Java\jdk1.6.0_13\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows xp" version: "5.1" arch: "x86" Family: "windows"
If you see similar message, means your Apache Maven is installed successfully on Windows
How to enable proxy setting in Maven
Posted on November 25, 2009 , Last modified : December 13, 2012
1 Maven configuration file
Find {M2_HOME}/conf/settings.xml, and put your proxies detail inside
{M2_HOME}/conf/settings.xml
<! proxies
the network.
| Unless otherwise specified (by system property or command-line switch),
the first proxy
Trang 5| Unless otherwise specified (by system property or command-line switch),
the first proxy
Restart is NOT required, how do you restart Maven? Maven is just a command, when you call it,
it will read this file again
Trang 6Cài đăt và thao tác với Maven trên Eclipse IDE [Giới thiệu về Apache
Maven]
Phần 1: Khái niệm về Maven
Đầu tiên là khái niệm về Maven, Maven là gì, nó có lợi ích gì, cái này các bạn "Gúc" một phát ra đầy rẫy mình chỉ tóm tắt chút đỉnh những tài liệu trên "Gúc" để mọi người có thể hình dung cơ bản về Maven Sau đó mình sẽ tiếp vào phần 2 luôn, tính mình không thích lý thuyết dài dòng.
Đối với những Dev nào làm về cách hệ thống lớn, phức tạp sử dụng nhiều thư viện và frame work lại đòi hỏi phải release liên tục cho nên công việc đóng gói (build & deploy), quản lý, nâng cấp và bào trì chúng thực sự rất oải Ngoài ra việc tích hợp các thư viện mới cũng như quản lý và nâng cấp các thư viện có sẵn là tương đối phức tạp.
Tuy nhiên, những trở ngại nói trên hoàn toàn có thể giải quyết được nhờ vào Apache
Maven
Vậy ưu điểm của Maven là gì đó là:
Tự động hóa toàn bộ quá trình release project, khởi tạo, cập nhật thư viện, build & unit test
và release, với một project cỡ lớn thì việc này là tối quan trọng, chỉ cần một sai lầm trong một công đoạn là có để delay cả dự án (lúc đấy thì tha hồ mà OT nhá =)).
Quản lý các dependency (các thư viện) trong project một các ưu việt: tự động cập nhật, mở rộng dễ dàng, đóng gói Vì thế toàn bộ các file jar thư viện điều được "vứt" cho Maven quản
lý điều đó khiến cho dung lượng project của chúng ta nhẹ nhàng đi rất nhiều.
Phân chia 1 project lớn thành các module nhỏ, từ đó cho phép làm việc đồng thời trên các module khác nhau, đồng thời vẫn tạo được tính thống nhất Không phụ thuộc vào IDE, cho phép chuyển đổi từ Eclipse sang NetBeans, IntelliJ IDEA một cách dễ dàng Mình thấy trong thực thế thấy rất nhiều dự án được nhiều công ty cùng phát triển, mỗi công ty lại phát
Trang 7triển một module và lại có một chuẩn IDE riêng nên Maven quả là một lựa chọn không tồi Trong thực tế thì cũng có khá nhiều dự án cũng như các công ty sử dụng Maven như một nền tảng build với những ứng dụng Java Enterprise Các Dev, các bạn đã khởi đầu với Ant như là Beginner, vậy hãy đến với Maven như một Professional.
Đã được chỉnh sửa lần cuối bởi iamvtn : 17-03-2012 lúc 12:34 AM.
In code we trust Trả lời cùng với trích dẫn
Bài viết 169
Cài đặt Maven Apache
Phần 2: Cài đặt Maven.
1 Đầu tiên bạn hãy tải Maven tại địa chỉ: maven.apache.org/download.html
Ở đây tôi tải bản 3.0.4 là bản mới nhất tại thời điểm tác giả viết bài này.
2 Giải nén rồi để ở đâu đó tùy bạn, ở đây tôi để Maven tại: C:\apache-maven-3.0.4
3 Thiết lập biến môi trường.
Bạn hãy vào System Properties chọn Environment Variables.
Trang 8Sau đó chọn New trong mục System variables thiết lập MAVEN_HOME chỉ đến thư mục cài Maven như hình sau.
Sau đó ta hãy thêm (edit) patch cho Maven "%M2_HOME%\bin" trong mục Patch trong mục System variables như trong hình:
Trang 9Sau đó ta hãy vào Command line (vào run gõ cmd -> enter) thử lệnh maven version
Nếu như trên hình tức là tức là mọi việc đã được cấu hình chính xác.
In code we trust Trả lời cùng với trích dẫn
Bài viết 169
Trang 10Thao tác với Maven trên Eclipse
Phần 3: Thao tác với Maven trên Eclipse.
Bạn cũng có thể tạo một dự án Maven từ dòng lệnh command line, nhưng ở đây tôi xin phép hướng dẫn thao tác với Maven trên Eclipse Eclipse mà một IDE tuyệt vời một người bạn không thể thiếu đối với những Java Dev.
Đầu tiên bạn phải tải plugin m2eclispe cho Eclipse tại đây: eclipse.org/m2e/download/
Ở đây tôi dùng Eclipse mới nhất là bản Eclipse 3.7 (Indigo).
Bạn vào menu Help ấn chọn Install New Software -> điền URL:
http://download.eclipse.org/technology/m2e/releases/ hoặc
http://download.eclipse.org/technology/m2e/releases-for-indigo/1.4/
http://download.eclipse.org/technology/m2e/releases/1.3/1.3.1.20130219-1424
vào mục work with rồi ấn Add -> Gõ tên plugin rồi ấn OK sẽ hiện thị ra như hình sau:
Sau đó việc của bạn là ấn next và chờ plugin cài đặt xong, tranh thủ thư giãn làm cốc caffe cái nhỉ
Khi plugin cài đặt xong bạn hãy restart lại Eclipse, giờ bạn đã có một công cụ tuyệt với để bắt đầu với một dự án Maven đầu tiên
Trước khi tạo project bạn hãy config lại thư viện Maven trong eclipse như sau: chọn menu Window -> Preferences -> Maven -> Installations ấn nút Add rồi chỉ đến thư mục cài đặt Maven rồi ấn OK để Eclipse làm việc với Maven mình vừa cài đặt bên trên.
Trang 11Việc tạo một một Maven bây giờ trở nên dễ hơn ăn kẹo, đơn giản chỉ là chọn New -> Maven
project Trong phần Artifact-Id cho từng loại project khác nhau, ở đây tôi chọn
maven-archetype-quickstart
Tiếp theo bạn hãy định nghĩa Group Id, Artifact Id và version cho project ở đây tôi gõ như sau
Group Id: com.iamvtn.maven
Artifact Id: helloworld
Vậy hai thằng này nó có ý nghĩa là gì? Tôi sẽ đề cập trong phần 4: Tìm hiểu các thành phần trong Maven project.
Trang 12Sau khi ấn finish project sẽ có cấu trúc như sau:
Giờ bạn có thể build, clean hay install hoàn toàn trên eclipse bằng cách bạn click chuột phải vào project chọn Run as -> Build, Clean, Install
File build được sẽ được lưu vào thư mục target của dự án.
(chú ý hãy enable chức năng Workspace Resolution bằng cách click chuột phải vào project chọn đến menu Maven -> Enable Workspace Resolution)
Đã được chỉnh sửa lần cuối bởi iamvtn : 17-03-2012 lúc 11:07 AM.
In code we trust Trả lời cùng với trích dẫn
3 17-03-2012, 12:26 AM#4
Trang 13Free as the wind
Ngày gia nhập
01 2007 Nơi ở Somewhere I belong
Bài viết 169
các thành phần trong Maven project
Phần 4: Tìm hiểu các thành phần trong Maven
Ta hãy tìm hiểu và mổ xẻ file pom.xml trên project maven ta vừa tạo trên phần 3.
Model version liên luôn là 4.0.0 với Maven 2.x trở lên
Select All | Show/Hide
2 <groupId> com.iamvtn.maven </groupId>
Đây chính là tên của maven project.
XML Code:
Select All | Show/Hide
Trang 143 <artifactId> helloworld </artifactId>
Định danh version của maven project
Select All | Show/Hide
5 <packaging> jar </packaging>
Tên của project
XML Code:
Select All | Show/Hide
6 <name> helloworld </name>
Đây là nơi gọi các library dependencies ở đây tôi có sử dụng JUnit cho việc viết Unit test Bạn có thể gọi thêm các library bằng thêm các thẻ dependency vào, vậy đấy việc thêm bớt hay upgrade thư viện chỉ đơn giản là config vài dòng lệnh, khỏe re không các bạn
XML Code:
Select All | Show/Hide
7 <dependencies>
8 <dependency>
9 <groupId> junit </groupId>
10 <artifactId> junit </artifactId>
19 <groupId> org.apache.maven.plugins </groupId>
20 <artifactId> maven-compiler-plugin </artifactId>
21 <configuration>
Trang 15Helloworld project vừa tạo mặc định ta có hai phần để chứa các java code đó là
src/mail/java để chứa các logic code và src/test/java để chứa các kịch bản Unit test.
Ta hãy xem lớp App.java trong src/mail/java, bạn có thể tự do phát triển và coding tùy ý ở đây.
"Code without test is just like cook without taste"
Trang 1650 public AppTest ( String testName )
Bài viết 169
Trang 17Làm việc với nhiều module với Maven
Phần 5 Làm việc với nhiều module với Maven
Như phần 1 đã nói maven có điểm mạnh với việc làm việc với nhiều module, sau đây ta sẽ tìm hiểu cách tạo các module và set các chúng sử dụng lẫn nhau (dependency) như thế nào Sau đây tôi sẽ tạo ra 4 module là Model, Persistence, Controller và View, nghe quen nhỉ, he
he tôi sẽ tạo một Maven project theo kiến trúc MVC
Đầu tiên để tạo một Maven Module thì Parent project phải có kiểu packaging là pom, ta config lại packaging của helloworld project như sau:
XML Code:
Select All | Show/Hide
1 <packaging> pom </packaging>
Sau đó click chuột phải vào helloworld project chọn New -> Maven Module cũng tương tự như tạo Maven project bạn chọn Artifact-Id là maven-archetype-quickstart rồi chọn package name ở đây tôi gõ là com.iamvtn.maven.model.domain
Sau khi Maven module được tạo ta sẽ có cấu trúc project như sau
Khi đó file pom.xml trong parent maven project sẽ thêm vào các module vừa tạo như sau: XML Code:
Select All | Show/Hide
2 <modules>
3 <module> model </module>
Trang 1820 public void setSayHello ( String sayHello ) {
21 this sayHello sayHello ;
22 }
Tương tự ta cũng sẽ tạo lần lượt tạo thêm module Persistence Module này có tác dụng set
dữ liệu cho Hello Object bên trên, để muốn dùng module Model thì trong file pom.xml của Persistence ta sẽ phải dependency đến Model module
XML Code:
Select All | Show/Hide
24 <dependency>
25 <groupId> com.iamvtn.maven </groupId>
26 <artifactId> model </artifactId>
Trang 1929. package com.iamvtn.maven.persistence.dao ;
30.
31. import com.iamvtn.maven.model.domain.Hello ;
32.
33. public interface HelloDao {
34 public Hello sayHello ();
43 public Hello sayHello ()
44 Hello hello = new Hello ();
45 hello setSayHello ( "Hello" );
46 return hello ;
Tương tự ta cũng tạo ra Maven module là Controller, thằng Controller này sử dụng đến lớp
Persistence và Model thì trong pom.xml của Controller ta cũng phải khai báo các
dependency sau:
XML Code:
Select All | Show/Hide
49 <dependency>
50 <groupId> com.iamvtn.maven </groupId>
51 <artifactId> model </artifactId>
52 <version> 1.0 </version>
53 </dependency>
54 <dependency>
55 <groupId> com.iamvtn.maven </groupId>
56 <artifactId> persistence </artifactId>
57 <version> 1.0 </version>
58 </dependency>
Trong Controller tôi cũng định nghĩa ra một Interface HelloLogicvà một Class implements
Trang 20HelloLogicImpl để thực hiện gọi đến Persistence và lấy ra dữ liệu của sayHello như sau:
61. public interface HelloLogic {
62 public String sayHello ();
73 public String sayHello ()
74 Hello hello = new Hello ();
75 HelloDao helloDao = new HelloDaoImpl ();
76 hello = helloDao sayHello ();
77 String sayHello = hello getSayHello ();
83 <groupId> com.iamvtn.maven </groupId>
84 <artifactId> controller </artifactId>
85 <version> 1.0 </version>
86 </dependency>
Trang 21Và cuối cùng ta sẽ thực hiện gọi đến Controller
100 HelloLogic helloLogic = new HelloLogicImpl ();
101 String sayHello = helloLogic sayHello ();
102 System out println ( sayHello );
Phù thế là bạn đã thực hiện tạo ra được một Maven project có nhiều các module con hoạt động theo mô hình MVC rồi đấy
Giờ bạn có thể build project maven và tùy ý sử dụng được rồi.
Có hai cách build một là bạn dùng Eclipse bằng cách click chuột phải vào project rồi chọn Run as -> Maven install
Hoặc sử dụng command line bằng cách gõ lệnh mvn instal
Trang 22Xem full mã nguồn ở đính kèm
Bạn có thể import maven project bằng cách chọn menu File -> Import -> Maven -> Existing Maven Project rồi chọn đến maven project bạn muốn mở là OK.
Vừa rồi tôi đã hướng dẫn bạn tìm hiểu cơ bản về Maven, đến đây chỉ mới là cào vào bề mặt của maven, Maven đang chờ bạn khám phá và chia sẻ tiếp đó
Attached Files
o helloworldmaven.rar (41.9 KB, 426 views)
Trang 24Getting Started with the Alfresco Maven
What About the Old Ant-based SDK?
Your First Project
Let's Run It
Trang 25 What Just Happened?
Other Commands for Invoking the Build
Working With Your Project in an IDE
Understanding the Project Structure
Creating a Project for Share Tier Customizations
Share project dependencies
Try It: Create a Share Project Using the Archetype
Understanding the Share Project Folder Structure
Running an Integration Test with Share
Dependency Management
Other Topics to Explore on Your Own
Where to Find More Information
License
This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 UnportedLicense To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA
Introduction
This tutorial is for people who have been handed a project that requires you to
customize Alfresco in some way and you're thinking, "Now what?" By the end, you'll know:
How to create a new Alfresco project using the Alfresco Maven SDK
What goes where depending on the type of customization you are doing
How to create and run tests
How to produce an artifact suitable for deploying to your environment or sharing with the community.
This tutorial should be considered a pre-requisite before moving on to more detailed tutorials on things like content modeling, custom actions, behaviors, advanced
workflows, or web scripts
Trang 26Before we jump in, a quick disclaimer: There are almost always multiple routes to a given solution In this tutorial, I'm going to take you through the most direct, safest route that will get you up-and-going quickly Later, if you decide you want to change things up or explore other paths, that's great.
Important Concepts
You don't need to know much about Maven, Alfresco, or why this SDK exists before jumping in but it kind of helps set the scene, so let me take you through those three concepts quickly
Apache Maven
Apache Maven is essentially a build management tool It has many features, but the primary time-saving feature is its ability to understand the dependencies your project relies on (and the dependencies of those dependencies and so on) Maven can then retrieve and cache those dependencies for you If you've ever spent time chasing down JAR file after JAR file, the value of such a tool will be immediately obvious
Alfresco Module Package (AMP)
An AMP is a ZIP file with a folder structure that follows a specific convention AMP files are used to make it easy to share and deploy customizations to the Alfresco platform If your project is about making customizations to the repository tier (the /alfresco web application) you will create a "repo" AMP If your project is about making
customizations to the Share tier (the /share web application) you will create a "share" AMP It is quite common for a project to require changes in both tiers, so in that case you will create two AMPs
Alfresco Maven SDK
Today's developers are used to rails-like frameworks where you have an empty directoryone moment and a fully instantiated, ready-to-run project the next Why should Alfrescodevelopers settle for anything less?
With the Alfresco Maven SDK you don't have to The goal of the Maven-based SDK is to make it extremely easy for you to get started with Alfresco development It consists of a project template (an "archetype" in Maven parlance) and some built-in smarts that make Maven understand how to create AMPs and deploy them to Alfresco