Maven学习笔记(三)-maven移植和属性过滤

来源:互联网 发布:h5小游戏源码下载 编辑:程序博客网 时间:2024/06/12 23:50
maven移植-级别
不可移植
环境可移植
组织内部可移植

广义可移植


Maven移植-Profiles作用是:Profile允许你为移植或特殊的需要,自定义一个特殊的构建。


1、maven移植-Profiles位于pom.xml

Profile可以覆盖几乎所有的pom元素

[html] view plain copy
 print?
  1. <profile>  
  2.     <build>  
  3.         <defaultGoal>...</defaultGoal>  
  4.         <finalName>...</finalName>  
  5.         <resources>...</resources>  
  6.         <testResources>...</testResources>  
  7.         <plugins>...</plugins>  
  8.     </build>  
  9.     <reporting>...</reporting>  
  10.     <modules>...</modules>  
  11.     <dependencies>...</dependencies>  
  12.     <dependencyManagement>...</dependencyManagement>  
  13.     <distributionManagement>...</distributionManagement>  
  14.     <repositories>...</repositories>  
  15.     <pluginRepositories>...</pluginRepositories>  
  16.     <properties>...<properties>  
  17. </profile>  


2、maven移植-Profiles位于profiles.xml

Profile抽取至profiles.xml文件中,将这种profile称为外部profile。注:maven3以后不再支持


3、maven移植-Profiles位于setting.xml

在setting.xml文件中配置profiles,会在所有的项目上生效。


4、maven移植-Profiles(激活方式)

a、通过调用id激活:mvn install -Pprofile-id

b、通过<activation>条件匹配激活

[html] view plain copy
 print?
  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.     <file>  
  11.         <exists>file2.properties</exists>  
  12.         <missing>file1.properties</missing>  
  13.     </file>  
  14. </activation>  

[html] view plain copy
 print?
  1. <activation>  
  2.     <activeByDefault>false</activeByDefault>  
  3.     <property>  
  4.         <name>mavenVersion</name>  
  5.         <value>2.0.5</value>  
  6.     </property>  
  7.     <!-- 是不是不存在这个环境变量的类型 -->  
  8.     <property>  
  9.         <name>!environment.type</name>  
  10.     </property>  
  11. </activation>  

c、通过activeByDefault默认激活

[html] view plain copy
 print?
  1. <profile>  
  2.     <activation>  
  3.         <!-- 默认激活 -->  
  4.         <activeByDefault>true</activeByDefault>  
  5.     </activation>  
  6. </profile>  

d、注意:只适用于settings.xml文件中激活profile,该设置只会激活settings profile,不会激活id匹配的pom.xml中的profile

[html] view plain copy
 print?
  1. <settings>  
  2.     <activeProfiles>  
  3.         <activeProfile>profile-id</activeProfile>  
  4.     </activeProfiles>  
  5. </settings>  


------------------------------------------------------------------------------------------------------------------

************************************************************************************************************

------------------------------------------------------------------------------------------------------------------



实例:

需求:如何做到对所有的项目,开发环境和生产环境的数据库配置连接不同?

下面是原pom.xml

[html] view plain copy
 print?
  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.   
  5.   <groupId>com.test.maven</groupId>  
  6.   <artifactId>test1</artifactId>  
  7.   <version>1.0-SNAPSHOT</version>  
  8.   <packaging>jar</packaging>  
  9.   
  10.   <name>test1</name>  
  11.   <url>http://maven.apache.org</url>  
  12.   
  13.   <properties>  
  14.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.   </properties>  
  16.   
  17.   <dependencies>  
  18.     <dependency>  
  19.         <groupId>junit</groupId>  
  20.         <artifactId>junit</artifactId>  
  21.         <version>3.8.1</version>  
  22.         <scope>test</scope>  
  23.     </dependency>  
  24.     <dependency>  
  25.         <groupId>org.springframework</groupId>  
  26.         <artifactId>spring-beans</artifactId>  
  27.         <version>3.2.0.RELEASE</version>  
  28.     </dependency>  
  29. </dependencies>  
  30.   
  31.   
  32.     <build>  
  33.         <plugins>  
  34.             <plugin>  
  35.                 <artifactId>maven-surefire-plugin</artifactId>  
  36.                 <version>2.7.1</version>  
  37.                 <configuration>  
  38.                     <skip>true</skip>  
  39.                 </configuration>  
  40.             </plugin>  
  41.         </plugins>  
  42.     </build>  
  43.       
  44.       
  45.     <profiles>  
  46.         <profile>  
  47.             <id>runTest</id>  
  48.             <!-- 激活profile -->  
  49.             <activation>  
  50.                 <activeByDefault>true</activeByDefault>  
  51.             </activation>  
  52.             <!-- 下面的build是覆盖上面的build从而达到控制是否跳过测试 -->  
  53.             <build>  
  54.                 <plugins>  
  55.                     <plugin>  
  56.                         <artifactId>maven-surefire-plugin</artifactId>  
  57.                         <version>2.7.1</version>  
  58.                         <configuration>  
  59.                             <skip>false</skip>  
  60.                         </configuration>  
  61.                     </plugin>  
  62.                 </plugins>  
  63.             </build>  
  64.         </profile>  
  65.     </profiles>    
  66. </project>  


