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

这篇具有很好参考价值的文章主要介绍了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. 通过自定义环境变量打包:文章来源地址https://www.toymoban.com/news/detail-708482.html

    mvn clean package -Denv=test
    

到了这里,关于maven根据操作系统的不同打包时引入不同的依赖(jar)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 在不同操作系统上安装Python的详细教程

    打开Python官方网站(https://www.python.org/downloads/)并下载最新版本的Python。 选择适合您操作系统的版本。如果您使用的是64位的Windows系统,请下载64位版本。如果您不确定自己的系统是32位还是64位,请按下Win+R键,在运行对话框中输入cmd,然后按下回车键。输入systeminfo命令并按

    2023年04月08日
    浏览(25)
  • 【winForm取消窗体关闭操作并隐藏到系统托盘操作以及打包exe】

    场景:点击窗体关闭按钮并隐藏在系统托盘里显示小图标,小图标右键有显示窗体和退出两个按钮以及最后打包成exe 1.在主页form窗体拖入控件notifyIcon和contextMenuStrip控件 设置notifyIcon1控件属性: notifyIcon1事件里设置 设置contextMenuStrip控件新增两个按钮 然后双击编辑“显示”按

    2024年02月06日
    浏览(69)
  • VMware ESXI系统如何操作:同服务器中的不同虚机实现不同网段通讯

    一、登录 1、登录系统       在左侧的功能栏中找到“网络”,看下是否有两个网卡(有时可能显示的和宿主机接口不一样,小编暂时还没有遇到),现关信息入下图:  2、添加虚拟交换机        当第1步的相关信息检查完毕后,确认满足网络需求,便可如下操作。      

    2024年02月09日
    浏览(29)
  • 同一操作系统中安装多个不同版本谷歌Chrome浏览器

    本来我电脑上已经有103版本的Chrome浏览器: 1. 现在我需要安装一个89版本的Chrome浏览器,我已经有该版本的.exe文件: 如果没有该安装包的,https://www.chromedownloads.net/chrome64win/ 通过这个网址可以下载到相应版本的chrome浏览器, 记住,必须时离线安装包,不能是在线安装包。

    2024年02月05日
    浏览(34)
  • 在不同操作系统上如何安装符号表提取工具(eu-strip)

    C++开发的小伙伴都知道符号表在调试和解决崩溃时扮演着非常重要的角色,那么如何提取和保存发布应用程序的符号表就变得非常重要。今天就来聊一下如何在不同的操作系统上使用eu-strip提取应用程序中的符号表信息。 如何在不同操作系统上安装符号表提取工具en-strip呢?

    2024年02月09日
    浏览(27)
  • uni-app打包小程序根据不同编译条件打包不同静态素材目录

    在uni-app开发小程序的时候,我们经常有这样的需求,一个小程序,拥有多个不同样式的模板,发布的时候,为了减少包的体积,我们希望只打包当前使用的模板对应的静态素材目录,而其他模板的目录不打包进去。 在package.json中定义的模板变量如下: 每一个模板的静态素材

    2024年02月10日
    浏览(34)
  • 【Maven】maven引入第三方jar包并打包

    idea中的springboot项目引用第三方jar包,打包时将其引入 本文参照官网:http://maven.apache.org/ 第一种:在pom文件引入jar包的目录 1.选择File下的project Structure 2.选择Module,选择项目模块,选择Dependencies下的加号:点击JARs or Directories…: 3.选择你jar包所在的位置,点击OK,点击Apply,此时已

    2024年02月16日
    浏览(39)
  • maven工程打包引入本地jar包

     在jar包的文件目录下执行:   maven仓库效果:   maven引入:

    2024年02月22日
    浏览(36)
  • 【云原生 | 02】分别在CentOS、Ubuntu、macOS、win7、win8、win10等不同操作系统下安装Docker详细教程

    🍁 博主简介 :         🏅云计算领域优质创作者         🏅2022年CSDN新星计划python赛道第一名         🏅2022年CSDN原力计划优质作者         🏅阿里云ACE认证高级工程师         🏅阿里云开发者社区专家博主 💊 交流社区 :CSDN云计算交流社区欢迎您的

    2024年01月17日
    浏览(54)
  • maven项目引入私有jar,并打包到java.jar中

    私有jar存放位置 maven依赖 maven build

    2024年03月11日
    浏览(45)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包