maven根据操作系统的不同打包时引入不同的依赖(jar)

在进行java开发时经常遇到一种情况,就是windows下和linux下需要引入的jar包是不一样的。

比如说我们需要使用java来操作OpenGL库,我们就需要通过maven引入JOGL的依赖,

然而在window下和在linux下需要引入JOGL的依赖是不一样的:

  • window下,需要引入JOGL-win版本的依赖。
  • linux下,则需要引入JOGL-linux版本的依赖。

那么我们应该怎么做呢,如何灵活配置和指定不同环境的依赖呢,我们需要使用Maven配置文件中的 <profiles> 元素。

下面是一个常用的pom.xml

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),需要加入如下依赖:

xml 复制代码
	<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的,如下:

xml 复制代码
	<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中添加如下配置:

xml 复制代码
    <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 被激活时,里面的配置将被设置为不同的值,在其他地方可以获取到并使用改属性,比如在资源文件中,使用 ${} 格式的占位符引用属性的值:

    properties 复制代码
    profileActive=${profileActive}
    db.url=${db.url}
  • <activation> 元素 :定义了 Profile 的激活条件。

    • <activeByDefault>元素:表示是否默认激活,设置为 true,表示在没有明确指定其他 Profile 时,该配置将自动激活,可以使用命令mvn clean package -Pwinmvn clean package -Plinux来指定对应的profile id
    • <property>元素:根据属性 env 的值来激活,可以使用命令-Denv=test-Denv=prd来激活。
  • <dependencies>元素:该环境下对应的依赖。

  • <build>元素:该环境下的构建选项。

  • ......:其他元素,用处不多,不展开介绍。

正如上面所说,这时候在构建时就可以指定选项来打包不同的依赖了:

  1. 通过profile id打包:

    shell 复制代码
    mvn clean package -Pwin
  2. 通过自定义环境变量打包:

    shell 复制代码
    mvn clean package -Denv=test
相关推荐
程序员南飞1 小时前
ps aux | grep smart_webrtc这条指令代表什么意思
java·linux·ubuntu·webrtc
弥琉撒到我1 小时前
微服务swagger解析部署使用全流程
java·微服务·架构·swagger
一颗花生米。2 小时前
深入理解JavaScript 的原型继承
java·开发语言·javascript·原型模式
问道飞鱼2 小时前
Java基础-单例模式的实现
java·开发语言·单例模式
ok!ko6 小时前
设计模式之原型模式(通俗易懂--代码辅助理解【Java版】)
java·设计模式·原型模式
2402_857589366 小时前
“衣依”服装销售平台:Spring Boot框架的设计与实现
java·spring boot·后端
吾爱星辰7 小时前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
哎呦没8 小时前
大学生就业招聘:Spring Boot系统的架构分析
java·spring boot·后端
编程、小哥哥8 小时前
netty之Netty与SpringBoot整合
java·spring boot·spring
IT学长编程9 小时前
计算机毕业设计 玩具租赁系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·玩具租赁系统