在进行java开发时经常遇到一种情况,就是windows
下和linux
下需要引入的jar包是不一样的。
比如说我们需要使用java来操作OpenGL
库,我们就需要通过maven
引入JOGL
的依赖,
然而在window
下和在linux
下需要引入JOGL
的依赖是不一样的:
window
下,需要引入JOGL
的-win
版本的依赖。linux
下,则需要引入JOGL
的-linux
版本的依赖。那么我们应该怎么做呢,如何灵活配置和指定不同环境的依赖呢,我们需要使用Maven配置文件中的
元素。
下面是一个常用的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>com.xxx.xxxgroupId>
<artifactId>xxx-xxxartifactId>
<version>1.0-SNAPSHOTversion>
parent>
<artifactId>xxx-xxxartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
...
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<version>${spring-boot.version}version>
<configuration>
<finalName>${project.artifactId}finalName>
<layers>
<enabled>trueenabled>
layers>
<includeSystemScope>trueincludeSystemScope>
configuration>
<executions>
<execution>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<version>2.20.1version>
<configuration>
<skipTests>trueskipTests>
configuration>
plugin>
plugins>
build>
project>
现在我需要操作OpenGL,在开发时是win环境(amd64),需要加入如下依赖:
<dependency>
<groupId>org.jogamp.joglgroupId>
<artifactId>jogl-allartifactId>
<version>2.3.2version>
<classifier>natives-linux-amd64classifier>
dependency>
<dependency>
<groupId>org.jogamp.gluegengroupId>
<artifactId>gluegen-rtartifactId>
<version>2.3.2version>
<classifier>natives-linux-amd64classifier>
dependency>
但是上生产的时候是运行在Linux上的,则需要将上述依赖改为Linux的,如下:
<dependency>
<groupId>org.jogamp.joglgroupId>
<artifactId>jogl-allartifactId>
<version>2.3.2version>
<classifier>natives-windows-amd64classifier>
dependency>
<dependency>
<groupId>org.jogamp.gluegengroupId>
<artifactId>gluegen-rtartifactId>
<version>2.3.2version>
<classifier>natives-windows-amd64classifier>
dependency>
那么如何通过maven的配置来指定使用哪个依赖呢,则需要使用 Maven 的Profiles
,在pom中添加如下配置:
<dependencies>
...
dependencies>
<profiles>
<profile>
<id>winid>
<properties>
<profileActive>winprofileActive>
<db.url>jdbc:mysql://localhost/win_dbdb.url>
properties>
<activation>
<activeByDefault>trueactiveByDefault>
<property>
<name>envname>
<value>testvalue>
property>
activation>
<dependencies>
<dependency>
<groupId>org.jogamp.joglgroupId>
<artifactId>jogl-allartifactId>
<version>2.3.2version>
<classifier>natives-windows-amd64classifier>
dependency>
<dependency>
<groupId>org.jogamp.gluegengroupId>
<artifactId>gluegen-rtartifactId>
<version>2.3.2version>
<classifier>natives-windows-amd64classifier>
dependency>
dependencies>
profile>
<profile>
<id>linuxid>
<properties>
<profileActive>linuxprofileActive>
<db.url>jdbc:mysql://localhost/linux_dbdb.url>
properties>
<activation>
<activeByDefault>falseactiveByDefault>
<property>
<name>envname>
<value>prdvalue>
property>
activation>
<dependencies>
<dependency>
<groupId>org.jogamp.joglgroupId>
<artifactId>jogl-allartifactId>
<version>2.3.2version>
<classifier>natives-linux-amd64classifier>
dependency>
<dependency>
<groupId>org.jogamp.gluegengroupId>
<artifactId>gluegen-rtartifactId>
<version>2.3.2version>
<classifier>natives-linux-amd64classifier>
dependency>
dependencies>
profile>
profiles>
元素:用于给 Profile 分配一个唯一的标识符,使用maven构建时,根据此id选择需要激活的环境。元素
:自定义的属性,不同的 Profile 被激活时,里面的配置将被设置为不同的值,在其他地方可以获取到并使用改属性,比如在资源文件中,使用 ${}
格式的占位符引用属性的值:profileActive=${profileActive}
db.url=${db.url}
元素
:定义了 Profile 的激活条件。
元素:表示是否默认激活,设置为 true,表示在没有明确指定其他 Profile 时,该配置将自动激活,可以使用命令mvn clean package -Pwin
或mvn clean package -Plinux
来指定对应的profile id
。
元素:根据属性 env
的值来激活,可以使用命令-Denv=test
或-Denv=prd
来激活。
元素:该环境下对应的依赖。
元素:该环境下的构建选项。......
:其他元素,用处不多,不展开介绍。正如上面所说,这时候在构建时就可以指定选项来打包不同的依赖了:
通过profile id
打包:
mvn clean package -Pwin
通过自定义环境变量
打包:
mvn clean package -Denv=test