Ant入了个门,算是可以看懂那些build XML文件了,对付目前的工作应该没问题,至于遇到的具体问题到时候具体解决。我现在最需要的是对这些东西先了解个大概,这样心里有点方向。
MAVEN也来入个门吧。参考文档来着官方网站。http://maven.apache.org/guides/getting-started/index.html
MAVEN已经安装完毕并且有和Eclipse集成了。
What is Maven?
In a nutshell,(今天学了个单词,“一言以蔽之”,看英文技术文档的好处在于你学到技术的时候还能顺便提高下英语。很BT吧)。
Maven is an attempt to apply patterns to a project's build infrastructure in order to promote comprehension and productivity by providing a clear path in the use of best practices.
Maven is essentially a project management and comprehension tool
* Builds
* Documentation
* Reporting
* Dependencies
* SCMs
* Releases
* Distribution
首先建个简单的mvn project,你可以通过命令行建议也可以直接在eclipse用GUI导向。
命令行方式:
mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.mycompany.app -DartifactId=my-app
GUI方式:
New-->Project-->Maven-->Maven Project 之后需要在第三部填写Group ID和Artifact ID
建好之后可以在eclipse工程中看到:
![]()
关于src下main和test两个目录,简单介绍一下:
the application sources reside in ${basedir}/src/main/java
test sources reside in ${basedir}/src/test/java
一个是Appliation srouce ,一个是test source。
你会看到在这个工程下有个pom.xml的文件。
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.archetypes</groupId>
<artifactId>my-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>my-app</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
关于POM是一个很重要的概念,会专门花一节时间来学习,这里先过,先看看这个xml文件里面都有些什么东东。
Project:这个元素是所有maven pom.xml文件的第一层
modelVersion :what version of the object model this POM is using
groupId :the unique identifier of the organization or group that created the project
artifactId :unique base name of the primary artifact being generated by this project
packaging :打包的方式,如. JAR, WAR, EAR, etc,默认为jar,大多数情况下不需要修改
version :项目的artifact的版本
name :项目的名字
url :关于这个项目的站点
description:关于你的project的基本描述,会用于你的文档中
编译maven:
使用mvn compile或者直接在eclipse下编译。
编译的结果放在target/classes下面。
POM中有个supper POM,所有的POM都继承于它。
The minimum requirement for a POM are the following:
- project root
- modelVersion - should be set to 4.0.0
- groupId - the id of the project's group.
- artifactId - the id of the artifact (project)
- version - the version of the artifact under the specified group
Here's an example:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>
POM之间可以继承,比如有两个artifact: com.mycompany.app:my-module:1.com.mycompany.app:my-app:1
如果想让
com.mycompany.app:my-module:1的POM继承与com.mycompany.app:my-app:1,则修改
com.mycompany.app:my-module:1的pom xml文件如下:
<project>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-module</artifactId>
<version>1</version>
</project>
前提条件: parent POM位于子POM所在目录的父目录,并且已经装好。
另一种方式是使用project aggression:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<packaging>pom</packaging>
<modules>
<module>my-module</module>
</modules>
</project>
在parent project的module中对子的进行申明。
但如果是这种关系:
.
|-- my-module
| `-- pom.xml
`-- parent
可以使用<relativePath>.../parent/pom.xml</relativePath>来进行引用
`-- pom.xml
也可以在module中使用路径。
<modules>
<module>../my-module</module>
</modules>