maven tutorial: build multiple jar packages according to different configuration files

Example of dir1-assembly.xml

The following is an example dir1-assembly.xmldescriptor file for building src/main/resources/dir1a JAR against a directory:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
  <id>dir1-assembly</id>
  <formats>
    <format>jar</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>src/main/resources/dir1</directory>
      <outputDirectory>/</outputDirectory>
    </fileSet>
  </fileSets>
</assembly>

In the above example, we defined an <assembly>element specifying the namespace and version of the descriptor file.

<id>element specifies the ID of the descriptor file, corresponding to the element in pom.xml.<execution>assemblyId

<formats>The element specifies the format of the JAR package to be generated, here it is set to jar.

<fileSets>element defines the collection of files to include. In this example, we define an <fileSet>element specifying src/main/resources/dir1directories as source directories to include and output to the root directory ( /).

You can add other <fileSet>elements to the descriptor file as needed to include more directories and files.

Hope this example helps you understand how to write dir1-assembly.xmla descriptor file to build src/main/resources/dir1a directory-specific JAR package. Please make appropriate adjustments according to your project structure and needs.

Build multiple jar packages according to different configuration files

To build different JAR packages according to different directories, maven-assembly-pluginmultiple configurations can be used to achieve.

The following is an example configuration that demonstrates how to use maven-assembly-plugindifferent directories to build different JAR packages:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <version>3.3.0</version>
      <executions>
        <execution>
          <id>dir1-assembly</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
          <configuration>
            <descriptors>
              <descriptor>src/main/assembly/dir1-assembly.xml</descriptor>
            </descriptors>
            <appendAssemblyId>false</appendAssemblyId>
          </configuration>
        </execution>
        <execution>
          <id>dir2-assembly</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
          <configuration>
            <descriptors>
              <descriptor>src/main/assembly/dir2-assembly.xml</descriptor>
            </descriptors>
            <appendAssemblyId>false</appendAssemblyId>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

In the above example, we defined two <execution>elements in the configuration section of the plugin, corresponding to builds of dir1and .dir2

In each <execution>element, we specify a custom descriptor file, such as src/main/assembly/dir1-assembly.xmland src/main/assembly/dir2-assembly.xml, corresponding to the configuration of dir1and respectively dir2.

In each descriptor file, you define which directories, files, and other resources need to be included. You can configure it as needed to meet the needs of a specific directory.

When executing the command, the following command can be used to build a specific JAR package:

mvn clean package -DassemblyId=dir1-assembly

or

mvn clean package -DassemblyId=dir2-assembly

Wherein, -DassemblyIdthe parameter specifies the ID of the JAR package to be built, that is, the ID corresponding to the descriptor file.

Hope this example can help you build different JAR packages according to different directories. Please make appropriate adjustments according to your project structure and needs.

Guess you like

Origin blog.csdn.net/a772304419/article/details/132662917