How to create executable jar package in java? 4 ways

1. Manually modify the
/META-INF/ MANIFEST.MF
ordinary jar package
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: Administrator
Created-By: Apache Maven 3.3.3
Build-Jdk: 1.8.0_171

to change Into an executable jar package, you need to specify main
Main-Class: com.my.jvm.AssemblyTest.
If the jar package depends on other third-party jars, you must also specify Class-Path
Class-Path: lib/log4j-1.2.17.jar lib /slf4j-log4j12-1.7.21.jar lib/slf4 2. Assembly
plug-in: also type in the related dependency packages and execute them through assembly: assembly under plugins. The disadvantage is that the spirng project may report errors (not verified).
   

  <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>com.my.jvm.AssemblyTest</mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>
                                    jar-with-dependencies
                                </descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </plugin>


3. Shade plug-in (recommended), the function is the same as assembly, directly executed by mvn package

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.my.jvm.AssemblyTest</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

4. maven-jar-plugin (recommended), the advantage is that no dependencies are packaged, and the package that comes out is very small, directly execute mvn package
       

  <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <executions>
                    <execution>
                        <id>make-a-jar</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <archive>         
                        <manifest>             
                            <addClasspath>true</addClasspath>             
                            <classpathPrefix>lib/</classpathPrefix>             
                            <mainClass>com.my.jvm.AssemblyTest</mainClass>          
                        </manifest>      
                    </archive>
                    <excludes>
                        <exclude>*.properties</exclude>
                        <exclude>*.txt</exclude>
                    </excludes>
                </configuration>
            </plugin>

 

Guess you like

Origin blog.csdn.net/x18094/article/details/113556479