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

在进行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 -Pwinmvn clean package -Plinux来指定对应的profile id
    • <property>元素:根据属性 env 的值来激活,可以使用命令-Denv=test-Denv=prd来激活。
  • <dependencies>元素:该环境下对应的依赖。
  • <build>元素:该环境下的构建选项。
  • ......:其他元素,用处不多,不展开介绍。

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

  1. 通过profile id打包:

    mvn clean package -Pwin
    
  2. 通过自定义环境变量打包:

    mvn clean package -Denv=test
    

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/77804.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

音视频编码格式-AAC ADT

例子:config 1408 1408(16进制) : 0001 0100 0000 1000 audioObjectType&#xff08;5bit&#xff09;为 00010 , 即 2&#xff0c; profie (audioObjectType -1 ) AAC LC samplingFrequencyIndex (4bit) 为 1000 , 即 8 , 对应的采样频率为 16000 channelConfiguration (…

vue预览txt

1.本地文件预览&#xff0c;网上很多使用iframe和embed标签的&#xff0c;但是我尝试都不可以&#xff0c;有了解的可以分享下原因。 2.读取txt数据并显示在div中&#xff1a; 2.1 本地txt <input type"file" ref"file"/> <div v-html"txtH…

C# OpenVinoSharp PP-TinyPose 人体姿态识别

效果 项目 部分代码 using OpenCvSharp; using OpenCvSharp.Extensions; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;name…

创建git分支命名原则

使用有意义的名称来描述分支的用途或目的&#xff0c;例如feature/add-login-page、bugfix/fix-typo等。 分支名称应该简短但明确&#xff0c;避免使用过长或复杂的名称。 使用斜杠(/)分隔不同的分支类型&#xff0c;例如feature、bugfix、hotfix、release、develop、main等。…

QTableView通过setColumnWidth设置了列宽无效的问题

在用到QT的QTableView时&#xff0c;为了显示效果&#xff0c;向手动的设置每一列的宽度&#xff0c;但是如下的代码是无效的。 ui->tableView->setColumnWidth(0,150);ui->tableView->setColumnWidth(1,150);ui->tableView->setColumnWidth(2,150);ui->t…

源码编译risc-v虚拟机和编译器 riscv-gnu-toolchain 和 riscv-tools 在ubuntu 22.04

1. 编译 riscv-gnu-toolchain 1.1 预备环境 $ sudo apt-get install autoconf automake autotools-dev curl python3 libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev 1.2 下载源代码 http…

lv4 嵌入式开发-1 Linux文件IO

目录 1 文件的概念和类型 2 如何理解标准IO 3 流(FILE)的含义 3.1 流 3.2 文本流和二进制流 3.3 流的缓冲类型 4 小结 5 缓存区实验 1 文件的概念和类型 概念&#xff1a;一组相关数据的有序集合 文件类型&#xff1a; 常规文件 r 目录文件 d 字符设备文件 …

前端工程化小记

1.引言 工业界一直都是&#xff1a;能机器做的都交给机器&#xff0c;因为人更容易犯错。所以关于代码语法检查、代码格式化、commit注释规范、代码编译等等这些工作量繁杂且巨大的苦力活&#xff0c;除非你不想把人当马用&#xff0c;那还是交给机器去做&#xff0c;是吗&…

【尚硅谷】第05章:随堂复习与企业真题(数组)

来源&#xff1a;尚硅谷Java零基础全套视频教程(宋红康2023版&#xff0c;java入门自学必备) 基本都是宋老师发的资料里面的内容&#xff0c;只不过补充几个资料里没直接给出答案的问题的答案。 不想安装markdown笔记的app所以干脆在这里发一遍。 第05章&#xff1a;随堂复习…

ChatGPT AIGC 一键总结SQL优化所有知识点

SQL优化一直是程序员非常关注的内容,使用ChatGPT AIGC结合思维导图进行总结SQL优化的所有知识点内容。 非常简单实用的操作,就得到了如何进行SQL优化的所有细节。 更多内容见: AIGC ChatGPT ,BI商业智能, 可视化Tableau, PowerBI, FineReport, 数据库Mysql Oracle, Off…

