ITeye博客

【转】<maven权威指南>学习笔记

hhhk · 2013-06-06 12:32 · 网摘 · 技术 · Web前端 · Markdown

一些常用的命令 

mvn help:effective-pom 
用来查看当前工程的完整的pom文件, 比如从父类pom以及默认pom继承的内容 

mvn install 
将当前的maven构建(项目打包后的文件)安装到本地仓库 
mvn install -Dmaven.test.skip=true 
跳过测试(同时会跳过test compile) 

mvn deploy 
将当前的maven构建(项目打包后的文件)安装到远程仓库 

mvn archetype:create 
这里的archetype是插件, create是目标(goal) 

profile命令: 
mvn install -DskipTests=true -Penv_test 

生命周期 
resource->compile->process-classes->process-test-resources->test-compile->test->prepare-package->package 

resources:resources 绑定在resource处理阶段, 用来将src/main/resources下或者任何指定其他目录下的文件copy到输出目录中 

resources:testResources 将test下的resources目录或者任何指定其他目录copy到test输出目录下 

compiler:testCompile 将测试类编译(包括copy资源文件) 

surefire:test 运行测试用例 

jar:jar 打jar包 

mvn help:describe -Dplugin=exec -Dfull 
查看插件的doc文档 

查看依赖 
mvn dependency:resolve 
查看项目依赖情况 

mvn  dependency:tree 
打印出项目的整个依赖树 

mvn dependency:analyze 
帮助你分析依赖关系, 用来取出无用, 重复依赖的好帮手 

mvn install -X 
追踪依赖的完整轨迹 

测试相关 
默认情况下, 遇到测试失败, surefire:test会中断测试, 如果想忽略测试失败, 继续运行后面的测试, 需要这么配置: 

Xml代码  收藏代码

  1. <groupId>org.apache.maven.plugins</groupId>
  2. <artifactId>maven-surefire-plugin</artifactId>
  3. <configuration>
  4. <testFailureIgnore>true</testFailureIgnore>
  5. </configuration>
  6. </plugin>

从doc文档中看到:Expression: ${maven.test.failure.ignore} 
因此也可以从命令行中通过传递参数来实现同样的功能 
mvn test -Dmaven.test.failure.ignore=true 

mvn install -Dmaven.test.skip=true 
跳过单元测试 

要跳过测试, 在pom文件也可以这样配置: 

Xml代码  收藏代码

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-surefire-plugin</artifactId>
  4. <configuration>
  5. <skip>true</skip>
  6. </configuration>
  7. </plugin>

打包输出 
Assembly是一个关于打包输出的插件, 比如这样配置: 

Xml代码  收藏代码

  1. <artifactId>maven-assembly-plugin</artifactId>
  2. <configuration>
  3. <descriptorRefs>
  4. <descriptorRef>jar-with-dependencies</descriptorRef>
  5. </descriptorRefs>
  6. </configuration>
  7. </plugin>

运行命令 
mvn assembly:assembly 
生成xxxx-1.0-jar-with-dependencies.jar的打包文件, 里面将包含所有的依赖文件 

生成项目 
mvn archetype:create 
用来生成项目, artifactId和groupId用来指定目标, archetypeArtifactId通过制定类型, 比如 maven-archetype-webapp用该创建一个web项目 

在maven中使用jetty容器, 我们需要这样配置一个插件: 

Xml代码  收藏代码

  1. <plugin>
  2. <groupId>org.mortbay.jetty</groupId>
  3. <artifactId>maven-jetty-plugin</artifactId>
  4. </plugin>

mvn jetty:run 
用来运行jetty服务器 

build配置和dependencies都会被所有的子模块继承 

依赖管理 
为了去掉依赖重复, 在多模块pom中将加入所有重复的依赖: 

Xml代码  收藏代码

  1. <dependencyManagement>
  2. <dependencies>

为了去掉版本号这样的重复, 可以这样写: 

Xml代码  收藏代码

  1. <properties>
  2. <hibernate.annotations.version>3.3.0.ga</hibernate.annotations.version>
  3. </properties>

这样用: 

Xml代码  收藏代码

  1. <version>${hibernate.annotations.version}</version>

属性引用 
对project属性的引用, 比如这样写: 
org.sonatype.mavenbook-${project.artifactId} 

直接定义属性: 
比如这个属性${foo}=bar, 定义如下: 

Xml代码  收藏代码

  1. <properties>
  2. <foo>bar</foo>
  3. </properties>

可选依赖的配置 

Xml代码  收藏代码

  1. <dependency>
  2. <groupId>net.sf.ehcache</groupId>
  3. <artifactId>ehcache</artifactId>
  4. <version>1.4.1</version>
  5. <optional>true</optional>
  6. </dependency>

