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
相关推荐
..Cherry..8 分钟前
【java】jvm
java·开发语言·jvm
老K的Java兵器库18 分钟前
并发集合踩坑现场:ConcurrentHashMap size() 阻塞、HashSet 并发 add 丢数据、Queue 伪共享
java·后端·spring
计算机毕业设计木哥38 分钟前
计算机毕业设计选题推荐:基于SpringBoot和Vue的爱心公益网站
java·开发语言·vue.js·spring boot·后端·课程设计
ANnianStriver40 分钟前
智谱大模型实现文生视频案例
java·aigc
普通网友1 小时前
KUD#73019
java·php·程序优化
番茄Salad1 小时前
自定义Spring Boot Starter项目并且在其他项目中通过pom引入使用
java·spring boot
程序员三明治1 小时前
详解Redis锁误删、原子性难题及Redisson加锁底层原理、WatchDog续约机制
java·数据库·redis·分布式锁·redisson·watchdog·看门狗
自由的疯1 小时前
Java 怎么学习Kubernetes
java·后端·架构
自由的疯1 小时前
Java kubernetes
java·后端·架构
普通网友2 小时前
IZT#73193
java·php·程序优化