把上面的build标签中加入resources标签,制定路径以及是否过滤。

[html] view plain copy
 print?
  1. <build>  
  2.         <!-- src/main/resources路径下的进行属性过滤 -->  
  3.         <resources>  
  4.             <resource>  
  5.                 <directory>src/main/resources</directory>  
  6.                 <filtering>true</filtering>  
  7.             </resource>  
  8.         </resources>  
  9.         <plugins>  
  10.             <plugin>  
  11.                 <artifactId>maven-surefire-plugin</artifactId>  
  12.                 <version>2.7.1</version>  
  13.                 <configuration>  
  14.                     <skip>true</skip>  
  15.                 </configuration>  
  16.             </plugin>  
  17.         </plugins>  
  18.     </build>  


项目结构图:



db.properties内容

[html] view plain copy
 print?
  1. driverClassName=${jdbc.driverClassName}  
  2. url=${jdbc.url}  
  3. username=${jdbc.username}  
  4. password=${jdbc.password}  
  5. property1=${property1}  

在pom.xml中的profiles中加入以下profile

[html] view plain copy
 print?
  1. <!-- 在setting.xml中配置environment.type,为dev时激活oracle的配置,为prod时激活mysql的配置 -->  
  2.     <profile>  
  3.         <id>development</id>  
  4.         <activation>  
  5.             <property>  
  6.                 <name>environment.type</name>  
  7.                 <value>dev</value>  
  8.             </property>  
  9.         </activation>  
  10.         <properties>  
  11.             <jdbc.driverClassName>oracle.jdbc.driver.OracleDriver</jdbc.driverClassName>  
  12.             <jdbc.url>jdbc:oracle:thin:@proddb01:1521:DEV</jdbc.url>  
  13.             <jdbc.username>dev_user</jdbc.username>  
  14.             <jdbc.password>devpass</jdbc.password>  
  15.         </properties>  
  16.     </profile>  
  17.   
  18.     <profile>  
  19.         <id>production</id>  
  20.         <activation>  
  21.             <property>  
  22.                 <name>environment.type</name>  
  23.                 <value>prod</value>  
  24.             </property>  
  25.         </activation>  
  26.         <properties>  
  27.             <jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName>  
  28.             <jdbc.url>jdbc:mysql://localhost:3306/production_db</jdbc.url>  
  29.             <jdbc.username>prod_user</jdbc.username>  
  30.             <jdbc.password>prodpass</jdbc.password>  
  31.         </properties>  
  32.     </profile>  


在settings.xml中的profiles中加入:

[html] view plain copy
 print?
  1. <profile>  
  2.         <id>environment</id>  
  3.         <activation>  
  4.             <activeByDefault>true</activeByDefault>  
  5.         </activation>  
  6.         <properties>  
  7.             <environment.type>dev</environment.type>  
  8.             <property1>2</property1>  
  9.         </properties>  
  10.     </profile>  
  11.   
  12.     <profile>  
  13.         <id>product</id>  
  14.         <activation>  
  15.             <property>  
  16.                 <name>environment.type</name>  
  17.                 <value>prod</value>  
  18.             </property>  
  19.         </activation>  
  20.         <properties>  
  21.             <database.password>sdfsdf</database.password>  
  22.             <property1>3</property1>  
  23.         </properties>  
  24.     </profile>  


执行mvn clean install,打开target下的classes中的db.properties





可以看到db.properties被改变了。


如果生产环境中的数据库连接、密码等信息不希望别人看到,我们可以在settings.xml中配置,运行后会覆盖pom.xml中的配置。我们可以在pom.xml中定义一些假的数据。

把settings.xml中生产环境下的properties改变了,如下:

[html] view plain copy
 print?
  1. <profile>  
  2.         <id>environment</id>  
  3.         <activation>  
  4.             <activeByDefault>true</activeByDefault>  
  5.         </activation>  
  6.         <properties>  
  7.             <environment.type>dev</environment.type>  
  8.             <property1>2</property1>  
  9.         </properties>  
  10.     </profile>  
  11.   
  12.     <profile>  
  13.         <id>product</id>  
  14.         <activation>  
  15.             <property>  
  16.                 <name>environment.type</name>  
  17.                 <value>prod</value>  
  18.             </property>  
  19.         </activation>  
  20.         <properties>  
  21.             <jdbc.driverClassName>update_className</jdbc.driverClassName>  
  22.             <jdbc.url>update_url</jdbc.url>  
  23.             <jdbc.username>update_username</jdbc.username>  
  24.             <jdbc.password>update_password</jdbc.password>  
  25.             <property1>3</property1>  
  26.         </properties>  

这样执行mvn clean install -Denvironment.type=prod执行就可以看到db.properties改变成settings.xml中定义的了。(-Denvironment.type=prod是指定激活属性叫environment.type是prod的profile)



