How to use Maven assembly plugin with multi module maven project

前端 未结 2 1024
南笙
南笙 2020-12-29 08:24

I am new to maven and spent ~3 days in generating the zip file with assembly plugin refering to http://www.petrikainulainen.net/programming/tips-and-tricks/creating-a-runnab

相关标签:
2条回答
  • 2020-12-29 08:42

    The basic thing you should change is to create a separate module where you do the packaging which will look like this.

       +-- root (pom.xml)
            +--- mod-1 (pom.xml)
            +--- mod-2 (pom.xml)
            +--- mod-assembly (pom.xml)
    

    The pom in the mod-assembly will look like this:

    <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>
    
      <parent>
        <groupId>org.test.parent</groupId>
        <artifactId>root</artifactId>
        <version>1.0.0-SNAPSHOT</version>
      </parent>
    
      <artifactId>dist</artifactId>
      <packaging>pom</packaging>
    
      <name>Packaging Test : Distribution</name>
    
      <dependencies>
        <dependency>
          <groupId>${project.groupId}</groupId>
          <artifactId>module-one</artifactId>
          <version>${project.version}</version>
        </dependency>
        <dependency>
          <groupId>${project.groupId}</groupId>
          <artifactId>module-two</artifactId>
          <version>${project.version}</version>
        </dependency>
      </dependencies>
    
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
              <execution>
                <id>make-bundles</id>
                <goals>
                  <goal>single</goal>
                </goals>
                <phase>package</phase>
                <configuration>
                  <descriptors>
                    <descriptor>proj1-assembly.xml</descriptor>
                  </descriptors>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    
    </project>
    

    That will solve the problem with running maven-assembly-plugin in every child and the problem with the dummy descriptor. Here you can find a real example of such structure.

    Furthermore having the modules in one folder and the dependencies in an other folder you can use a assembly descriptor like this:

      <id>bin</id>
      <formats>
        <format>zip</format>
      </formats>
      <includeBaseDirectory>false</includeBaseDirectory>
      <dependencySets>
        <dependencySet>
              <useProjectArtifact>false</useProjectArtifact>
              <useTransitiveDependencies>true</useTransitiveDependencies>
              <outputDirectory>lib</outputDirectory>
              <unpack>false</unpack>
              <excludes>
                <exclude>${project.groupId}:*:*</exclude>
              </excludes>
          </dependencySet>
      </dependencySets>
      <moduleSets>
        <moduleSet>
          <useAllReactorProjects>true</useAllReactorProjects>
          <binaries>
            <outputDirectory>modules</outputDirectory>
            <unpack>false</unpack>
          </binaries>
        </moduleSet>
      </moduleSets>
    
    0 讨论(0)
  • 2020-12-29 08:59

    I think the post How to Assemble Multi-Module Project with Maven might answer your question with an example.

    My answers to your questions:

    1) You should add the Assembly plugin in the parent pom within the build and pluginManagement tags to reuse it in the modules; the post mentioned above is a good example on how to do it. You can also take a look at 8.6 Best Practise chapter, topic 8.6.2, of the online book 'Maven: The Complete Reference'.

    2) The exclusion tag might be tricky. Again, take a look at 8.5 Controlling the Contents of an Assembly chapter of the online book 'Maven: The Complete Reference'. For example, the subtopic 8.5.4, about dependencySets mentions how to fine tune dependency includes and excludes for dependencySets; again, the subtopic 8.5.5, now about moduleSets, shows how to use it in that context.

    0 讨论(0)
提交回复
热议问题