Several postures of Idea+Maven playing jar package

Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it

target

Our local projects should be released through Jar package or used by others. Today, I recorded the packaging process of the project completed by le idea and maven.

Using Idea+Maven, we can make the following three different jar packages:

  1. The imported dependent jar package is not included
  2. Contains all imported dependent jar packages
  3. Contains partially imported dependent jar packages

Take a simple project as an example:

1, Create a Maven project locally

When creating a project, we can choose to create a maven project.

2, Edit POM XML files and logical code

  1. If we want to type a jar package that does not contain dependencies
    At this point, our POM The XML file does not need special treatment, but only needs to import the jar package required by the project.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.chen</groupId>
    <artifactId>PinYinHelper</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <!--Import project dependency-->
    <dependencies>
        <dependency>
            <groupId>com.belerweb</groupId>
            <artifactId>pinyin4j</artifactId>
            <version>2.5.1</version>
        </dependency>
    </dependencies>
</project>


At this point, we can directly use the package instruction in Lifecycle in maven on the right to package. Double click the package instruction:

At this time, after maven's compilation and packaging, the project jar package without dependency can be generated in the target directory of our project path.

  1. If we want to package a jar package with dependencies
    At this point, our POM The XML file needs to be completed with the help of the assembly plug-in
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.chen</groupId>
    <artifactId>PinYinHelper</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <!--With the help of assembly The plug-in completes the packaging that contains project dependencies-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <!--Set to type in all dependencies jar In the bag-->
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <!-- Specify here main Method entry class -->
                            <mainClass>com.chen.Application</mainClass>
                        </manifest>
                    </archive>
                    <!--Specified type jar Package output directory-->
                    <outputDirectory>E:\IdeaWorkspace\PinYinHelper1\target</outputDirectory>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <!--take assembly The plug-in is bound to package On, you just need to double-click package Command is enough-->
                        <phase>package</phase>
                        <goals>
                            <goal>assembly</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <!--Import project dependency-->
    <dependencies>
        <dependency>
            <groupId>com.belerweb</groupId>
            <artifactId>pinyin4j</artifactId>
            <version>2.5.1</version>
        </dependency>
    </dependencies>
</project>

At this time, we also double-click the package instruction in Lifecycle in maven on the right, compile and package the plug-in, and generate jar packages containing all dependencies of the project in the jar package output directory we set.

  1. If the package containing jar depends on us
    For example, if you want to make a tool jar package, you need to rely on the public jar and your own local jar package. The local jar package needs to be decompressed into a class and typed into the jar package, while the dependent public jar package does not need to.
    Excluding public jar packages can be used
    Meaning of the value of:
    compile, the default value, is applicable to all phases and will be released with the project.
    provided, similar to compile, expects JDK, container or user to provide this dependency. Such as servlet jar.
    Runtime is only used at runtime, such as JDBC driver, which is applicable to the run and test phases.
    Test, used only during testing, is used to compile and run test code. Will not be published with the project.
    system, similar to provided, needs to explicitly provide a jar containing dependencies. Maven will not find it in the Repository.

For example, we add junit dependency to the project dependency, set it to compile in the scope attribute, and then type the dependency into the project package:


It can be seen that junit is typed into the jar package at this time.

If we set the scope attribute to test, try it in the package:


It can be seen that junit is not typed into the jar package at this time.

Executable jar package

If we specify the main entry class of the program when packaging with the assembly plug-in, the jar package printed at this time is an executable jar package, which can be executed through the cmd command line. The command is: Java - jar XXXX jar.

Keywords: Maven jar intellij-idea

Added by tinyang on Mon, 14 Feb 2022 17:15:47 +0200