maven-shade-plugin
pom
1 2 3 4 5 6 <dependency > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-shade-plugin</artifactId > <version > 3.2.4</version > </dependency >
使用
打包可执行 jar 包
通过设置主类的方式,打包可执行 jar 包。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <plugin > <executions > <execution > <configuration > <transformers > <transformer implementation ="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" > <mainClass > pub.wii.cook.Main</mainClass > </transformer > </transformers > </configuration > </execution > </executions > </plugin >
目标 jar 包含指定包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-shade-plugin</artifactId > <version > 2.3</version > <executions > <execution > <phase > package</phase > <goals > <goal > shade</goal > </goals > <configuration > <relocations > <relocation > <pattern > com.google.protobuf</pattern > <shadedPattern > shade.com.google.protobuf</shadedPattern > </relocation > </relocations > <artifactSet > <includes > <include > com.github.scopt:scopt_2.12</include > <include > com.google.protobuf:protobuf-java</include > <include > com.google.protobuf:protobuf-java-util</include > </includes > </artifactSet > </configuration > </execution > </executions > </plugin >
配置示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-shade-plugin</artifactId > <version > 3.2.4</version > <configuration > <filters > <filter > <artifact > *:*</artifact > <excludes > <exclude > META-INF/*.SF</exclude > <exclude > META-INF/*.DSA</exclude > <exclude > META-INF/*.RSA</exclude > </excludes > </filter > </filters > </configuration > <executions > <execution > <phase > package</phase > <goals > <goal > shade</goal > </goals > <configuration > <transformers > <transformer implementation ="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" /> <transformer implementation ="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" > <mainClass > pub.wii.cook.Main</mainClass > </transformer > </transformers > </configuration > </execution > </executions > </plugin >
maven-assembly-plugin
pom
1 2 3 4 5 6 <dependency > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-assembly-plugin</artifactId > <version > 3.3.0</version > </dependency >
使用
maven-assembly-plugin
插件的使用需要打包配置(通常命名为 assembly.xml、release.xml 等,具体内容参考配置示例),插件内置了一部分配置,比如 jar-with-dependencies
,使用如下配置,来启用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <plugin > <executions > <execution > <configuration > <descriptorRefs > <descriptorRef > jar-with-dependencies</descriptorRef > </descriptorRefs > </configuration > </execution > </executions > </plugin >
这里需要注意两点。
非配置文件示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <plugin > <artifactId > maven-assembly-plugin</artifactId > <configuration > <archive > <manifest > <mainClass > pub.wii.cook.scala.main</mainClass > </manifest > </archive > <descriptorRefs > <descriptorRef > jar-with-dependencies</descriptorRef > </descriptorRefs > </configuration > <executions > <execution > <id > make-assembly</id > <phase > package</phase > <goals > <goal > single</goal > </goals > </execution > </executions > </plugin >
配置示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-assembly-plugin</artifactId > <version > 3.3.0</version > <executions > <execution > <phase > package</phase > <goals > <goal > single</goal > </goals > <configuration > <descriptors > <descriptor > src/assemble/assembly.xml</descriptor > </descriptors > <archive > <manifest > <mainClass > pub.wii.cook.Main</mainClass > </manifest > </archive > </configuration > </execution > </executions > </plugin >
assembly.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 <assembly xmlns ="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd" > <id > jar-with-dependencies</id > <formats > <format > jar</format > </formats > <includeBaseDirectory > false</includeBaseDirectory > <dependencySets > <dependencySet > <outputDirectory > /</outputDirectory > <useProjectArtifact > true</useProjectArtifact > <unpack > true</unpack > <scope > runtime</scope > </dependencySet > </dependencySets > <containerDescriptorHandlers > <containerDescriptorHandler > <handlerName > metaInf-services</handlerName > </containerDescriptorHandler > <containerDescriptorHandler > <handlerName > metaInf-spring</handlerName > </containerDescriptorHandler > <containerDescriptorHandler > <handlerName > plexus</handlerName > </containerDescriptorHandler > <containerDescriptorHandler > <handlerName > file-aggregator</handlerName > <configuration > <filePattern > .*/META-INF/services/io.grpc.NameResolverProvider</filePattern > <outputPath > META-INF/services/io.grpc.NameResolverProvider</outputPath > </configuration > </containerDescriptorHandler > </containerDescriptorHandlers > </assembly >
参考