IDEA打包JAR的多种方式

By timebusker on June 6, 2018

IDEA打包JAR的多种方式

用IDEA自带的打包形式

image
image
image
image

用maven-shade-plugin打包

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>1.4</version>
  <configuration>
      <createDependencyReducedPom>true</createDependencyReducedPom>
  </configuration>
  <executions>
      <execution>
          <phase>package</phase>
          <goals>
              <goal>shade</goal>
          </goals>
          <configuration>
              <transformers>
                  <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
				      <!--用来配置Main函数的入口-->
                      <mainClass>主类</mainClass>
                  </transformer>
              </transformers>
          </configuration>
      </execution>
  </executions>
</plugin>

项目安装打包:

mvn clean compile //清除之前target编译文件并重新编译
mvn clean package //对项目进行打包(因为配置过插件,所以jar包是可执行的)
mvn clean install //安装项目,然后就可以使用了

用maven-assembly-plugin打包

<plugin>
   <artifactId>maven-assembly-plugin</artifactId>
   <version>2.4</version>
   <configuration>
       <descriptorRefs>
           <descriptorRef>jar-with-dependencies</descriptorRef>
       </descriptorRefs>
       <archive>
           <manifest>
			   <!--用来配置Main函数的入口-->
               <mainClass>主类</mainClass>
           </manifest>
       </archive>
   </configuration>
   <executions>
       <execution>
           <id>make-assembly</id>
           <phase>package</phase>
           <goals>
               <goal>single</goal>
           </goals>
       </execution>
   </executions>
</plugin>

项目打包部署:

mvn assembly:assembly