Here we summarize the various ways to use IDEA to package JAR packages. You can refer to the following form for future projects to package JAR packages:
Use IDEA's own packaging method:
Open IDEA's file -> Project Structure and enter the project configuration page. As shown in the figure below:
Click Artifacts to enter the Artifacts configuration page, click + and select the options in the figure below.
Enter the Create JAR from Modules page and configure it as shown in the figure below.
After clicking OK , the following interface appears. Right-click <output root>, click Create Directory , create a libs , and put all third-party JARs into the libs directory.
After success, as shown in the following figure:
After putting in, click the name of the jar we want to type, which is kafka-cps.jar and select classpath for configuration.
The edited results are as follows:
All jars are written in libs/ . Click OK to return to the configuration page.
At the same time, please note that on the configuration page, check build on make
Finally, click OK on the configuration page to complete the configuration. Back to IDEA, click Build->Build Artifacts , and select build
The jar package we need will be generated. Its location is in /out/artifacts/kafka_cps_jar in the out directory of the project directory.
The following is a correctly configured manifest file content
Package with maven-shade-plugin
The above packaging process is really too cumbersome and does not take advantage of the features of maven management projects. To do this, we are using the maven-shade-plugin plugin in maven here. In pom.xml, we add the following information to add the plugin.
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>1.4</version> <configuration><createDependencyReducedPom>true</createDependencyReducedPom> </configuration> <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>
There is a 'configuration' tag in it. There is a transformer tag under this tag to configure the entrance to the Main function ( <mainClass>Main.Main</mainClass> ). Of course, the content of this tag is very complicated, not as simple as written above. The reason why the above is so simple is that there is only one Main method in all classes (including third-party Jars). If there is a Main method in a third-party jar, additional configuration is required. The above configuration may not be successful.
After adding this code to pom.xml, we can use the maven command to package it. The instructions are as follows:
mvn clean compile //Clear the previous target compile file and recompile mvn clean package //Package the project (because the plug-in is configured, the jar package is executable) mvn clean install //Installe the project, and then you can use it
Then run it through java -jar cps-1.0-SNAPSHOT.jar .
If you use IDEA, you can use the included maven management tool to execute the above command instead. As shown in the figure below, click the blue part in turn.
Package with maven-assembly-plugin
We also need to click on many commands to package the above method. This time, using a new plug-in, it can be easier to package. Similarly, add the following code to pom.xml. The above maven-shade-plugin plugin code can be deleted. It is best not to write 2 plug-in codes.
<plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</version> <configuration> <descriptorRefs> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>Main.Main</mainClass> </manifest> </archive> </configuration> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
Here is also a manifest tag to configure the entrance to the Main function. Then use the following instructions to realize packaging.
mvn assembly:assembly
If you use IDEA, you can use the included maven management tool to execute the above command instead. As shown in the figure below, click on the blue section.
Then run by executing java -jar cps-1.0-SNAPSHOT-jar-with-dependencies.jar .
The above is all the content of this article. I hope that the content of this article will be of some help to everyone’s study or work. I also hope to support Wulin.com more!