maven的变量
maven定义了很多变量属性,参考这里 http://docs.codehaus.org/display/MAVENUSER/MavenPropertiesGuide
- 内置属性
- ${basedir } represents the directory containing pom.xml
- ${version } equivalent to ${project.version } or ${pom.version }
- Pom/Project properties
所有pom中的元素都可以用 project. 前缀进行引用,以下是部分常用的- ${project.build.directory } results in the path to your "target" dir, this is the same as${pom.project.build.directory }
- ${project.build. outputD irectory } results in the path to your "target/classes" dir
- ${project.name } refers to the name of the project.
- ${project.version } refers to the version of the project.
- ${project.build.finalName } refers to the final name of the file created when the built project is packaged
- 本地用户设定
所有用的的 settings.xml 中的设定都可以通过 settings. 前缀进行引用- ${settings.localRepository } refers to the path of the user's local repository.
- ${maven.repo.local } also works for backward compatibility with maven1 ??
- 环境变量
系统的环境变量通过 env. 前缀引用- ${env.M2_HOME } returns the Maven2 installation path.
- ${java.home } specifies the path to the current JRE_HOME environment use with relative paths to get for example:
<jvm>${java.home}../bin/java.exe</jvm>
- java系统属性
所有JVM中定义的java系统属性. - 用户在pom中定义的自定义属性
<project> ... <properties> <my.filter.value>hello</my.filter.value> </properties> ... </project>
- ${my.filter.value } will result in hello if you inserted the above XML fragment in your pom.xml
- 上级工程的变量
上级工程的pom中的变量用前缀 ${project.parent } 引用. 上级工程的版本也可以这样引用: ${parent.version }.
maven的使用
我们已经知道maven预定义了许多的阶段(phase),每个插件都依附于这些阶段,并且在进入某个阶段的时候,调用运行这些相关插件的功能。我们先来看完整的maven生命周期:
生命周期 阶段描述validate | 验证项目是否正确,以及所有为了完整构建必要的信息是否可用 |
generate-sources | 生成所有需要包含在编译过程中的源代码 |
process-sources | 处理源代码,比如过滤一些值 |
generate-resources | 生成所有需要包含在打包过程中的资源文件 |
process-resources | 复制并处理资源文件至目标目录,准备打包 |
compile | 编译项目的源代码 |
process-classes | 后处理编译生成的文件,例如对Java类进行字节码增强(bytecode enhancement) |
generate-test-sources | 生成所有包含在测试编译过程中的测试源码 |
process-test-sources | 处理测试源码,比如过滤一些值 |
generate-test-resources | 生成测试需要的资源文件 |
process-test-resources | 复制并处理测试资源文件至测试目标目录 |
test-compile | 编译测试源码至测试目标目录 |
test | 使用合适的单元测试框架运行测试。这些测试应该不需要代码被打包或发布 |
prepare-package | 在真正的打包之前,执行一些准备打包必要的操作。这通常会产生一个包的展开的处理过的版本(将会在Maven 2.1+中实现) |
package | 将编译好的代码打包成可分发的格式,如JAR,WAR,或者EAR |
pre-integration-test | 执行一些在集成测试运行之前需要的动作。如建立集成测试需要的环境 |
integration-test | 如果有必要的话,处理包并发布至集成测试可以运行的环境 |
post-integration-test | 执行一些在集成测试运行之后需要的动作。如清理集成测试环境。 |
verify | 执行所有检查,验证包是有效的,符合质量规范 |
install | 安装包至本地仓库,以备本地的其它项目作为依赖使用 |
deploy | 复制最终的包至远程仓库,共享给其它开发人员和项目(通常和一次正式的发布相关) |
变量
问:如何使用变量替换?项目中的某个配置文件比如jdbc.properties使用了一些pom中的变量,如何在发布中使用包含真实内容的最终结果文件?答:使用资源过滤功能,比如:
<project>
..
<properties>
<jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>
<jdbc.url>jdbc:mysql://localhost:3306/development_db</jdbc.url>
<jdbc.username>dev_user</jdbc.username>
<jdbc.password>s3cr3tw0rd</jdbc.password>
</properties>
..
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
..
<profiles>
<profile>
<id>production</id>
<properties>
<jdbc.driverClassName>oracle.jdbc.driver.OracleDriver</jdbc.driverClassName>
<jdbc.url>jdbc:oracle:thin:@proddb01:1521:PROD</jdbc.url>
<jdbc.username>prod_user</jdbc.username>
<jdbc.password>s00p3rs3cr3t</jdbc.password>
</properties>
</profile>
</profiles>
</project>
答: maven-svn-revision-number-plugin 可以从 SVN 中获取版本号,并将其变成环境变量,交由其他插件或者profile使用,详细帮助在这里 。一般和resource的filter机制同时使用
<plugins>
<plugin>
<groupId>com.google.code.maven-svn-revision-number-plugin</groupId>
<artifactId>maven-svn-revision-number-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<entries>
<entry>
<prefix>prefix</prefix>
</entry>
</entries>
</configuration>
</plugin>
</plugins>
这段代码负责把resource文件中的内容替换成适当内容
repository = ${prefix.repository}
path = ${prefix.path}
revision = ${prefix.revision}
mixedRevisions = ${prefix.mixedRevisions}
committedRevision = ${prefix.committedRevision}
status = ${prefix.status}
specialStatus = ${prefix.specialStatus}