IDEA利用Maven将依赖的第三方jar包打入一个jar包

    xiaoxiao2023-10-17  181

    在我利用Maven构建好一个工程后,在IDEA的Maven Projects里双击package,发现打出的jar包里没有所要依赖的jar包,于是又在IDEA控制台执行path/to/maven/bin/mvn assembly:assembly命令后,发现又一个个Download很多依赖的jar包,而这些jar包我原先就已经下好了,此时打包的时间很长很长,以至于我无法忍受。(此时pom.xml中已经有maven-scala-plugin、maven-compiler-plugin、maven-assembly-plugin、maven-surefire-plugin这些plugin) 经过一番搜寻,在pom.xml中添加maven-shade-plugin,最终的pom.xml如下:

    <build> <plugins> <plugin> <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <version>2.15.2</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.3</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19</version> <configuration> <skip>true</skip> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.4.3</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass></mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> <defaultGoal>compile</defaultGoal> </build>

    添加后等待Maven加载完,再次点击package,不一会儿Maven就打包完成,会打成两个jar包,一个是无依赖的jar即original-boya-1.0-SNAPSHOT.jar;一个是有依赖的jar即boya-1.0-SNAPSHOT.jar,里面包括了所依赖的其他jar包

    最新回复(0)