Flink CDC 菜鸟教程 -环境篇

本教程将介绍如何使用 Flink CDC 来实现这个需求, 在 Flink SQL CLI 中进行,只涉及 SQL,无需一行 Java/Scala 代码,也无需安装 IDE。 系统的整体架构如下图所示: 环境篇 1、 准备一台Linux 2、准备教程所需要的组件 下载 flink-1.13.2 并将其解压至目录 flink-1.13.2 …

阿里后端开发:抽象建模经典案例【文末送书】

文章目录 写作前面1.抽象思维2.软件世界中的抽象3. 经典抽象案例4. 抽象并非一蹴而就&#xff01;需要不断假设、验证、完善5. 推荐一本书 写作末尾 写作前面 在互联网行业&#xff0c;软件工程师面对的产品需求大都是以具象的现实世界事物概念来描述的&#xff0c;遵循的是人…

微信小程序父子组件通讯方法

自定义方法中发送命令 const app getApp() Component({options: {styleIsolation: isolated},data: {},properties: {},attached() {this.init()},methods: {init() {console.log(父组件执行子组件)},clickBtn() {this.triggerEvent(changeRoute, 发送数据到父组件, {})},} }…

typeScript--[函数定义]

一.TypesScript 函数的定义 函数的定义包括两种类型&#xff1a;函数声明和函数表达式。 1.函数声明 function hello(): string {return "hello" } 2.函数表达式 var hello1 function (): string {return "hello" } 二.函数之可选参数 参数后面的限…

Android Studio导入aosp源码

1、在 Ubuntu 系统下&#xff0c;进入源码根目录&#xff0c;运行如下命令&#xff1a; source build/envsetup.sh # 初始化环境变量。 lunch sdk_phone_x86_64 make idegen -j6 # 六核编译 idegen 模块 忽略此命令&#xff1a;mmm development/tools/idegen/ # 此命令可…

XL-LightHouse 与 Flink 和 ClickHouse 流式大数据统计系统

一个Flink任务只能并行处理一个或少数几个数据流&#xff0c;而XL-LightHouse一个任务可以并行处理数万个、几十万个数据流&#xff1b; 一个Flink任务只能实现一个或少数几个数据指标&#xff0c;而XL-LightHouse单个任务就能支撑大批量、数以万计的数据指标。 1、XL-LightHo…

2023计算机毕设选题 python毕业设计如何选题

文章目录 一、python 毕设 选题推荐二、选题注意事项2.1 难度怎么把控&#xff1f;2.2 题目名称怎么取&#xff1f; 三、开题指导3.1 起因3.2 如何避坑(重中之重)3.3 为什么这么说呢&#xff1f; 四、最后 一、python 毕设 选题推荐 以下为学长手动整理python 毕业设计 项目&a…

自动驾驶的区块链结合价值所在

区块链结合自动驾驶的价值所在 1.责任判定 车辆行驶发生事故时&#xff0c;需要进行责任判定&#xff0c; 目前的事故组合方式&#xff1a; 普通车-----普通车 自动驾驶车&#xff08;处于自动驾驶状态&#xff09;--------普通车 自动驾驶车&#xff08;人工驾驶状态&#x…

预测多基因扰动的转录结果

了解细胞对基因扰动的反应是许多生物医学应用的核心&#xff0c;从识别癌症中涉及的基因相互作用到开发再生医学方法。然而&#xff0c;可能的多基因扰动数量的组合爆炸严重限制了实验验证。在这里&#xff0c;作者提出了图增强的基因激活和抑制模拟器&#xff08;GEARS&#x…

06-mq

1、消息队列有什么优点和缺点? 优点&#xff1a; 解耦、异步、削峰填谷。 缺点&#xff1a; 系统可用性降低 系统复杂性提高 一致性问题 2、常见消息队列的比较 3、Kafka的特性 1.消息持久化 2.高吞吐量 3.扩展性强&#xff08;动态&#xff09;4集群&#xff0b;4台集群…