今天无意中刺激到我啦,这促使我发现自己还不够强大,我需要不断充实自己,扩充自己的实力,加油吧,骚年!!!

今天我要讲的是maven灵活构建相关的知识,请做好准备,知识点有点多哦~~~
maven属性
首先带来的是maven的属性知识,在maven中一共有6大属性,分别为:
内置属性
basedir 项目pom文件所在地址
version 项目版本
使用方式为直接使用${basedir}的方式进行引用
pom属性
可以使用该类属性引用pom文件中的配置,因为pom文件根元素为project,所以使用时通过${project.version},${project.groupId}的方式引用
settings属性
可以使用该类属性引用settings文件中的配置,前面已经提到,我们推荐在用户目录下使用该settings.xml文件,比如我在windows系统中该文件位置为:C:\Users\Administrator\.m2\settings.xml,settings.xml中根元素为settings,所以引用需要通过${settings.localRepository}的方式
自定义属性
这也许是我们最常用的一种属性了,在pom文件的properties中定义我们想要引用的属性,然后再通过${property_name}的方式引用,比如
<project>
<properties>
<spring.version>3.2.0</spring.version>
</properties>
</project>
通过${spring.version}方式引用
java系统属性
这种属性可以通过在控制台中输入mvn help:system的方式查询当前系统中存在的系统属性,通过${prop_name}的方式引用,比如${file.encoding.pkg}

环境变量属性
环境变量属性也可以通过mvn help:system的方式查看,引用的方式通过${env.varname}的方式引用

