Package and generate an executable jar package using Maven implementation: detailed configuration explanation is attached

Keywords: Linux Operation & Maintenance Kubernetes

1. Demand

The project shall be packaged to meet the following requirements:

1. Make a Zip package for the whole project, including the application, the jar package that the application depends on, and the documentation
2. The jar package of the project can execute the Main function in different classes
3. The jar package of the project source code should be separated from the dependent third-party jar package
4. The execution scripts in the project should also be packaged and classified
5. The readme.txt in the document directory is placed in the root directory of the compressed package, and the others are also placed in this directory
6. Remove unnecessary directories (files) from the jar package

2. Development environment

IDEA-2016 Maven3.3.9

Directory structure of the project:

3. Introduction to Maven packaging plug-in

Assembly translates to assembly
Maven has three common packaging plug-ins for project packaging, namely:

plug-in unitfunction
maven-jar-pluginmaven packages the plug-in by default, which is used to create project jar s
maven-shade-pluginType the executable package, executable(fat) jar
maven-assembly-pluginSupport custom packaging method

Maven jar plugin and Maven assembly plugin are used here
Project directory:

clean the jar package before you find it, otherwise the IDEA will think that your project has not been modified and will not be reloaded

Note: the comments of the configuration file are very detailed, so we won't explain it here

4. Maven uses Maven jar plugin to print executable jar packages

The main configurations are as follows:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.4</version>
  <!-- Yes, I'll take a taxi jar Package configuration -->
  <configuration>
    <!-- Configuration of the archiver -->
    <archive>
      <!--Generated jar Do not include pom.xml and pom.properties These two documents-->
      <addMavenDescriptor>false</addMavenDescriptor>

      <!-- Manifest specific configuration -->
      <manifest>
        <!--Do you want to send the third party jar put to manifest of classpath in-->
        <addClasspath>true</addClasspath>
        
        <!--Generated manifest in classpath Prefix of,
        Because the third party jar put to lib Under the directory,
        therefore classpath The prefix is lib/-->
        <classpathPrefix>lib/</classpathPrefix>
      </manifest>
    </archive>
    <!--Filter out unwanted items jar Files in-->
    <excludes>
      <!-- Exclude unwanted folders(Path is jar Path inside package) -->
      <exclude>**/assembly/</exclude>
    </excludes>
  </configuration>
</plugin>

See bottom for complete configuration

5. Maven uses Maven assembly plugin to pack the files to be packaged into the zip package

The main configurations under pom.xml are as follows:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.4</version>
  <!-- Configure the assembly of the project -->
  <configuration>
    <!-- appoint assembly Location of the plug-in's configuration file -->
    <descriptors>
      <descriptor>src/main/resources/assembly/package.xml</descriptor>
    </descriptors>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id>
      <!-- Bind assembly to maven What stage of the life cycle -->
      <phase>package</phase>
      <goals>
        <!-- appoint assembly Packaging method of plug-ins-->
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

The configuration file package.xml of the assembly plug-in is shown at the bottom

6. Maven generates executable jar packages and zip project packages

Double clicking mvn:package will generate two packages: executable jar package and project compressed package, because the assembly configuration is bound to it
Double click to execute assembly:single to generate only the compressed package of the project

Execute mvn:package here

Directory structure of unzipped project package:

7. Execute the jar package

Unzip the generated project package
TestString source code:

public class TestString {
  public static void main(String[] args) {
    String[] arr = new String[]{"aaa", "bbb", "ccc", "DDD", "EEE", "FFF"};
    System.out.println(StringUtils.join(arr, "---"));
  }
}

TestNumber source code:

public class TestNumber {
  public static void main(String[] args) {
    Integer[] arr = new Integer[]{11, 22, 33, 44, 55, 66};
    System.out.println(StringUtils.join(arr, "---"));
  }
}

Run the generated jar from the command line

java -classpath dong.jar com.dong.bigdata.TestString
java -classpath dong.jar com.dong.bigdata.TestNumber

Operation results:

8. pom.xml configuration

Contains two files:
pom.xml overall configuration
package.xml is contained in pom.xml, which is used to specify the configuration of assembly assembly

