当前位置:网站首页>Multiple ways for idea to package jars

Multiple ways for idea to package jars

2022-06-22 08:01:00 xiaoke815

idea pack jar In many ways


Here is a summary of IDEA pack jar There are many ways to pack , Future projects are packaged Jar Packages can refer to the following forms :

  1. use IDEA It's packed in its own form
  2. use Maven plug-in unit maven-shade-plugin pack
  3. use Maven plug-in unit maven-assembly-plugin pack

use IDEA It's packed in its own way :

open IDEA Of file -> Project Structure, Enter the project configuration page . Here's the picture :


Click on Artifacts, Get into Artifacts Configuration page , Click on + , Select the option shown in the figure below .


Get into Create JAR from Modules page , Configure as shown in the figure below .


  1. Step 1 selection Main Function execution class .
  2. Step 2 select the options shown in the figure , The purpose is for third parties Jar Do additional configuration when packaging packages , Do not select this option if no additional configuration is required ( But there is no guarantee that the package will succeed )
  3. The third step needs to be in src/main Under the table of contents , Create a new one resources Catalog , take MANIFEST.MF The files are kept here , Because if you use the default value , stay IDEA12 There will be bug.

Click on OK after , The following interface appears , Right click on the <output root>, Click on Create Directory, Create a libs, Will all third parties JAR In the libs Under the table of contents .


After success , As shown in the figure below :


After putting it in , Click on what we want to type jar Name , Inside is kafka-cps.jar, choice classpath To configure .


The result of editing is as follows :


Here will be all jar All written in libs/ Inside . Click on OK, Back to the configuration page .

Also note that on the configuration page , Check build on make


Finally, click... On the configuration page OK, Complete the configuration . go back to IDEA, Click on Build->Build Artifacts, choice build


Will generate what we need jar package . Its location is in the project directory out Under the table of contents /out/artifacts/kafka_cps_jar.

Now put the contents of a properly configured manifest file


use maven-shade-plugin pack

The above packaging process is too cumbersome , And it doesn't take advantage of maven Features of the management project . So , We use maven Medium maven-shade-plugin plug-in unit . stay pom.xml in , We add the following information to add the plug-in .

<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>

This is equipped with a `configuration` Label content , Under this label There is one transformer label , Used to configure Main Function entry ( <mainClass>Main.Main</mainClass>), Of course, the content of this tag is very complicated , It's not as simple as it says , The reason why the above is so simple , Because in all classes ( Including third parties Jar) only one Main Method . If a third party jar There is Main Method , Additional configuration is required , The above configuration , It may not be successful .

For details, please refer to maven plug-in unit .

Add this code to pom.xml after , We can use it maven The order to pack . The instructions are as follows :

mvn clean compile // Before clearing target Compile the file and recompile

mvn clean package// Package the project ( Because the plug-in has been configured , therefore jar Packages are executable )

mvn clean install // Installation project , Then you can use it

And then through java -jar cps-1.0-SNAPSHOT.jar function .

If you use IDEA Words , You can do it by bringing it with you maven Instead of executing the above command, the management tool . As shown in the figure below , Click on the blue part in turn .


use maven-assembly-plugin pack

The above method , We also need to click many commands to package . This time with a new plug-in , It can be packaged more easily . Again , stay pom.xml Add the following code to . The above maven-shade-plugin Plug in code can be deleted . Better not write 2 Plug in codes .

 <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>

There is also a manifest Tag to configure Main Function entry . Then the following instructions are used to implement the packaging .

mvn assembly:assembly

If you use IDEA Words , You can do it by bringing it with you maven Instead of executing the above command, the management tool . As shown in the figure below , Click on the blue part .


And then by executing java -jar cps-1.0-SNAPSHOT-jar-with-dependencies.jar function .


原网站

版权声明
本文为[xiaoke815]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202220530339072.html