手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Maven 5 分钟入门教程

[日期:2012-01-31] 来源:Linux社区  作者:ieqq [字体:大 中 小]
  • POM配置文件
  • Pom.xml是工程Maven配置文件的核心,它是个包含了主要配置信息的单一文件。通过这个配置文件,你可以安装你想要的方式来编译整个工程。它的复杂性确实有点望而生畏,所以你将来需要花时间去慢慢熟悉它,这样你的工作才能更有效率。
    Xml代码
    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    3.   <modelVersion>4.0.0</modelVersion>  
    4.   <groupId>com.mycompany.app</groupId>  
    5.   <artifactId>my-app</artifactId>  
    6.   <packaging>jar</packaging>  
    7.   <version>1.0-SNAPSHOT</version>  
    8.   <name>Maven Quick Start Archetype</name>  
    9.   <url>http://maven.apache.org</url>  
    10.   <dependencies>  
    11.     <dependency>  
    12.       <groupId>junit</groupId>  
    13.       <artifactId>junit</artifactId>  
    14.       <version>4.8.2</version>  
    15.       <scope>test</scope>  
    16.     </dependency>  
    17.   </dependencies>  
    18. </project>  
  • 刚才这个命令行做了哪些事情?
  • 这个命令行执行了一个Maven的目标任务(goal) archetype:generate, 我们传了很多参数给这个目标任务。前缀archetype 表示一个插件. 插件封装了很多目标任务(goal)。这个概念跟Ant里面的类似。你可以到Apache官网了解更多关于插件的信息。http://maven.apache.org/plugins/index.html这里提供了各种各样的插件来满足不同的需要。每个都够你折腾一段时间。
  • Build the Project
  • Windows command代码
    1. cd my-app   
    2. mvn package  

    执行这个指令是帮你把项目打包,当然所以打包依赖的任务也都会自动被执行到。这根Ant大同小异。 你可以在控制台看到这样的信息。
    Console output代码
    1.  ...   
    2. [INFO] ------------------------------------------------------------------------   
    3. [INFO] BUILD SUCCESSFUL   
    4. [INFO] ------------------------------------------------------------------------   
    5. [INFO] Total time: 2 seconds   
    6. [INFO] Finished at: Thu Jul 07 21:34:52 CEST 2011  
    7. [INFO] Final Memory: 3M/6M   
    8. [INFO] ------------------------------------------------------------------------  

    和前面你执行的那个长长的命令行不同(archetype:generate),你可能已经注意到这个命令行很短,后面跟了个参数 package,这个参数和前面不一样,它不是什么插件,也不是什么目标任务。Maven给它起了个新名字(phase),我们暂时翻译成状态。可以理解为项目的一中状态。当你输入这个指令后,Maven帮你做了下面这些事情:
    • 1.validate
    • 2.generate-sources
    • 3.process-sources
    • 4.generate-resources
    • 5.process-resources
    • 6.compile

    执行 Tree . 命令,你可以看到它生存了新的目录。
    Console output代码
    1. ├─src   
    2. │  ├─main   
    3. │  │  └─Java   
    4. │  │      └─com   
    5. │  │          └─mycompany   
    6. │  │              └─app   
    7. │  └─test   
    8. │      └─java   
    9. │          └─com   
    10. │              └─mycompany   
    11. │                  └─app   
    12. └─target   
    13.     ├─classes   
    14.     │  └─com   
    15.     │      └─mycompany   
    16.     │          └─app   
    17.     ├─maven-archiver   
    18.     ├─surefire-reports   
    19.     └─test-classes   
    20.         └─com   
    21.             └─mycompany   
    22.                 └─app  

    其中还打了个jar包 my-app-1.0-SNAPSHOT.jar
  • Maven Phases(Maven 预定义指令)
  • 下面列举了Maven最常用的一些指令
    • validate: validate the project is correct and all necessary information is available
    • compile: compile the source code of the project
    • test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
    • package: take the compiled code and package it in its distributable format, such as a JAR.
    • integration-test: process and deploy the package if necessary into an environment where integration tests can be run
    • verify: run any checks to verify the package is valid and meets quality criteria
    • install: install the package into the local repository, for use as a dependency in other projects locally
    • deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
    • clean: cleans up artifacts created by prior builds
    • site: generates site documentation for this project

    对于上面的这些指令,我们建议你每个都执行一下,看看什么效果。对你了解Maven的功能很有帮助。
  • 生成你的项目站点
  • Windows command代码
    1. mvn site  

    这个比较有意思,会帮你的Project生存一个站点。
    This phase generates a site based upon information on the project's pom. You can look at the documentation generated under target/site.
    如果你有兴趣的话,可以到官网了解更多的信息。
    http://maven.apache.org/guides/getting-started/index.html linux
    相关资讯       Maven 
    本文评论   查看全部评论 (0)
    表情: 表情 姓名: 字数

           

    评论声明
    • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
    • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
    • 本站管理人员有权保留或删除其管辖留言中的任意内容
    • 本站有权在网站内转载或引用您的评论
    • 参与本评论即表明您已经阅读并接受上述条款