------------------------------------------------------------------------------------------------------------------

************************************************************************************************************

------------------------------------------------------------------------------------------------------------------

需求:我们希望windows系统打包以后名称带-win,Linux打包以后名称带-linux

在profiles中加入以下profile:

[html] view plain copy
 print?
  1. <profile>  
  2.         <id>windows</id>  
  3.         <activation>  
  4.             <os>  
  5.                 <family>windows</family>  
  6.             </os>  
  7.         </activation>  
  8.         <!-- 打jar包的时候分类标识符加个win -->  
  9.         <build>  
  10.             <plugins>  
  11.                 <plugin>  
  12.                     <artifactId>maven-jar-plugin</artifactId>  
  13.                     <configuration>  
  14.                         <classifier>win</classifier>  
  15.                     </configuration>  
  16.                 </plugin>  
  17.             </plugins>  
  18.         </build>  
  19.     </profile>  
  20.       
  21.     <profile>  
  22.         <id>linux</id>  
  23.         <activation>  
  24.             <os>  
  25.                 <family>unix</family>  
  26.             </os>  
  27.         </activation>  
  28.         <!-- 打jar包的时候分类标识符加个linux -->  
  29.         <build>  
  30.             <plugins>  
  31.                 <plugin>  
  32.                     <artifactId>maven-jar-plugin</artifactId>  
  33.                     <configuration>  
  34.                         <classifier>linux</classifier>  
  35.                     </configuration>  
  36.                 </plugin>  
  37.             </plugins>  
  38.         </build>  
  39.     </profile>  

执行mvn clean install



但是这样配置只针对这一个项目生效,如果我们想在所有项目中都使用这种打包方式,可以在settings.xml中定义这个profile从而生效吗?答案是不行的,原因是maven3认为一个外部的profile是不安全的,在使用的时候有很多的限制。

注意事项:settings.xml和profiles.xml里定义的profiles不会被deploy到repository,有诸多限制,因此,只有下面几个profiles能够在settings.xml和profiles.xml里定义:
repositories
pluginRepositories
properties
其他类型的profiles必须在pom.xml里定义。


在pom.xml中的build下的plugins中加入一个plugin

[html] view plain copy
 print?
  1. <plugin>  
  2.                 <artifactId>maven-jar-plugin</artifactId>  
  3.                 <configuration>  
  4.                     <classifier>${envClassifier}</classifier>  
  5.                 </configuration>  
  6.             </plugin>  

修改profiles中的profile

[html] view plain copy
 print?
  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>  
  12.       
  13.     <profile>  
  14.         <id>linux</id>  
  15.         <activation>  
  16.             <os>  
  17.                 <family>unix</family>  
  18.             </os>  
  19.         </activation>  
  20.         <properties>  
  21.             <envClassifier>linux</envClassifier>  
  22.         </properties>  
  23.     </profile>  

执行mvn clean install发现打成的jar包以win结尾。这样把上面的profile拿到settings.xml中去,发现运行正常。这样就完成了以后所有的项目都可以使用这个profile。



maven属性分五类:
1、project.*
2、settings.*
3、env.*
4、系统属性
5、用户自定义属性


如果pom.xml中定义了groupId为com.test.maven,那么我们可以在下面以${project.groupId}的方式进行使用。
project.groupId
project.version
project.artifactId
project.name
project.description
project.build.sourceDirectory
project.build.scriptSourceDirectory
project.build.testSourceDirectory
project.build.outputDirectory
project.build.testOutputDirectory
project.build.directory


env.PATH 包含了Maven运行的当前的Path。该Path包含了一个用来查找可运行脚本和程序的目录列表。
env.HOME (在*nix系统中)这个变量指向了用户的home目录。但你更应该使用/home/hudson,而非这个变量
env.JAVA_HOME 指向了你Java的安装目录,它要么指向jdk安装目录,要么指向JRE目录。但你更应该考虑使用/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/jre而非这个属性。
env.M2_HOME 指向了maven2安装目录


java.version java运行环境版本
java.vendor java运行环境供应商
java.vendor.url java供应商url
java.home java安装目录
java.vm.specification.version java虚拟机规格说明版本
java.vm.specification.vendor java虚拟机规格说明供应商
java.vm.version java虚拟机实现版本
java.vm.vendor java虚拟机实现供应商
java.vm.name java虚拟机实现名称
java.specification.version java运行环境规格说明版本
java.specification.vendor java运行环境规格说明供应商
java.specification.name java运行环境规格说明名称
java.class.version java类格式版本号
java.class.path java类路径
java.ext.dirs 扩展目录路径
os.name 操作系统名称
os.arch 操作系统架构
os.version 操作系统版本
file.separator 文件分隔符(unix上是“/”,windows是“\”)
path.separator 路径分隔符(unix上是“:”,windows是“;”)
line.separator 行分隔符(在unix和windows上都是”\n“)
user.name 用户账户名称
user.home 用户home目录
user.dir 用户当前工作目录

来源: http://blog.csdn.net/benjamin_whx/article/details/42778695

阅读全文
0 0
原创粉丝点击