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