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

在我利用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)
IDEA利用Maven将依赖的第三方jar包打入一个jar包_第1张图片
经过一番搜寻,在pom.xml中添加maven-shade-plugin,最终的pom.xml如下:

<build>
    <plugins>
      <plugin>
        <groupId>org.scala-toolsgroupId>
        <artifactId>maven-scala-pluginartifactId>
        <version>2.15.2version>
        <executions>
          <execution>
            <goals>
              <goal>compilegoal>
              <goal>testCompilegoal>
            goals>
          execution>
        executions>
      plugin>

      <plugin>
        <artifactId>maven-compiler-pluginartifactId>
        <version>3.6.0version>
        <configuration>
          <source>1.8source>
          <target>1.8target>
        configuration>
      plugin>
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-assembly-pluginartifactId>
        <version>2.3version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependenciesdescriptorRef>
          descriptorRefs>
        configuration>
      plugin>

      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-surefire-pluginartifactId>
        <version>2.19version>
        <configuration>
          <skip>trueskip>
        configuration>
      plugin>

      <plugin>
         <groupId>org.apache.maven.pluginsgroupId>
         <artifactId>maven-shade-pluginartifactId>
         <version>2.4.3version>
         <executions>
              <execution>
                <phase>packagephase>
                <goals>
                  <goal>shadegoal>
                goals>
                <configuration>
                  <filters>
                    <filter>
                      <artifact>*:*artifact>
                      <excludes>
                        <exclude>META-INF/*.SFexclude>
                        <exclude>META-INF/*.DSAexclude>
                        <exclude>META-INF/*.RSAexclude>
                      excludes>
                    filter>
                  filters>
                  <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                      <mainClass>mainClass>
                    transformer>
                  transformers>
                configuration>
              execution>
            executions>
          plugin>
    plugins>
    <defaultGoal>compiledefaultGoal>
  build>

添加后等待Maven加载完,再次点击package,不一会儿Maven就打包完成,会打成两个jar包,一个是无依赖的jar即original-boya-1.0-SNAPSHOT.jar;一个是有依赖的jar即boya-1.0-SNAPSHOT.jar,里面包括了所依赖的其他jar包
IDEA利用Maven将依赖的第三方jar包打入一个jar包_第2张图片

你可能感兴趣的:(maven,IDEA,Maven打第三方jar包)