日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Maven发布轻量二方包

發布時間:2023/12/3 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Maven发布轻量二方包 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

fat jar(打包發布自身依賴的jar包)

在公共項目開發了common模塊,其中包含了發釘釘消息、時間處理等多種公共方法,現在想要發布到maven倉庫給其他應用使用。

其他應用使用過程中發現,引入common模塊的依賴后還需要單獨引用common內部的其他依賴。

client如果要使用發消息模塊的能力,需要引入如下依賴:
pom.xml:

<!-- common內部發消息的依賴 --> <dependency><groupId>com.dingtalk.open</groupId><artifactId>dingtalk-openapi-sdk</artifactId><version>xxx</version> </dependency><dependency><groupId>com.alibaba.logistics</groupId><artifactId>logistics-infra-common</artifactId><version>1.0.1-SNAPSHOT</version> </dependency>

這樣使用存在兩個問題:
1.client需要手動引入common內部使用的依賴,沒有起到統一管理的效果。
2.common升級內部依賴client也要跟著升級,否則可能出現低版本使用高版本依賴的方法,比如:ObjectUtils.allNull() 方法就在 commons-lang3 低版本就沒有。

那為什么不直接將common模塊所需要依賴都打進去呢?
pom.xml:

<plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration><!-- 包名稱末尾不跟assemblyId --><appendAssemblyId>false</appendAssemblyId><descriptors><descriptor>assembly.xml</descriptor></descriptors> </configuration> </plugin><!-- 打包源碼 --> <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><version>3.0.1</version> </plugin> <plugin><artifactId>maven-source-plugin</artifactId><executions><execution><id>attach-sources</id><goals><goal>jar</goal></goals></execution></executions> </plugin>

assembly.xml:

<?xml version="1.0" encoding="UTF-8" ?> <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0http://maven.apache.org/xsd/assembly-1.1.0.xsd"><id>common.jar</id><formats><format>jar</format></formats><includeBaseDirectory>false</includeBaseDirectory><fileSets><fileSet><directory>${project.build.directory}/classes</directory><outputDirectory>/</outputDirectory><includes><include>**/**</include></includes></fileSet></fileSets><dependencySets><dependencySet><outputDirectory>/</outputDirectory><!-- 忽略自己產生的依賴包 --><useProjectArtifact>false</useProjectArtifact></dependencySet></dependencySets></assembly>

MAC解壓:tar -zxvf xx.jar -C test1

依賴展示:

看看現在打包后的結果,確實把common所需要的依賴都打包進去了,client不用再單獨引入,但是這樣有一個問題,common與client的包有很多都是重復的,有可能發生包沖突等問題,對client端很不友好。

那能不能給common依賴瘦下身?排除掉常用的依賴,只包含特有的依賴。
pom.xml:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration><!-- 包名稱末尾不跟assemblyId --><appendAssemblyId>false</appendAssemblyId><descriptors><descriptor>src/assembly/assembly.xml</descriptor></descriptors> </configuration> <executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution> </executions> </plugin><!-- 打包源碼 --> <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><version>3.0.1</version> </plugin> <plugin><artifactId>maven-source-plugin</artifactId><executions><execution><id>attach-sources</id><goals><goal>jar</goal></goals></execution></executions> </plugin>

assembly:

<?xml version="1.0" encoding="UTF-8" ?> <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0http://maven.apache.org/xsd/assembly-1.1.0.xsd"><id>common.jar</id><formats><format>jar</format></formats><includeBaseDirectory>false</includeBaseDirectory><fileSets><fileSet><directory>${project.build.directory}/classes</directory><outputDirectory>/</outputDirectory><includes><include>**/**</include></includes></fileSet></fileSets><dependencySets><dependencySet><outputDirectory>/</outputDirectory><!-- 忽略自己產生的依賴包 --><useProjectArtifact>false</useProjectArtifact><includes><include>com.aliyun.tair:tairjedis-sdk-singlepath</include></includes><excludes><exclude>javax.annotation:javax.annotation-api</exclude><exclude>log4j:log4j</exclude><exclude>org.slf4j:slf4j-api</exclude><exclude>org.slf4j:jul-to-slf4j</exclude><exclude>ch.qos.logback:logback-classic</exclude><exclude>ch.qos.logback:logback-core</exclude><exclude>org.apache.logging.log4j:log4j-to-slf4j</exclude><exclude>org.apache.logging.log4j:log4j-api</exclude><exclude>org.projectlombok:lombok</exclude><exclude>org.springframework:spring-aop</exclude><exclude>org.springframework:spring-beans</exclude><exclude>org.springframework.boot:spring-boot</exclude><exclude>org.springframework.boot:spring-boot-autoconfigure</exclude><exclude>org.springframework.boot:spring-boot-starter</exclude><exclude>org.springframework.boot:spring-boot-starter-logging</exclude><exclude>org.springframework:spring-context</exclude><exclude>org.springframework:spring-core</exclude><exclude>org.springframework:spring-expression</exclude><exclude>org.springframework:spring-jcl</exclude><exclude>org.yaml:snakeyaml</exclude></excludes></dependencySet></dependencySets></assembly>

依賴展示:

經過上述的打包優化后,打出的common包已經很輕量化了,client使用只需要引入common本身即可。

<dependency><groupId>com.alibaba.logistics</groupId><artifactId>logistics-infra-common</artifactId><version>1.0.1-SNAPSHOT</version> </dependency>

正常打包

<build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding><compilerArgument>-parameters</compilerArgument></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>3.2.0</version><configuration><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId><version>3.2.0</version><executions><execution><id>attach-sources</id><goals><goal>jar</goal></goals></execution></executions></plugin></plugins> </build>

總結

以上是生活随笔為你收集整理的Maven发布轻量二方包的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。