我的Maven2之旅:三.Maven的一小步,个人的一大步

来源:互联网 发布:怎样短期网络投资理财 编辑:程序博客网 时间:2024/05/19 03:29

 上一篇已经讲到了Maven的基本命令,今天我们就来小试牛刀一把.

此前,我们可以用Eclispe创建一个小的Java工程,本人创建了一个能输出"Hello!Maven!"的工程,它只有一个类,一个函数,不用猜你也能想到里面写的什么.

指的一提的是它的目录结构,下面是这个工程目录结构的截图:

为什么要在src底下放两层目录呢,一会将见分晓.

接下来,我们使用Maven创建一个HelloMaven的Maven项目,打开cmd窗口,转入你的Maven工程目录(本人机器上是C:/tmp),然后输入如下的命令:
mvn archetype:create -DgroupId=com.sitinspring -DartifactId=HelloMaven
其中,com.sitinspring告知了Maven工程的代码根目录,而HelloMaven是工程名.这段命令指示Maven创建一个HelloMaven的工程,它的代码根目录是com.sitinspring.命令运行后将生成一个名为HelloMaven的目录,其中有主目录,测试目录和工程文件pom.xml.

接到你的命令,Maven开始工作了,它输出了一大段文字,见下图:

你将得到一个名叫HelloMaven的目录,如下图:

在HelloMaven目录下,还将生成一个pom.xml的文件,它就是Maven生成的工程文件.暂时你不需要关心其具体内容.
因为接下来还有任务:把Eclispe工程中的Inlet.java文件放入C:/tmp/HelloMaven/src/main/java/com/sitinspring中.

然后打开cmd窗口,进入C:/tmp/HelloMaven目录.执行命令:
mvn package
你将看到Maven再次输出一堆信息.如下图:

其后C:/tmp/HelloMaven的目录结构如下图:

在target目录下,有classes目录.这是生成的class文件;根目录下还生成了我们的目标文件HelloMaven-1.0-SNAPSHOT.jar,它就是打包后的jar.

测试一下,我们打开cmd窗口,进入目录 C:/tmp/HelloMaven/target,然后执行命令:
java -cp HelloMaven-1.0-SNAPSHOT.jar com.sitinspring.Inlet,你将看到Hello!Maven!被输出出来.如下图.

现在为止,我们未曾写一句代码就得到了项目的jar文件,你可以拿自己以前的工程来试试mvn package的功能了.
如果是Swing程序的话效果更好.下图是Swing程序包执行的效果.

本文例子下载一:http://www.blogjava.net/Files/sitinspring/MavenPakcageExample01.zip
本文例子下载二:http://www.blogjava.net/Files/sitinspring/MavenPakcageExample02.zip
(这个例子就是上图输出的Swing程序,但Src路径要深一些,所以DgroupId也不同,具体写法请大家揣摩)

附录:
Maven工程的目录结构:

Maven project root         pom.xml Maven2 project file Maven2项目文件     src/             main/ project main directory 项目主体目录                 java source code directory 原码目录                 resources the directory of resource for source code 原码所需资源                 filters the directory of filtering resource files 原码所需资源文件过滤                 assembly Assembly descriptors                   config the directory of configuration file  配置文件目录         test                     java test source code directory 测试原码目录                 resources the directory of resource for the test source code 测试原码所需资源                 filters the directory of filtering test resource files 测试原码所需资源文件过滤         site the directory of site resource 与site相关的资源目录     target the directory for output 输出目录         classes the directory for output classes class 目录         test-classes the directory for output test classes  test class 目录         site the directory for output site and reports site 输出目录

注:转自http://www.blogjava.net/sitinspring


原创粉丝点击