使用这些属性可以帮我们在引入模块时简化维护和配置的工作
管理多模块依赖
groupId,version都是相同的,这时可以通过pom属性将groupId,versionId进行简化
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>account-service</artifactId>
<version>${project.version}</version>
</dependency>
管理项目依赖
依赖某一个模块,比如spring一系列的jar包时,他们的version其实是一样的,所以我们也可以通过自定义属性定义spring.version属性,将版本号抽离出来放入<properties>标签中然后进行引用
配置插件
大量插件也用到了属性,比如maven-surefire-plugin中的reportsDirectory我们可以通过pom属性修改报告的位置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<reportsDirectory>${project.build.directory}/test-reports/</reportsDirectory>
</configuration>
</plugin>
构建环境的差异
在不同的环境中,项目的源码应该会用不同的方式进行构建,比如数据库的配置,在测试、开发、正式这三个不同的环境下是不会一样的,按照传统的方式,在src/main/resources下放置有配置文件jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
其中包含了数据库连接的配置信息,当在不同的环境中时,手工修改这个数据库配置,然后再构建,但这种方式是非常麻烦笨拙的
资源过滤(命令行)
为了解决上面的变化,maven提供了一种解决方式,通过profile隔离环境的变化
首先将jdbc.properties变化的配置抽离出来
jdbc.driverClassName=${jdbc.driverClassName}
jdbc.url=${jdbc.url}
jdbc.username=${jdbc.username}
jdbc.password=${jdbc.password}
通过maven属性与profile结合的方式配置不同环境的特有属性
<profiles>
<profile>
<id>dev</id>
<properties>
<jdbc.driverClassName>com.mysql.jdbc.Driver</jdbc.driverClassName/>
<jdbc.url>jdbc:mysql://localhost:3306/test</jdbc.url>
<jdbc.username>root</jdbc.username>
<jdbc.password>root</jdbc.password>
</properties>
</profile>
</profiles>
我们知道,对于maven的属性,只能在pom文件中使用,那么在资源配置文件jdbc.properties中是无法访问的,为了让其能够访问到属性,我们需要使用maven-resources-plugin做一些配置:
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
通过directory指定资源配置文件所在的目录,并通过filtering=true表示当前目录下的资源文件都需要进行过滤,也就是其中配置的属性引用需要被pom属性值替换
配置完之后。我们可以通过命令行mvn clean install -Pdev的方式运行,完成后输出目录中的数据库配置就被dev中的属性替换掉了
maven profile
上面提到,我们可以通过profile来隔离不同环境的属性变化,然后通过命令行-PprofileId的方式激活profile,对于profile的激活,还可以有其他几种方式
profile激活
命令行激活方式
mvn命令行参数-P加上profileId来激活profile,多个profile用逗号隔开
mvn clean install -Pdev-x,dev-y,这里将会激活dev-x,dev-y的配置信息
settings文件显示激活
如果希望某一配置默认一直处于激活状态,就可以配置settings.xml文件中的activeProfiles元素,它表示配置的profile对所有项目都处于激活状态
<settings>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
系统属性激活
可以用当某一个系统属性存在的时候自动激活profile
<profiles>
<profile>
<activation>
<property>
<name>test</name>
</property>
</activation>
...
</profile>
</profiles>
可以进一步精确,当存在属性test,且值为x时激活profile
<profiles>
<profile>
<activation>
<property>
<name>test</name>
<value>x</value>
</property>
</activation>
...
</profile>
</profiles>
我们可以通过命令行声明系统属性,所以我们可以通过
mvn clean install -Dtest=x的方式从命令行激活profile
操作系统环境激活
<profiles>
<profile>
<activation>
<os>
<name>Windows 10</name>
<family>Windows</family>
<arch>amd64</arch>
<version>10.0</version>
</os>
</activation>
</profile>
</profiles>
family包括Windows,UNIX,Mac等,其他几项name,arch,version可以通过mvn help:system方式查看os.开头的系统属性,这里分别对应os.name,os.arch,os.version
文件是否存在激活
<profiles>
<profile>
<activation>
<file>
<missing>x.properties</missing>
<exists>y.properties</exists>
</file>
</activation>
...
</profile>
</profiles>
存在y.properties,且不存在x.properties时激活profile
默认激活(开发中用的最多)
用户可以在配置profile时指定默认激活
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
不过需要注意的是,如果有任何一个profile通过其他以上任意一种方式被激活了,那么当前的默认激活配置就会失效
如果项目中配置了多种激活方式,那么我们可以通过maven-help-plugin查看
查看当前已激活的配置
mvn help:active-profiles
列出当前所有profile
mvn help:all-profiles
profile的种类
根据需要,用户可以将profile配置到:
pom.xml 只对当前项目有效
用户settings.xml 对本机该用户所有项目有效
全局settings.xml 安装maven目录下的conf/settings.xml中的profile对本机所有用户的所有项目有效
在profile中可以使用的pom元素
<project>
<repositories></repositories>
<pluginRepositories></pluginRepositories>
<distributionManagement></distributionManagement>
<dependencies></dependencies>
<dependencyMangement></dependencyManagement>
<modules></modules>
<properties></properties>
<reporting></reporting>
<build>
<plugins></plugins>
<defaultGoal></defaultGoal>
<resources></resources>
<testResources></testResources>
<finalName></finalName>
</build>
</project>
web资源过滤
与普通的配置文件一样,web资源(src/main/webapps/下的资源文件css/js/img等)也不能使用pom属性,需要通过maven-war-plugin进行配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/webapp</directory>
<includes>
<include>**/*.css</include>
<include>**/*.js</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
通过directory声明web资源目录src/main/webapp(这也是默认的web资源目录),然后配置filtering开启过滤,并使用includes指定要过滤的文件,这里指定所有的js,css都需要进行过滤
在工作中的使用
我们需要根据不同环境配置不同的参数,就拿数据库配置为例,account-web表示项目名
我们需要在account-web下建立不同环境的配置文件,用于分离环境参数配置

dev.properties的配置如下,其他环境比如test,production环境可能需要配置不同的数据库连接方式
jdbc.username=root
jdbc.password=root
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/account?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false&maxReconnects=10
在pom中指定三种不同环境的profile
<profiles>
<profile>
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<env>production</env>
</properties>
</profile>
</profiles>
<build>
<filters>
<filter>${basedir}/config/${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.xsd</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
这里需要注意使用includes表示构建时需要将那些资源文件纳入构建中,这里需要指定所有需要用到的资源文件,否则在目标构建中将不会包含include之外的配置文件
如此在运行maven命令进行打包时就会使用激活的profile对应的配置替换其中变量
好啦,今天就讲到这里,刚好00:00,还有其他事情要做,今天就到这里吧~~~