Idea打包的多种方式研究

Idea打包方式

  1. 打一个empty只包含编译文件的包,artifact===> 点击+ ====> jar =====> Empty ===> Output layout ===> 选择相关模块下的 complie output即可
  2. 打包一个Empty包含编译文件和依赖的包,artifact===> 点击+ ====> jar =====> Empty ===> Output layout ===> 选择相关模块下的 complie output&& 相关依赖【extratct into output root】
  3. 打包一个带有main方法的jar包,artifact===> 点击+ ====> jar =====> From modules with dependcies ===> 选择module main方法类
  4. 运行方式
  • java -cp jar包 com.test.main主类
  • java -jar jar包

Maven打包方式

  1. maven-jar-plugin,打出来是一个不包含依赖的包几十kb大小
<plugin>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-jar-plugin</artifactId>
                                    <version>3.0.2</version>
                                    <configuration>
                                        <archive>
                                            <addMavenDescriptor>false</addMavenDescriptor>
                                            <manifest>
                                                <addClasspath>true</addClasspath>
                                                <classpathPrefix>lib/</classpathPrefix>
                                                <mainClass>com.test</mainClass>
                                            </manifest>
                                            <manifestEntries>
                                                <Class-Path>./</Class-Path>
                                            </manifestEntries>
                                        </archive>
                                        <!-- 过滤掉不希望包含在jar中的文件  -->
                                        <excludes>
                <!--                            <exclude>*.xml</exclude>-->
                <!--                            <exclude>spring/**</exclude>-->
                <!--                            <exclude>config/**</exclude>-->
                                        </excludes>
                <!--                        <includes>-->
                <!--                            <include>**.keytab</include>-->
                <!--                            <include>*.conf</include>-->
                <!--                        </includes>-->
                                    </configuration>
                                </plugin>
  1. maven-assembly-plugin打包方式,打出来也是一个不包含依赖的包,几十kb大小
 <plugin>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-assembly-plugin</artifactId>
                                    <configuration>
                                        <descriptorRefs>
                                            <descriptorRef>jar-with-dependencies</descriptorRef>
                                        </descriptorRefs>
                                        <archive>
                                            <manifest>
                                                <mainClass>com.test</mainClass>
                                            </manifest>
                                        </archive>
                                    </configuration>

                                    <executions>
                                        <execution>
                                            <id>make-assembly</id>
                                            <phase>package</phase>
                                            <goals>
                                                <goal>single</goal>
                                            </goals>
                                        </execution>
                                    </executions>
                                </plugin>

3.maven-shade-plugin可以打包出来一个包含依赖的包

<plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>3.0.0</version>

                    <executions>
                        <!-- Run shade goal on package phase -->
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <artifactSet>
                                    <excludes>
                                        <exclude>org.apache.flink:force-shading</exclude>
                                        <exclude>com.google.code.findbugs:jsr305</exclude>
                                        <!--                                          <exclude>org.slf4j:*</exclude>-->
                                        <!--                                        <exclude>log4j:*</exclude>-->
                                    </excludes>
                                </artifactSet>
                                <filters>
                                    <filter>
                                        <!-- Do not copy the signatures in the META-INF folder.
                                        Otherwise, this might cause SecurityExceptions when using the JAR. -->
                                        <artifact>*:*</artifact>
                                        <excludes>
                                            <exclude>META-INF/*.SF</exclude>
                                            <exclude>META-INF/*.DSA</exclude>
                                            <exclude>META-INF/*.RSA</exclude>
                                            <exclude>resources</exclude>
                                        </excludes>
                                    </filter>
                                </filters>
                                <transformers>
                                    <transformer
                                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>com.test</mainClass>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
posted @ 2022-08-01 13:58  堕落先锋  阅读(775)  评论(0编辑  收藏  举报