Idea packs a jar in a variety of ways, using idea's packaged form, packaged with the Maven plugin Maven-shade-plugin in Idea's own packaged form, packaged with the Maven plugin Maven-assembly-plugin

Source: Internet
Author: User

Here's a summary of the many ways to package a jar with idea, and later package packages can refer to the following form:

    1. The package form with idea comes in
    2. Package with Maven plugin maven-shade-plugin
    3. Package with Maven plugin maven-assembly-plugin
The way to pack with idea comes in:

Open idea file -> Project Structure and go to the project configuration page. Such as:

Click on Artifacts , go to the Artifacts configuration page, click +, select the option like.

Enter Create JAR from Modules the page, as configured.

    1. The first step is to select the class that the main function executes.
    2. The second step selects the option to do additional configuration when packaging the third-party jar package, but does not choose this option if no additional configuration is required (but does not guarantee package success)
    3. The third step needs to be in the src/main directory, create a new resources directory, MANIFEST.MF Save the file in this area, because if the default defaults, there will be a bug in the IDEA12 version.

After clicking, it OK appears as interface, 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:

After putting it in, click on the name of the jar that we want to play, which is kafka-cps.jar , select Classpath to configure.

The results of the edits are as follows:

This is where all the jars are written libs/ . Click to OK go back to the configuration page.
Also note on the configuration page, tickbuild on make

Finally click on the configuration page OK to complete the configuration. Back to Idea, click Build->Build Artifacts , selectbuild

Will generate the jar packages we need. Its location is under the Out directory of the project directory /out/artifacts/kafka_cps_jar .
Put a properly configured manifest file content below

Packing with Maven-shade-plugin

The packaging process above is too cumbersome and does not take advantage of the features of MAVEN management projects. To do this, we take advantage of the plugins in Maven maven-shade-plugin . 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><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> Span class= "Hljs-tag" ></PLUGIN>        

This is configured with a ' configuration ' tag, which has a label underneath it, which is transformer used to configure the entry for the main function ( <mainClass>Main.Main</mainClass> ), which is very complex, not as simple as it says, because it is so simple in all classes ( Includes a third-party jar) has only one main method. If there is a main method in a third-party jar, additional configuration is necessary, and the above configuration is not necessarily successful.

Refer to the Maven plugin for details.

After adding this code to Pom.xml, we can use MAVEN's command to pack. The instructions are as follows:

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

Then java -jar cps-1.0-SNAPSHOT.jar run through.

If you are using idea, you can use your own MAVEN management tool instead of executing the above command. Click the Blue section, as shown.

Packing with Maven-assembly-plugin

The above method, we also need to click a lot of commands to pack. This time with a new plugin, you can pack more easily. Similarly, add the following code to the Pom.xml. The maven-shade-plugin plugin code above can be removed. It is best not to write 2 plugin code.

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

A manifest tag is also configured here to configure the entry for the main function. The package is then implemented with the following instructions.

assembly:assembly

If you are using idea, you can use your own MAVEN management tool instead of executing the above command. As shown, click on the Blue section.

Then run by execution java -jar cps-1.0-SNAPSHOT-jar-with-dependencies.jar .

Idea packs a jar in a variety of ways, using idea's packaged form, packaged with the Maven plugin Maven-shade-plugin in Idea's own packaged form, packaged with the Maven plugin Maven-assembly-plugin

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.