pom.xml file:

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<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>
  <!-- ####################### Basic settings ###################### -->
  <!--groupId:The unique flag of the project or organization, and the generation path is also generated during configuration, such as org.myproject.mojo The generated relative path is:/org/myproject/mojo-->
  <groupId>com.dong</groupId>
  <!--Common name of the project-->
  <artifactId>bigdata</artifactId>
  <!--Packaging mechanism, such as pom,jar,maven-plugin,ejb,war,ear,rar,par-->
  <packaging>jar</packaging>
  <!--Version of the project-->
  <version>1.0-SNAPSHOT</version>

  <!-- ####################### Project information ###################### -->
  <!--The user describes the name of the project, something that doesn't matter-->
  <name>bigdata</name>
  <!--It doesn't matter to write down the development team's website-->
  <url>http://http://www.dong.com/.com</url>

  <!-- ####################### Environment settings ###################### -->
  <properties>
    <!-- Project execution script directory -->
    <project.script.execute.directory>src/main/scripts/execute</project.script.execute.directory>
    <!-- Project description document directory -->
    <project.document.directory>document</project.document.directory>
    <!-- Project profile directory -->
    <project.config.directory>src/main/resources</project.config.directory>
    <!-- Item code -->
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <!-- Local compilation JDK edition -->
    <maven.compiler.source>1.8</maven.compiler.source>
    <!-- Project deployment JDK edition -->
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <!--
   to configure Maven Warehouse, Warehouses configured here take precedence over setting.xml Warehouse configured in,
   Which warehouse is recommended to be fast,Which configuration comes first, Then if Maven If you can't find the warehouse configured in the front, you will find it in the back warehouse,
   If I can't find the warehouse in the back, I'll go setting.xml In the central warehouse
   -->
  <repositories>
    <!-- Alibaba cloud warehouse,to configure Maven The warehouse is fast, and the configuration is at the front -->
    <repository>
      <id>aliyun</id>
      <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </repository>
    <!-- Domestic alternative warehouse -->
    <repository>
      <id>repo2</id>
      <url>http://repo2.maven.org/maven2/</url>
    </repository>

    <!-- Cloudera Warehouse,If you can't find it in the Alibaba cloud warehouse Cloudera In the warehouse, mainly CDH edition Hadoop Dependent jar -->
    <repository>
      <id>cloudera</id>
      <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
    </repository>

    <!-- Scala Warehouse,If the first two cannot be found, go to the central warehouse to find it -->
    <repository>
      <id>scala-tools.org</id>
      <name>Scala-Tools Maven2 Repository</name>
      <url>http://scala-tools.org/repo-releases</url>
    </repository>
  </repositories>

  <dependencies>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.4</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>dong</finalName>
    <plugins>
      <!-- The configuration of maven-jar-plugin -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <!-- Yes, I'll take a taxi jar Package configuration -->
        <configuration>
          <!-- Configuration of the archiver -->
          <archive>
            <!--Generated jar Do not include pom.xml and pom.properties These two documents-->
            <addMavenDescriptor>false</addMavenDescriptor>

            <!-- Manifest specific configuration -->
            <manifest>
              <!--Do you want to send the third party jar put to manifest of classpath in-->
              <addClasspath>true</addClasspath>

              <!--
              Generated manifest in classpath Prefix of,
              Because the third party jar put to lib Under the directory,
              therefore classpath The prefix is lib/
              -->
              <classpathPrefix>lib/</classpathPrefix>
            </manifest>
          </archive>
          <!--Filter out unwanted items jar Files in-->
          <excludes>
            <!-- Exclude unwanted folders(Path is jar Path inside package) -->
            <exclude>**/assembly/</exclude>
          </excludes>
        </configuration>
      </plugin>

      <!-- The configuration of maven-assembly-plugin -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <!-- Configure the assembly of the project -->
        <configuration>
          <!-- appoint assembly Location of the plug-in's configuration file -->
          <descriptors>
            <descriptor>src/main/resources/assembly/package.xml</descriptor>
          </descriptors>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <!-- Bind assembly to maven What stage of the life cycle -->
            <!--<phase>package</phase>-->
            <goals>
              <!-- appoint assembly Packaging method of plug-ins-->
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

9. package.xml file

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<assembly>
  <id>full</id>
  <!-- Finally packaged into a for publishing zip file -->
  <formats>
    <format>zip</format>
  </formats>

  <!-- Put dependence jar Package into Zip Compressed file lib Directory -->
  <dependencySets>
    <dependencySet>
      <!--Not using project artifact,Third party jar Don't unpack it, pack it in zip Document lib catalogue-->
      <useProjectArtifact>false</useProjectArtifact>

      <!-- Third party jar Pack in Zip Document lib Under the directory, -->
      <!-- Note that this directory should be consistent with maven-jar-plugin in classpathPrefix The specified directory is the same, -->
      <!-- Otherwise, these dependencies jar Package loaded into ClassPath You won't find it when you get there-->
      <outputDirectory>lib</outputDirectory>

      <!-- Third party jar Do not decompress-->
      <!--<unpack>false</unpack>-->
    </dependencySet>
  </dependencySets>

  <!-- File settings, which files you want to include or exclude, are configured here-->
  <fileSets>
    <!-- The executable that compiles the project itself jar,Pack in zip The root directory of the file -->
    <fileSet>
      <directory>${project.build.directory}</directory>
      <outputDirectory></outputDirectory>
      <includes>
        <include>*.jar</include>
      </includes>
    </fileSet>

    <!--
    Put the project readme Documentation, packaged in zip File root directory
    (This is for directories document/readme.txt file)
    ${projet.document.directory}yes pom.xml Self configured in
     -->
    <fileSet>
      <directoryl>${projet.document.directory}</directoryl>
      <outputDirectory></outputDirectory>
      <includes>
        <include>readme.*</include>
      </includes>
    </fileSet>

    <!--
    Put the project related instruction documents(except readme file),
    Pack in zip Under the root directory of the file document catalogue
    (Here for document/exclode.txt file)
    ${project.document.directory}Yes pom.xml Self configured in
    -->
    <fileSet>
      <directory>${project.document.directory}</directory>
      <outputDirectory>document</outputDirectory>
      <excludes>
        <exclude>readme.*</exclude>
      </excludes>
    </fileSet>

    <!--
    Put the script file directory of the project(src/main/scripts )Startup script file in,
    Pack in zip The root directory of the file
    (This is for src/scripts/execute/include-file.sh file)
    ${project.script.execute.directory}
    -->
    <fileSet>
      <directory>${project.script.execute.directory}</directory>
      <outputDirectory></outputDirectory>
      <includes>
        <include>*</include>
      </includes>
    </fileSet>

  </fileSets>
</assembly>

Reference link: https://www.yisu.com/zixun/313235.html

Posted by tgavin on Tue, 16 Nov 2021 20:07:42 -0800