这里举的是关于两种缓存方案的依赖配置, 在编译的时候需要, 但是运行的时候可能不需要. 

依赖范围 
compile, test是常用的 
需要接的是运行时依赖, 表示在编译时不需要, 在运行时需要, 例子是driver编译的时候是一个api接口依赖, 运行时就需要一个具体的driver class实例了. 

感觉只要理解compile和test依赖范围即可, a对b是test依赖, b对c是compile依赖, 那么a对c是test依赖, compile对所有直接依赖下的compile范围都是compile传递依赖, 很绕:( 

一个对传递性依赖的排除 

Xml代码  收藏代码

  1. <dependency>
  2. <groupId>org.sonatype.mavenbook</groupId>
  3. <artifactId>project-a</artifactId>
  4. <version>1.0</version>
  5. <exclusions>
  6. <exclusion>
  7. <groupId>org.sonatype.mavenbook</groupId>
  8. <artifactId>project-b</artifactId>
  9. </exclusion>
  10. </exclusions>
  11. </dependency>

表示虽然依赖project-a, 但是不想依赖a所依赖的b 
这里是排除一个依赖, 添加另外一个依赖 

Xml代码  收藏代码

  1. <dependency>
  2. <groupId>org.hibernate</groupId>
  3. <artifactId>hibernate</artifactId>
  4. <version>3.2.5.ga</version>
  5. <exclusions>
  6. <exclusion>
  7. <groupId>javax.transaction</groupId>
  8. <artifactId>jta</artifactId>
  9. </exclusion>
  10. </exclusions>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.apache.geronimo.specs</groupId>
  14. <artifactId>geronimo-jta_1.1_spec</artifactId>
  15. <version>1.1</version>
  16. </dependency>

使用排除依赖, 一般是一个传递依赖在编译的时候需要, 但是在实际运行环境不需要的时候 

dependencyManagement用来在父pom文件中定义公共的依赖, 以及版本号, 是一个集中管理依赖版本的地方 

项目关系 
groupId用.分隔, artifactId用-分隔 

在继承模块中, maven约定父pom在本地仓库, 或者在当前项目的父目录../pom.xml中可用.可以通过relativePath指定具体的父pom.xml的位置, 一般在多模块中与子模块同级存放父pom.xml, 这时就需要通过relativePath来指定pom.xml了. 

从父模块继承要加这样的标签: 

Xml代码  收藏代码

  1. <parent>
  2. <groupId>com.training.killerapp</groupId>
  3. <artifactId>a-parent</artifactId>
  4. <version>1.0-SNAPSHOT</version>
  5. </parent>

将共同的依赖使用一个pom项目统一组织起来, 并让其他的模块添加对该模块的依赖是一个不错的复用机制. 
此时使用的标记是dependencies而不是dependencyManagment了.比如这样写:  

Xml代码  收藏代码

  1. <groupId>org.sonatype.mavenbook</groupId>
  2. <artifactId>persistence-deps</artifactId>
  3. <version>1.0</version>
  4. <packaging>pom</packaging>
  5. </dependencies>
  6. <dependency>
  7. <groupId>mysql</groupId>
  8. <artifactId>mysql-connector-java</artifactId>
  9. <version>${mysqlVersion}</version>
  10. </dependency>
  11. </dependencies>
  12. <properties>
  13. <mysqlVersion>(5.1,)</mysqlVersion>
  14. <properties>

对该pom的依赖需要这样写: 

Xml代码  收藏代码

  1. <dependency>
  2. <groupId>org.sonatype.mavenbook</groupId>
  3. <artifactId>persistence-deps</artifactId>
  4. <version>1.0</version>
  5. <type>pom</type>
  6. </dependency>

多了一个type标记 

一个简单的多模块+继承应用 
server-side包含web-apps和server-lib两个子模块, 并且是二者的父模块, web-apps依赖server-lib模块(里面定义了所有的web-app所要依赖的jar包), web-apps 是个多模块, web-client, web-admin是他的两个子模块, 同时具有父子关系. 这样设计的结果将导致web-client和web-admin的定义将非常小巧, 简洁 
POM继承十分有用,但它可能被滥用。当然在需要共享依赖和配置的时候,父子关联需要被使用。但当两个项目截然不同的时候使用父子关联是不明智的。 

生命周期 
在执行clean之前的pre-clean中指定需要执行的插件目标, 这个配置没看懂:( 

由于生命周期阶段会绑定对应的插件, 因此可以对对应的插件进行定制, 达到定制声明周期的目的 
比如对clean插件的定制: 

Xml代码  收藏代码

  1. <plugin>
  2. <artifactId>maven-clean-plugin</artifactId>
  3. <configuration>
  4. <filesets>
  5. <fileset>
  6. <directory>target-other</directory>
  7. <includes>
  8. <include>*.class</include>
  9. </includes>
  10. </fileset>
  11. </filesets>
  12. </configuration>
  13. </plugin>

资源处理周期 
resources:resources会将src/main/resources下的文件内容复制到target/classes中. 同时还可以在copy之前做一些过滤替换工作. 
关于过滤替换, 如果有这样一个文件: 

Xml代码  收藏代码

  1. <service>
  2. <!-- This URL was set by project version 0.6-SNAPSHOT -->
  3. <url>${jdbc.url}</url>
  4. <user>${jdbc.username}</user>
  5. <password>${jdbc.password}</password>
  6. </service>

属性配置文件如下: 
jdbc.url=jdbc:hsqldb:mem:mydb 
jdbc.username=sa 
jdbc.password= 
过滤默认是不开启的, 需要配置一下: 

Xml代码  收藏代码

  1. <build>
  2. <filters>
  3. <filter>src/main/filters/default.properties</filter>
  4. </filters>
  5. <resources>
  6. <resource>
  7. <directory>src/main/resources</directory>
  8. <filtering>true</filtering>
  9. </resource>
  10. </resources>
  11. </build>

src/main/resources是默认的资源文件目录, 但是也可以指定其他目录: 

Xml代码  收藏代码

  1. <resources>
  2. <resource>
  3. <directory>src/main/resources</directory>
  4. </resource>
  5. <resource>
  6. <directory>src/main/xml</directory>
  7. </resource>
  8. <resource>
  9. <directory>src/main/images</directory>
  10. </resource>
  11. </resources>

对不同的资源进行分门别类的处理, 这样方便我们对不同的资源采用不同的过滤处理, 比如这样写: 

Xml代码  收藏代码

  1. <resource>
  2. <filtering>true</filtering>
  3. <directory>/src/main/command</directory>
  4. <includes>
  5. <include>run.bat</include>
  6. <include>run.sh</include>
  7. </includes>
  8. <targetPath>/</targetPath>

会将指定目录下的run.bat, sun.sh文件复制到指定的目录(targetPath)中 

编译周期 
maven默认的编译(compile:compile)版本是1.3, 运行平台版本是1.1, 现在一般需要配置成1.5以上版本 

Xml代码  收藏代码

  1. <plugin>
  2. <artifactId>maven-compiler-plugin</artifactId>
  3. <configuration>
  4. <source>1.5</source>
  5. <target>1.5</target>
  6. </configuration>
  7. </plugin>

上面是指定插件的版本, 如果要制定目标的版本, 需要将configuration元素放到execution元素下. 
如果你想要存储项目的源码至src/java而非src/main/java,让构建输出至classes而非target/classes,你可以覆盖定义在超级POM中的sourceDirectory的默认值(不推荐这样做, 我们始终应该遵循"约定优于配置"的原则)。 

Xml代码  收藏代码

  1. <build>
  2. ...
  3. <sourceDirectory>src/java</sourceDirectory>
  4. <outputDirectory>classes</outputDirectory>
  5. ...
  6. lt;/build>

Test周期 
绑定的surefire:test, 默认情况下, 如果测试失败, 停止构建, 如果希望继续构建, 需要这样设置: 

Xml代码  收藏代码

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-surefire-plugin</artifactId>
  4. <configuration>
  5. <testFailureIgnore>true</testFailureIgnore>
  6. </configuration>
  7. </plugin>

profile 
通过profile可以针对特定的环境来定制不同的artifact, 比如这样写来覆盖compile插件的默认配置: 

Xml代码  收藏代码

  1. <profiles>#
  2. <profile>
  3. <id>production</id>#
  4. <build>#
  5. <plugins>
  6. <plugin>
  7. <groupId>org.apache.maven.plugins</groupId>
  8. <artifactId>maven-compiler-plugin</artifactId>
  9. <configuration>
  10. <debug>false</debug>#
  11. <optimize>true</optimize>
  12. </configuration>
  13. </plugin>
  14. </plugins>
  15. </build>
  16. </profile>
  17. </profiles>

使用profile的命令行用法: 
mvn clean install -Pproduction -X    
可以根据某种条件激活对应 profile, 比如根据jdk版本包含指定的模块: 

Xml代码  收藏代码

  1. <profile>
  2. <id>jdk16</id>
  3. <activation>
  4. <jdk>1.6</jdk>
  5. </activation>
  6. <modules>
  7. <module>simple-script</module>
  8. </modules>
  9. </profile>

activation元素列出了所有激活profile需要的条件, 比如下面的配置: 

Xml代码  收藏代码

  1. <activation>
  2. <activeByDefault>false</activeByDefault>#
  3. <jdk>1.5</jdk>#
  4. <os>
  5. <name>Windows XP</name>#
  6. <family>Windows</family>
  7. <arch>x86</arch>
  8. <version>5.1.2600</version>
  9. </os>
  10. <property>
  11. <name>mavenVersion</name>#
  12. <value>2.0.5</value>
  13. </property>
  14. <file>
  15. <exists>file2.properties</exists>#
  16. <missing>file1.properties</missing>
  17. </file>
  18. </activation>

通过属性来激活: 

Xml代码  收藏代码

  1. <activation>
  2. <property>
  3. <name>!environment.type</name>
  4. </property>
  5. </activation>

可以将profile从pom.xml中拆分出来, 单独的放在profiles.xml文件中 
除了可以在pom, pom外部设置profile外, 还可以在setting中设置profile, 可以将一些私密的信息设置在自己的settging.xml中 
通过profile指定属性, 比如有一个envClassifier属性: 

Xml代码  收藏代码

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <artifactId>maven-jar-plugin</artifactId>
  5. <configuration>
  6. <classifier>${envClassifier}</classifier>
  7. </configuration>
  8. </plugin>
  9. </plugins>
  10. </build>

那么可以有这样的profile来指定一个envClassifier: 

Xml代码  收藏代码

  1. <profile>
  2. <id>windows</id>
  3. <activation>
  4. <os>
  5. <family>windows</family>
  6. </os>
  7. </activation>
  8. <properties>
  9. <envClassifier>win</envClassifier>
  10. </properties>
  11. </profile>

  
Assembly 
简单的说就是打包方式, 比如jar, ear, war都是不同的Assembly, 比如希望打包的文件中带有源码, api文档等都需要定制Assembly. 
一些Assembly描述符(指定打包包含内容) 
bin:最小打包文件 
jar-with-dependencies:包含所有依赖 
project:包含所有的项目信息, 可以直接构建maven项目, 因此里面会包含pom.xml文件, 如果是个eclipse工程, 还会包含eclipse工程配置文件 
src:包含源码 
将项目打包一份儿, 发给其他人的命令: 
mvn -DdescriptorId=project assembly:single 
配置一个将一个工程打包成一个可运行的jar文件(跟package阶段绑定): 

Xml代码  收藏代码

  1. <plugin>
  2. <artifactId>maven-assembly-plugin</artifactId>
  3. <version>2.2-beta-2</version>
  4. <executions>
  5. <execution>
  6. <id>create-executable-jar</id>
  7. <phase>package</phase>
  8. <goals>
  9. <goal>single</goal>
  10. </goals>
  11. <configuration>
  12. <descriptorRefs>
  13. <descriptorRef>
  14. jar-with-dependencies
  15. </descriptorRef>
  16. </descriptorRefs>
  17. <archive>
  18. <manifest>
  19. <mainClass>org.sonatype.mavenbook.App</mainClass>
  20. </manifest>
  21. </archive>
  22. </configuration>
  23. </execution>
  24. </executions>
  25. </plugin>

写在顶层pom中pluginManagement(dependencyManagement)标签下的配置只是定义, 并不会被使用.子模块中需要显式的激活, 比如这样写: 

Xml代码  收藏代码

  1. <pluginManagement>
  2. <plugins>
  3. <plugin>
  4. <artifactId>maven-assembly-plugin</artifactId>
  5. <version>2.2-beta-2</version>
  6. <executions>
  7. <execution>
  8. <id>create-project-bundle</id>
  9. <phase>package</phase>
  10. <goals>
  11. <goal>single</goal>
  12. </goals>
  13. <configuration>
  14. <descriptorRefs>
  15. <descriptorRef>project</descriptorRef>
  16. </descriptorRefs>
  17. </configuration>
  18. </execution>
  19. </executions>
  20. </plugin>
  21. </plugins>
  22. </pluginManagement>

子模块的激活配置: 

Xml代码  收藏代码

  1. <plugins>
  2. <plugin>
  3. <artifactId>maven-assembly-plugin</artifactId>
  4. </plugin>
  5. </plugins>

maven插件 
一个插件包含一个描述符和多个mojo, 可以将mojo理解为目标, 比如compile:compile对应的是compile插件的CompileMojo类 
一个描述符用来告诉maven, 一个插件的各种配置他在jar文件中的META-INFO/maven/plugin.xml, 当maven加载一个插件的时候, 它首选读取该描述文件, 然后去找Mojo以及资源等信息.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。