第一个简单的maven项目
来源:互联网 发布:linux下修改dns 编辑:程序博客网 时间:2024/05/10 20:27
学习一个新的东西,最快的方式就是实践。所以我们也不用多说什么了,直接拿一个项目来练手。下面的整理取自maven权威指南,在一堆maven资料中,我觉得这本书写的最好。
在你能开始使用Maven做复杂的,多模块的构建之前,我们需要从基础开始。如果你之前用过Maven,你将会注意到这里很好的照顾到了细节。你的构建倾向于“只要能工作”,只有当你需要编写插件来自定义默认行为的时候,才需要深入Maven的细节。另一方面,当你需要深入Maven细节的时候,对Maven核心概念的彻底理解是至关重要的。本章致力于为你介绍一个尽可能简单的Maven项目,然后介绍一些使Maven 成为一个可靠的构建平台的核心概念。看过这个例子之后,你将会对构建生命周期 (build lifecycle),Maven仓库 (repositories),依赖管理 (dependency management)和项目 对象模型 (Project Object Model)有一个基本的理解。
关于详细的POM文件,我后面会专门整理到的。
- 简介
在你能开始使用Maven做复杂的,多模块的构建之前,我们需要从基础开始。如果你之前用过Maven,你将会注意到这里很好的照顾到了细节。你的构建倾向于“只要能工作”,只有当你需要编写插件来自定义默认行为的时候,才需要深入Maven的细节。另一方面,当你需要深入Maven细节的时候,对Maven核心概念的彻底理解是至关重要的。本章致力于为你介绍一个尽可能简单的Maven项目,然后介绍一些使Maven 成为一个可靠的构建平台的核心概念。看过这个例子之后,你将会对构建生命周期 (build lifecycle),Maven仓库 (repositories),依赖管理 (dependency management)和项目 对象模型 (Project Object Model)有一个基本的理解。
- 1,创建一个简单的项目
在这里我使用eclipse来生成一个项目,然后修改POM的文件的默认依赖,然后修改项目的目录文件,形成一个新的项目。OK,下面是该项目的项目结构,这里也贴出POM文件。
<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.linkinpark.maven</groupId><artifactId>linkinMaven</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>linkinMaven</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>4.12</version><scope>test</scope></dependency><dependency><groupId>org.hamcrest</groupId><artifactId>hamcrest-junit</artifactId><version>2.0.0.0</version><scope>test</scope></dependency></dependencies></project>
- 2,构建一个简单的项目
这里我们暂时写2个简单的Java文件,一个是源文件,一个是测试文件,下面是代码。
package org.linkinpark.maven.linkinMaven;public class HelloLinkin{public static void main(String[] args){System.out.println("hello,LinkinPark。。。");}}
package org.linkinpark.maven.linkinMaven;import org.junit.Assert;import org.junit.Test;public class HelloTest{@Testpublic void test(){Assert.assertEquals("字符串的结果应该是LinkinPark", "LinkinPark", "LinkinPark");}}
写完代码之后我们在POM文件上右键然后run as maven install,控制台输出如下:
最后的结果build success,OK,没有问题。注意的是上面的测试tests命令之后的install,控制台明确的告诉了maven默认将我们编译过后生成的这个jar包放到了什么位置上面。
[INFO] Scanning for projects...[INFO] [INFO] ------------------------------------------------------------------------[INFO] Building linkinMaven 0.0.1-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ linkinMaven ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] Copying 0 resource[INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ linkinMaven ---[INFO] Nothing to compile - all classes are up to date[INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ linkinMaven ---[INFO] Using 'UTF-8' encoding to copy filtered resources.[INFO] Copying 0 resource[INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ linkinMaven ---[INFO] Nothing to compile - all classes are up to date[INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ linkinMaven ---[INFO] Surefire report directory: /Users/LinkinPark/WorkSpace/linkinMaven/target/surefire-reports------------------------------------------------------- T E S T S-------------------------------------------------------Running org.linkinpark.maven.linkinMaven.HelloTestTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.052 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ linkinMaven ---[INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ linkinMaven ---[INFO] Installing /Users/LinkinPark/WorkSpace/linkinMaven/target/linkinMaven-0.0.1-SNAPSHOT.jar to /Users/LinkinPark/repository/org/linkinpark/maven/linkinMaven/0.0.1-SNAPSHOT/linkinMaven-0.0.1-SNAPSHOT.jar[INFO] Installing /Users/LinkinPark/WorkSpace/linkinMaven/pom.xml to /Users/LinkinPark/repository/org/linkinpark/maven/linkinMaven/0.0.1-SNAPSHOT/linkinMaven-0.0.1-SNAPSHOT.pom[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 1.493 s[INFO] Finished at: 2016-02-17T00:21:24+08:00[INFO] Final Memory: 10M/309M[INFO] ------------------------------------------------------------------------
最后的结果build success,OK,没有问题。注意的是上面的测试tests命令之后的install,控制台明确的告诉了maven默认将我们编译过后生成的这个jar包放到了什么位置上面。
我们切到maven给我们安装的jar所在文件夹下,使用Java -cp命令来执行jar包看下效果:
- 3,POM文件的说明
在前面第一点上我已经贴出了POM文件。这个pom.xml文件是你将会面对的Maven项目中最基础的POM,一般来说一个POM文件会复杂得多:定义多个依赖,自定义插件行为。最开始的几个元素——groupId,artifactId, packaging, version——是Maven的坐标(coordinates),它们唯一标识了一个项目。name和url是POM提供的描述性元素,它们给人提供了可阅读的名字,将一个项目关联到了项目web站点。最后,dependencies元素定义了一个单独的,测试范围(test-scoped)依赖,依赖于称为JUnit的单元测试框架。关于POM,记住最重要的一句话,pom.xml是一个让Maven跑起来的文件。
当Maven运行的时候,它是根据项目的pom.xml里设置的组合来运行的,一个最上级的POM定义了Maven的安装目录,在这个目录中全局的默认值被定义了,(可能)还有一些用户定义的设置。想要看这个“有效的 (effective)”POM,或者说Maven真正运行根据的POM,在simple项目的基础目录下跑下面的命令,
mvn help:effective-pom
一旦你运行了此命令,你应该能看到一个大得多的POM,它暴露了Maven的默认设置。这里我贴出一份有效的pom文件,也就是默认的那个pom文件。ffective POMs, after inheritance, interpolation, and profiles are applied:<!-- ====================================================================== --><!-- --><!-- Generated by Maven Help Plugin on 2016-02-16T06:14:54 --><!-- See: http://maven.apache.org/plugins/maven-help-plugin/ --><!-- --><!-- ====================================================================== --><!-- ====================================================================== --><!-- --><!-- Effective POM for project --><!-- 'org.linkinpark.maven:linkinMaven:jar:0.0.1-SNAPSHOT' --><!-- --><!-- ====================================================================== --><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.linkinpark.maven</groupId> <artifactId>linkinMaven</artifactId> <version>0.0.1-SNAPSHOT</version> <name>linkinMaven</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>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-junit</artifactId> <version>2.0.0.0</version> <scope>test</scope> </dependency> </dependencies> <repositories> <repository> <snapshots> <enabled>false</enabled> </snapshots> <id>central</id> <name>Central Repository</name> <url>https://repo.maven.apache.org/maven2</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <releases> <updatePolicy>never</updatePolicy> </releases> <snapshots> <enabled>false</enabled> </snapshots> <id>central</id> <name>Central Repository</name> <url>https://repo.maven.apache.org/maven2</url> </pluginRepository> </pluginRepositories> <build> <sourceDirectory>/Users/LinkinPark/WorkSpace/linkinMaven/src/main/java</sourceDirectory> <scriptSourceDirectory>/Users/LinkinPark/WorkSpace/linkinMaven/src/main/scripts</scriptSourceDirectory> <testSourceDirectory>/Users/LinkinPark/WorkSpace/linkinMaven/src/test/java</testSourceDirectory> <outputDirectory>/Users/LinkinPark/WorkSpace/linkinMaven/target/classes</outputDirectory> <testOutputDirectory>/Users/LinkinPark/WorkSpace/linkinMaven/target/test-classes</testOutputDirectory> <resources> <resource> <directory>/Users/LinkinPark/WorkSpace/linkinMaven/src/main/resources</directory> </resource> </resources> <testResources> <testResource> <directory>/Users/LinkinPark/WorkSpace/linkinMaven/src/test/resources</directory> </testResource> </testResources> <directory>/Users/LinkinPark/WorkSpace/linkinMaven/target</directory> <finalName>linkinMaven-0.0.1-SNAPSHOT</finalName> <pluginManagement> <plugins> <plugin> <artifactId>maven-antrun-plugin</artifactId> <version>1.3</version> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> </plugin> <plugin> <artifactId>maven-dependency-plugin</artifactId> <version>2.8</version> </plugin> <plugin> <artifactId>maven-release-plugin</artifactId> <version>2.3.2</version> </plugin> </plugins> </pluginManagement> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>2.5</version> <executions> <execution> <id>default-clean</id> <phase>clean</phase> <goals> <goal>clean</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>default-testResources</id> <phase>process-test-resources</phase> <goals> <goal>testResources</goal> </goals> </execution> <execution> <id>default-resources</id> <phase>process-resources</phase> <goals> <goal>resources</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <executions> <execution> <id>default-jar</id> <phase>package</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <executions> <execution> <id>default-compile</id> <phase>compile</phase> <goals> <goal>compile</goal> </goals> </execution> <execution> <id>default-testCompile</id> <phase>test-compile</phase> <goals> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.12.4</version> <executions> <execution> <id>default-test</id> <phase>test</phase> <goals> <goal>test</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.4</version> <executions> <execution> <id>default-install</id> <phase>install</phase> <goals> <goal>install</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.7</version> <executions> <execution> <id>default-deploy</id> <phase>deploy</phase> <goals> <goal>deploy</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-site-plugin</artifactId> <version>3.3</version> <executions> <execution> <id>default-site</id> <phase>site</phase> <goals> <goal>site</goal> </goals> <configuration> <outputDirectory>/Users/LinkinPark/WorkSpace/linkinMaven/target/site</outputDirectory> <reportPlugins> <reportPlugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> </reportPlugin> </reportPlugins> </configuration> </execution> <execution> <id>default-deploy</id> <phase>site-deploy</phase> <goals> <goal>deploy</goal> </goals> <configuration> <outputDirectory>/Users/LinkinPark/WorkSpace/linkinMaven/target/site</outputDirectory> <reportPlugins> <reportPlugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> </reportPlugin> </reportPlugins> </configuration> </execution> </executions> <configuration> <outputDirectory>/Users/LinkinPark/WorkSpace/linkinMaven/target/site</outputDirectory> <reportPlugins> <reportPlugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> </reportPlugin> </reportPlugins> </configuration> </plugin> </plugins> </build> <reporting> <outputDirectory>/Users/LinkinPark/WorkSpace/linkinMaven/target/site</outputDirectory> </reporting></project>
关于详细的POM文件,我后面会专门整理到的。
0 0
- 第一个简单的maven项目
- 第一个Maven 项目
- 第一个maven项目
- 第一个项目的简单感受
- maven 第一个项目HelloWord
- 第一个maven+hadoop项目
- maven构建第一个项目
- 创建第一个Maven项目
- 第一个maven web项目
- 用maven创建第一个maven项目
- Maven1_构建自己的第一个maven项目
- 使用eclipse创建第一个maven管理的web项目
- 2.Maven特点,Maven约定,建立第一个Maven项目
- 用Maven创建第一个web项目
- 用Maven创建第一个web项目
- 第一个项目(Maven学习3)
- 用Maven创建第一个web项目
- 用Maven创建第一个web项目
- Python 各种测试框架简介
- Atitit.图片木马的原理与防范 attilax 总结
- 新建项目或者修改后运行旁app出现红叉
- 判断两矩形是否相交
- c语言笔试题总结2
- 第一个简单的maven项目
- 且行且珍惜,还在coder的人们
- OpenGL ES 学习教程(十) Light casters 之 Directional Light (平行光)
- 心理学
- 决策树分类算法(监督式)
- 使用QEMU创建虚拟机
- 4.14-3 Android 6.0 请求权限
- bzoj2705 [SDOI2012]Longge的问题
- Java RSA 加密解密算法 入门