因为厌倦了Ant总是要明确指定要做什么事它才会去做什么事的笨构建方式。这段时间终于下定决心,着手全面转向使用Maven来作项目管理和构建,今天开始也做下使用笔记。
以前好几次都决心把项目管理的工作都转到Maven,但一直没有下决心,也是断断续续的看了Maven的很多资料,是时候发挥下它的价值了。这是第一篇应使笔记,从搭建Maven的第一步开始,今后陆续会把使用的整个过程和碰到的问题写进来。
1.到官方站点 下载Maven
(当前版本2.10)
并解压到 $USERHOME 目录下 我使用的SuSe11.1 Linux x86_64 系统,所以我的解压目录是/home/bencmai/MyProgram/apache-maven-2.1.0.如果你使用的是其他系统,你大可以解压到任意你喜欢的地方。
2.创建一个符号链接,这样能更容易使用,升级Maven的时候也不再需要改变环境变量
bencmai@benc-linux:~> su
口令:******
#(这句只需做一此,你可以把符号连接放任何地方,记得相应修改下两句的环境变量输出)
benc-linux:/home/bencmai # ln -s /home/bencmai/MyProgram/apache-maven-2.1.0 maven
benc-linux:/home/bencmai # export M2_HOME=/usr/local/maven
benc-linux:/home/bencmai # export PATH=${M2_HOME}/bin:${PATH}
#如果你想不用每次都得敲输出环境变量,就把这两句放到你相应用户的 .bashrc 文件中,我的是(/home/bencmai/.bashrc)
benc-linux:/home/bencmai # export M2_HOME=/usr/local/maven
benc-linux:/home/bencmai # export PATH=${M2_HOME}/bin:${PATH}
#执行mvn -v命令验证下是否正确安装
benc-linux:/home/bencmai # mvn -v
Apache Maven 2.1.0 (r755702; 2009-03-19 03:10:27+0800)
Java version: 1.6.0_13
Java home: /usr/lib64/jvm/java-1.6.0-sun-1.6.0/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux" version: "2.6.27.21-0.1-default" arch: "amd64" Family: "unix"
#有反应并输出Maven的一些版本信息,说明安装正确.可以那来干活了,呵呵
3.创建我们的第一个Maven项目,进入我们用户的老家(/home/bencmai/)
#回用户老家
bencmai@benc-linux:/> cd
#擦看当前老家目录结构
bencmai@benc-linux:~> ls
bin Desktop Documents example MyProgram public_html SoftWeave
#用mvn在用户老家创建一个简单的Maven原型项目
bencmai@benc-linux:~> mvn archetype:create -DgroupId=com.yotexs.srp \
> -DartifactId=yotexs \
> -DpackageName=com.yotexs
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO] task-segment: [archetype:create] (aggregator-style)
[INFO] ------------------------------------------------------------------------
Downloading: http://repo1.maven.org/maven2/com/jcraft/jsch/0.1.27/jsch-0.1.27.pom
965b downloaded (jsch-0.1.27.pom)
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:create]
[WARNING] This goal is deprecated. Please use mvn archetype:generate instead
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating OldArchetype: maven-archetype-quickstart:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.yotexs.srp
[INFO] Parameter: packageName, Value: com.yotexs
[INFO] Parameter: package, Value: com.yotexs
[INFO] Parameter: artifactId, Value: yotexs
[INFO] Parameter: basedir, Value: /home/bencmai
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] OldArchetype created in dir: /home/bencmai/yotexs
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 seconds
[INFO] Finished at: Sun May 24 08:38:55 HKT 2009
[INFO] Final Memory: 16M/168M
[INFO] ------------------------------------------------------------------------
bencmai@benc-linux:~> cd yotexs
bencmai@benc-linux:~/yotexs> tree
./
yotexs
|--
pom.xml
`-- src
|--
main
| `-- java
| `-- com
| `-- yotexs
| `-- App.java
`--
test
`-- java
`-- com
`-- yotexs
`-- AppTest.java
9 directories, 3 files
bencmai@benc-linux:~/yotexs>
就这么简单,我们的第一个HelloWorld级别的(yotexs)项目就建成了,虽然很原始,但所有的配置,目录结构,Maven都按照它自身的所谓的约定优于配置
的原则自动给我们构建好了。
archetype:create
称为一个Maven目标
(goal) ,类似于一个Ant目标 (target);它们都描述了将会在构建中完成的工作单元,Maven有许多可用的archetype,上面用到的 archetype:create 目标只是其中最原始的一个,如果你想选其他的目标,删除之前构建的
yotexs项目后,可以用 archetype:generate 代替
archetype:create ,再来构建一此,这样我们就可以选择你所需要的项目结构了,我们再来全程完整测试一遍
bencmai@benc-linux:~> mvn archetype:generate -DgroupId=com.yotexs.srp -DartifactId=yotexs -DpackageName=com.yotex
#(1)创建项目
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO] task-segment: [archetype:generate] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:generate]
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: internal -> appfuse-basic-jsf (AppFuse archetype for creating a web application with Hibernate, Spring and JSF)
2: internal -> appfuse-basic-spring (AppFuse archetype for creating a web application with Hibernate, Spring and Spring MVC)
3: internal -> appfuse-basic-struts (AppFuse archetype for creating a web application with Hibernate, Spring and Struts 2)
4: internal -> appfuse-basic-tapestry (AppFuse archetype for creating a web application with Hibernate, Spring and Tapestry 4)
5: internal -> appfuse-core (AppFuse archetype for creating a jar application with Hibernate and Spring and XFire)
6: internal -> appfuse-modular-jsf (AppFuse archetype for creating a modular application with Hibernate, Spring and JSF)
7: internal -> appfuse-modular-spring (AppFuse archetype for creating a modular application with Hibernate, Spring and Spring MVC)
8: internal -> appfuse-modular-struts (AppFuse archetype for creating a modular application with Hibernate, Spring and Struts 2)
9: internal -> appfuse-modular-tapestry (AppFuse archetype for creating a modular application with Hibernate, Spring and Tapestry 4)
10: internal -> maven-archetype-j2ee-simple (A simple J2EE Java application)
11: internal -> maven-archetype-marmalade-mojo (A Maven plugin development project using marmalade)
12: internal -> maven-archetype-mojo (A Maven Java plugin development project)
13: internal -> maven-archetype-portlet (A simple portlet application)
14: internal -> maven-archetype-profiles ()
15: internal -> maven-archetype-quickstart ()
16: internal -> maven-archetype-site-simple (A simple site generation project)
17: internal -> maven-archetype-site (A more complex site project)
18: internal -> maven-archetype-webapp (A simple Java web application)
19: internal -> jini-service-archetype (Archetype for Jini service project creation)
20: internal -> softeu-archetype-seam (JSF+Facelets+Seam Archetype)
21: internal -> softeu-archetype-seam-simple (JSF+Facelets+Seam (no persistence) Archetype)
22: internal -> softeu-archetype-jsf (JSF+Facelets Archetype)
23: internal -> jpa-maven-archetype (JPA application)
24: internal -> spring-osgi-bundle-archetype (Spring-OSGi archetype)
25: internal -> confluence-plugin-archetype (Atlassian Confluence plugin archetype)
26: internal -> jira-plugin-archetype (Atlassian JIRA plugin archetype)
27: internal -> maven-archetype-har (Hibernate Archive)
28: internal -> maven-archetype-sar (JBoss Service Archive)
29: internal -> wicket-archetype-quickstart (A simple Apache Wicket project)
30: internal -> scala-archetype-simple (A simple scala project)
31: internal -> lift-archetype-blank (A blank/empty liftweb project)
32: internal -> lift-archetype-basic (The basic (liftweb) project)
33: internal -> cocoon-22-archetype-block-plain ([http://cocoon.apache.org/2.2/maven-plugins/])
34: internal -> cocoon-22-archetype-block ([http://cocoon.apache.org/2.2/maven-plugins/])
35: internal -> cocoon-22-archetype-webapp ([http://cocoon.apache.org/2.2/maven-plugins/])
36: internal -> myfaces-archetype-helloworld (A simple archetype using MyFaces)
37: internal -> myfaces-archetype-helloworld-facelets (A simple archetype using MyFaces and facelets)
38: internal -> myfaces-archetype-trinidad (A simple archetype using Myfaces and Trinidad)
39: internal -> myfaces-archetype-jsfcomponents (A simple archetype for create custom JSF components using MyFaces)
40: internal -> gmaven-archetype-basic (Groovy basic archetype)
41: internal -> gmaven-archetype-mojo (Groovy mojo archetype)
Choose a number: (1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41) 15: :
#(2)选这项目原型和配置
Define value for version: 1.0-SNAPSHOT: :
Confirm properties configuration:
groupId: com.yotexs.srp
artifactId: yotexs
version: 1.0-SNAPSHOT
package: com.yotexs.srp
Y: :
#(3) 确认配置信息
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating OldArchetype: maven-archetype-quickstart:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.yotexs.srp
[INFO] Parameter: packageName, Value: com.yotexs.srp
[INFO] Parameter: package, Value: com.yotexs.srp
[INFO] Parameter: artifactId, Value: yotexs
[INFO] Parameter: basedir, Value: /home/bencmai
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] OldArchetype created in dir: /home/bencmai/yotexs
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL(自动创建)
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5 seconds
[INFO] Finished at: Sun May 24 10:01:04 HKT 2009
[INFO] Final Memory: 16M/168M
[INFO] ------------------------------------------------------------------------
bencmai@benc-linux:~> tree yotexs
#(4)查看下Maven为我们创建的项目结构
yotexs
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- com
| `-- yotexs
| `-- srp
| `-- App.java
`-- test
`-- java
`-- com
`-- yotexs
`-- srp
`-- AppTest.java
11 directories, 3 files
bencmai@benc-linux:~> cd yotexs
#(5) 进入刚创建的项目目录中
bencmai@benc-linux:~/yotexs> mvn install
#(6) 执行项目构建指令
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building yotexs
[INFO] task-segment: [install]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/bencmai/yotexs/src/main/resources
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to /home/bencmai/yotexs/target/classes
[INFO] [resources:testResources]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/bencmai/yotexs/src/test/resources
[INFO] [compiler:testCompile]
[INFO] Compiling 1 source file to /home/bencmai/yotexs/target/test-classes
[INFO] [surefire:test]
[INFO] Surefire report directory: /home/bencmai/yotexs/target/surefire-reports
-------------------------------------------------------
T E S T S(自动测试)
-------------------------------------------------------
Running com.yotexs.srp.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.046 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] [jar:jar]
[INFO] Building jar: /home/bencmai/yotexs/target/yotexs-1.0-SNAPSHOT.jar
[INFO] [install:install]
[INFO] Installing /home/bencmai/yotexs/target/yotexs-1.0-SNAPSHOT.jar to /home/bencmai/.m2/repository/com/yotexs/srp/yotexs/1.0-SNAPSHOT/yotexs-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Sun May 24 10:01:20 HKT 2009
[INFO] Final Memory: 26M/173M
[INFO] ------------------------------------------------------------------------
bencmai@benc-linux:~/yotexs> cd
..
bencmai@benc-linux:~> tree yotexs
#(6) 我们查看一下Maven构建完后都干了些什么
yotexs
|-- pom.xml
|-- src
| |-- main
| | `-- java
| | `-- com
| | `-- yotexs
| | `-- srp
| | `-- App.java
| `-- test
| `-- java
| `-- com
| `-- yotexs
| `-- srp
| `-- AppTest.java
`-- target
|-- classes
| `-- com
| `-- yotexs
| `-- srp
| `-- App.class
|-- maven-archiver
| `-- pom.properties
|-- pom-transformed.xml
|-- surefire-reports
| |-- TEST-com.yotexs.srp.AppTest.xml
| `-- com.yotexs.srp.AppTest.txt
|-- test-classes
| `-- com
| `-- yotexs
| `-- srp
| `-- AppTest.class
`-- yotexs-1.0-SNAPSHOT.jar
22 directories, 10 files
bencmai@benc-linux:~> cd yotexs
#(6) 进入我们的项目目录,执行一下我们构建好的HelloWorld 程序
bencmai@benc-linux:~/yotexs> java -cp target/yotexs-1.0-SNAPSHOT.jar com.yotexs.srp.App
Hello World!
bencmai@benc-linux:~/yotexs>
Maven 让我们的项目从建立到测试到构建发布,都如此的简单。呵呵,方便之极
环境搭建好了,下一篇,我们将开始用Maven做点实际的事情了
分享到:
相关推荐
在“JavaFX+Jfoenix 学习笔记(一)--环境搭建及多款Hello World演示源码”中,我们将主要探讨以下几个知识点: 1. **JavaFX环境搭建**: - 安装Java Development Kit (JDK):JavaFX不再包含在标准JDK中,因此需要...
【标题】"201205_Maven学习笔记2.rar" 涉及到的是 Maven,这是一个在软件开发中广泛使用的项目管理和综合工具。Maven 提供了一种标准化的方式来构建、文档化以及管理Java项目,它通过一个项目对象模型(Project ...
【Web Service学习笔记——XFrie框架详解】 Web Service是一种通过网络进行通信的服务,它允许不同的应用程序之间进行数据交换,跨越了操作系统和编程语言的障碍。XFrie是一个轻量级、高性能的Java Web Service框架...
- Maven学习笔记:介绍了Maven的实战应用,包括环境配置、坐标和依赖、仓库管理、生命周期和插件、聚合、继承、反应堆、Nexus使用、测试、Hudson集成、Profile定制、项目站点生成和插件编写等主题。 - 学习Maven的...
然而,随着项目的发展,依赖库的数量会不断增多,这时,本地Maven仓库可能无法满足需求,这时就需要搭建一个私有的Maven仓库,即Maven私服。Nexus作为一款广泛应用的Maven私服,可以有效地解决这个问题。本文将详细...
本篇学习笔记将引导我们从零开始,通过一个简单的"HelloWorld"项目,了解Struts2的基础知识。 首先,我们需要在本地环境中搭建Struts2的开发环境。这包括安装JDK、配置Java环境变量、下载Apache Struts2的最新版本...
### SpringBoot学习笔记 #### 一、SpringBoot入门 ##### 1、SpringBoot简介 - **简化Spring应用开发**:SpringBoot是简化Spring应用开发的一个框架,它通过默认配置极大地减少了开发过程中的样板代码,使得开发者...
【标题】"hibernate学习笔记-01helloworld"主要涵盖了Hibernate框架的基础入门知识,是初学者了解和掌握Hibernate的重要起点。Hibernate是一款强大的Java对象关系映射(ORM)框架,它简化了数据库操作,使得开发者...
《良葛格 Java 学习笔记-JavaGossip全(v1+v2)》是一部集成了作者良葛格多年编程经验的学习资料,旨在帮助初学者和有经验的开发者深入理解和掌握Java这门强大的编程语言。这份笔记包含了JavaGossip的两个版本,v1和...
SpringBoot经典学习笔记是针对Java开发者的宝贵资源,它涵盖了SpringBoot的核心概念、特性以及实践应用。SpringBoot是由Pivotal团队开发的框架,旨在简化Spring应用的初始搭建以及开发过程,通过“约定优于配置”的...
### Maven in Action 读书笔记知识点概述 #### 1. Maven简介 - **Maven作为构建工具的优势**:Maven的设计理念在于最小化构建过程中的重复性工作,它通过抽象出通用的构建生命周期,并为常见的构建任务提供现成的...
### Spring Boot 学习笔记知识点总结 #### 一、Spring发展史 - **Spring1.x时代**:在Spring1.x的时代,主要通过XML文件来配置Bean。随着项目的规模扩大,XML配置文件的数量也随之增加,这导致开发人员需要频繁地...
### Spring Boot 学习笔记概览 #### 一、Spring 的发展历程 1. **Spring 1.x 时代:** - 在Spring框架发展的初期(即Spring 1.x时代),主要采用XML作为配置文件来管理Bean的声明周期。这种方式在项目规模较小的...
在“尚硅谷spring boot 代码及笔记”中,你将找到一系列关于 Spring Boot 的实践代码和学习笔记,这对于初学者或有一定经验的开发者来说都是宝贵的资源。 首先,Spring Boot 的核心特性包括自动配置、起步依赖和...
总结来说,"Eclipse搭建SSH框架开发学习笔记"涵盖了从基础环境准备(JDK安装与环境变量配置)、开发工具(Eclipse与Tomcat配置)、数据库(MySQL服务器搭建)到系统服务管理等多个环节,为初学者提供了详尽的步骤...
包含了<<maven权威指南>>,maven官方api使用等,自己的maven笔记等详细的maven使用教程,同时还包含了maven客户端搭建,服务器配置于搭建的详细资料,是以整套的maven学习资料.
在本篇学习笔记中,我们将聚焦于环境搭建和创建第一个"Hello, World!"程序。 首先,要开始学习Struts2,你需要确保你的开发环境中已经安装了以下基本组件: 1. **Java Development Kit (JDK)**:Struts2依赖于Java...