Maven构建项目(Building Java Projects with Maven)
本手册带领你用maven构建java项目
你将构建?
你可以构建并编译事先提供给你的java项目。
你需要准备:
- 15分钟
-
你喜欢的文本编辑器或集成开发工具(IDE)
-
JDK 6 或更新版本的JDK
如何使用本手册
和其它Spring的入门指南(Getting Started guides)一样,你可以浏览并完成每一个步骤,或者跳过你熟悉的基本安装步骤, 无论哪种方式,你都可以完成你的编码并结束本教程的学习。
从头开始,跳转到“建立项目”(Set up the project).
跳过基础知识, 完成以下步骤:
如果你完成此步骤, 你可以在gs-maven/complete
.代码中检查你的结果
建立项目(Set up the project)
首先,你需要创建一个java项目以便通过maven的来进行编译. 为了把重点放到maven, 现在我们使创建的项目尽可能简单。
创建一个目录结构
在你创建的项目根目录文件夹下,创建以下结构的子目录; 如, 在linux系统可用以下指令创建mkdir -p src/main/java/hello
:
└── src
└── main
└── java
└── hello
在src/main/java/hello
路径下,你可以创建任何你想要的java类. 为了和本指南的其余部分保持一致, 创建以下两个类:HelloWorld.java
和Greeter.java
.
src/main/java/hello/HelloWorld.java
package hello; public class HelloWorld { public static void main(String[] args) { Greeter greeter = new Greeter(); System.out.println(greeter.sayHello()); }}
src/main/java/hello/Greeter.java
package hello; public class Greeter { public String sayHello() { return "Hello world!"; }}
现在,你已经准备好了用maven进行构建的java项目, 下一个步骤就是安装Maven。
从这 http://maven.apache.org/download.cgi. 下载Maven,只需要下载二进制文件, 所以只需要找到 apache-maven-{version}-bin.zip(windows系统下)的链接或apache-maven-{version}-bin.tar.gz(Linux系统下)
下载后将其解压到你电脑的某个目录下. 然后将bin目录添加到环境变量的 path路径下。
Windows 2000/XP
- Unzip the distribution archive, i.e. apache-maven-3.1.1-bin.zip to the directory you wish to install Maven 3.1.1. These instructions assume you chose C:\Program Files\Apache Software Foundation. The subdirectory apache-maven-3.1.1 will be created from the archive.
- Add the M2_HOME environment variable by opening up the system properties (WinKey + Pause), selecting the "Advanced" tab, and the "Environment Variables" button, then adding the M2_HOME variable in the user variables with the value C:\Program Files\Apache Software Foundation\apache-maven-3.1.1. Be sure to omit any quotation marks around the path even if it contains spaces. Note: For Maven 2.0.9, also be sure that the M2_HOME doesn't have a '\' as last character.
- In the same dialog, add the M2 environment variable in the user variables with the value %M2_HOME%\bin.
- Optional: In the same dialog, add the MAVEN_OPTS environment variable in the user variables to specify JVM properties, e.g. the value -Xms256m -Xmx512m. This environment variable can be used to supply extra options to Maven.
- In the same dialog, update/create the Path environment variable in the user variables and prepend the value %M2% to add Maven available in the command line.
- In the same dialog, make sure that JAVA_HOME exists in your user variables or in the system variables and it is set to the location of your JDK, e.g. C:\Program Files\Java\jdk1.5.0_02 and that %JAVA_HOME%\bin is in your Path environment variable.
- Open a new command prompt (Winkey + R then type cmd) and run mvn --version to verify that it is correctly installed.
为检测maven是否安装成功,可在命令行下执行以下mvn
命令。
mvn -v
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 07:51:28-0600)
Maven home: /usr/share/maven
Java version: 1.7.0_09, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.8.3", arch: "x86_64", family: "mac"
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 23:22:2
2+0800)
Maven home: D:\professSoftware\apache-maven-3.1.1
Java version: 1.6.0_43, vendor: Sun Microsystems Inc.
Java home: D:\professSoftware\JDKx64\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
E:\IntellijIDEAWorkspace\MavenTestProject>
定义一个简单的Maven构建(Define a simple Maven build)
在项目的根目录下创建 pom.xml并输入以下内容:
<?xml version="1.0" encoding="UTF-8"?><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"> <modelVersion>4.0.0</modelVersion> <groupId>org.springframework.gs</groupId> <artifactId>gs-maven-initial</artifactId> <version>0.1.0</version> <packaging>jar</packaging></project>
除选项 <packaging>
元素外,这可能是一个构建java项目所需的最简单的pom.xml文件.它包含了以下详细的项目配制:
-
<modelVersion>
. POM 模版的重酬(总是 4.0.0). -
<groupId>
. 项目所属的组或组织名称. 通常是逆序的公司域名。 -
<artifactId>
. 用于其它项目依赖的包名(如, JAR 或 WAR 文件的名称). -
<version>
. 项目要构建的版本号 -
<packaging>
- 项目将要被打包的类型.默认是 "jar"用于打包成JAR文件. 使用"war" 用于打成WAR包
When it comes to choosing a versioning scheme, Spring recommends the [semantic versioning](http://semver.org) approach. |
到目前为止,你的最小的、可以执行的Maven项目已经定义完成。
构建java源代码(Build Java code)
通过以下命令编译项目源代码:
mvn compile
你可能不想或不可能直接去使用.class文件,而是希望运行pgcage目标进行打包。
mvn package
package构建目标将会编译你的源代码,执行所有的测试,将源代码打包成 JAR 文件到target 目录下。JAR 文件的名称将依赖于<artifactId>
和<version>
。如,之前给定的最简单的pom.xml 文件,JAR 文件将会命名成 gs-maven-initial-0.1.0.jar.
If you’ve changed the value of <packaging> from "jar" to "war", the result will be a WAR file within the target directory instead of a JAR file. |
Maven 也维护着一个本地依赖关系库 (通常在你home 目录下 a .m2/repository目录) 以便快速地访问项目的依赖关系. 如果你想将打包好的项目jar文件安装到本地依赖库中,你应该调用 install
构建目标:
mvn install
install 构建目标编译、测试、打包你的项目源代码并且将它复制到本地依赖库,为其它的项目引用它作为依赖。
说到依赖关系, 现在到了在Maven构建中声明依赖关系的时候了.
声明依赖关系(Declare Dependencies)
简单的Hello World程序完全是独立的,没有依赖任何其它类库。然而,大部分的应用程序,需要依赖其它的类库以处理共同的和复杂的问题。
例如, 假设有saying "Hello World!"的别处一种实现方式,你需要应用程序打印当前日期和时间,虽然你可以用本地的java类库(jdk)来实现日期和时间的显示,但为了让程序变得更有趣,你可能想通过调用 Joda Time libraries来实现
首先,更改你的 HelloWorld.java如下:
package hello; import org.joda.time.LocalTime; public class HelloWorld { public static void main(String[] args) { LocalTime currentTime = new LocalTime(); System.out.println("The current local time is: " + currentTime); Greeter greeter = new Greeter(); System.out.println(greeter.sayHello()); }}
这里的 HelloWorld
使用了Joda Time 的 LocalTime
类来获得和打印当前时间。
如果你现在想通过运行 mvn compile
来编译项目,编译将失败,因为你声明 Joda Time 作为编译时所需要的依赖.你可以通过在pom.xml 文件中加入以下代码来修复(加到 <project>
标签内):
<dependencies> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.2</version> </dependency></dependencies>
这部分的 XML 代码块声明了一系列的项目依赖,特别地,这只声明了一个Joda Time的依赖。在<dependency>
标签内,这依赖有三个子元素构成。
-
<groupId>
-项目所属的组或组织名称. -
<artifactId>
- 要引用的库(包)名称 -
<version>
- 要引用的库(包)的指定版本号
到目前为止,所有的依赖都限定在编译依赖范围内,也就是说,它们在编译时是有效的 (如果你构建的是 WAR 文件,包含了WAR中的 /WEB-INF/libs 文件夹下的文件)。 另外,你可能要用 <scope>
元素来指定以下的范围之一。
-
provided
- 编译项目代码时要求的依赖,但需在运行时被容器提供以运行源代码(e.g., the Java Servlet API)。 -
test
- 编译和运行测试时要求的依赖,但不是在构建或运行项目运行时编码所要求的。
现在如果你运行 mvn compile
或mvn package
,Maven 应当会决定并从Maven的中心库下载 the Joda Time 的依赖,然后成功编译。
这是完整的 pom.xml
文件:
pom.xml
<?xml version="1.0" encoding="UTF-8"?><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"> <modelVersion>4.0.0</modelVersion> <groupId>org.springframework</groupId> <artifactId>gs-maven</artifactId> <packaging>jar</packaging> <version>0.1.0</version> <dependencies> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>hello.HelloWorld</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build></project>