Maven发布轻量二方包
fat jar(打包發布自身依賴的jar包)
在公共項目開發了common模塊,其中包含了發釘釘消息、時間處理等多種公共方法,現在想要發布到maven倉庫給其他應用使用。
其他應用使用過程中發現,引入common模塊的依賴后還需要單獨引用common內部的其他依賴。
client如果要使用發消息模塊的能力,需要引入如下依賴:
pom.xml:
這樣使用存在兩個問題:
1.client需要手動引入common內部使用的依賴,沒有起到統一管理的效果。
2.common升級內部依賴client也要跟著升級,否則可能出現低版本使用高版本依賴的方法,比如:ObjectUtils.allNull() 方法就在 commons-lang3 低版本就沒有。
那為什么不直接將common模塊所需要依賴都打進去呢?
pom.xml:
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:
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本身即可。
正常打包
<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发布轻量二方包的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 发布golang第三方包
- 下一篇: go 常用第三方包