Various ways for IDEA to package jar packages

Here is a summary of the various ways to package jar packages with IDEA. For future projects to package jar packages, you can refer to the following forms:

  1. Use IDEA's own packaging form
  2. maven-shade-pluginPackaging with Maven Plugin
  3. maven-assembly-pluginPackaging with Maven Plugin

Use IDEA's own packaging method:

Open IDEA file -> Project Structureand enter the project configuration page. As shown below:

Click Artifactsto enter the Artifactsconfiguration page, click + , and select the option as shown in the figure below.

Enter the Create JAR from Modulespage and configure as shown below.

  1. The first step is to select the class that the Main function executes.
  2. The second step is to select the option as shown in the figure, the purpose is to make additional configuration when packaging the third-party Jar package. If no additional configuration is required, this option can not be selected (but the packaging is not guaranteed to be successful)
  3. The third step is to src/maincreate a new resourcesdirectory in the directory and MANIFEST.MFsave the file in it, because if the default value is used, there will be bugs in the IDEA12 version.

After clicking OK, the following interface appears, right-click <output root>, click Create Directory, create one libs, and put all third-party JARs into the libs directory.

After success, as shown in the following figure:

After putting it in, click the name of the jar we want to type, here is kafka-cps.jar, select the classpath to configure.

The edited result is as follows:

Here all the jars are written libs/inside. Click OKto return to the configuration page.
Also note that on the configuration page, check the boxbuild on make

Finally, click on the configuration page to OKcomplete the configuration. Go back to IDEA, click Build->Build Artifacts, selectbuild

The jar package we need will be generated. Its location is in the out directory of the project directory /out/artifacts/kafka_cps_jar.
Put the contents of a properly configured manifest file below

Packaged with maven-shade-plugin

上面的打包过程实在是过于的繁琐,而且也没有利用到maven管理项目的特色。为此,我们这里利用maven中的maven-shade-plugin插件。在pom.xml中,我们加入如下的信息来加入插件。

<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">
                                    <mainClass>Main.Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

这里面配置了一个`configuration`标签内容,在此标签下面 有一个transformer标签,用来配置Main函数的入口( <mainClass>Main.Main</mainClass>),当然此标签内容很复杂,不是上面写的那么简单,上面之所以如此简单,是因为在所有类中(包括第三方Jar)只有一个Main方法。如果第三方jar中有Main方法,就要进行额外的配置,上面这么配置,不一定能执行成功。

具体可以参见maven插件

在加入这段代码到pom.xml之后,我们就可以用maven的命令去打包了。其指令如下:

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

然后通过java -jar cps-1.0-SNAPSHOT.jar运行。

如果使用IDEA的话,可以通过自带的maven管理工具代替执行上面的命令。如下图所示,依次点击蓝色的部分。

用maven-assembly-plugin打包

上面的方法,我们还需要点击很多命令去打包。这次利用一个新的插件,可以打包更简单。同样,在pom.xml中加入如下代码。上文的maven-shade-plugin插件代码可以删除。最好不要写2个插件代码。

 <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>Main.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

这里同样配置了一个manifest标签来配置Main函数的入口。然后通过如下指令来实现打包。

mvn assembly:assembly

如果使用IDEA的话,可以通过自带的maven管理工具代替执行上面的命令。如下图所示,点击蓝色的部分。

然后通过执行java -jar cps-1.0-SNAPSHOT-jar-with-dependencies.jar运行。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326912497&siteId=291194637