在单个jar中组装多模块Maven项目

时间:2020-09-17 15:49:05

标签: java maven maven-3 maven-plugin maven-assembly-plugin

我有一个父Maven项目,其中包括一些子项目和一个构建/装配项目。该结构如下所示,其中buildProj不包含任何类

ParentProj
    + pom.xml 
    + ChildProj1
        ++   pom.xml
    + ChildProj2
     ++   pom.xml
    + buildProj
     ++   pom.xml

这是buildProj的pom.xml:

<?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>gr1</groupId>
    <artifactId>buildproj</artifactId>
    <version>2.0.0</version>

    <dependencies>
        <dependency>
            <groupId>gr1</groupId>
            <artifactId>ChildProj1</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>gr1</groupId>
            <artifactId>ChildProj2</artifactId>
            <version>2.0.0</version>
        </dependency>
    </dependencies>


    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>com.bonanza.CabalImpl</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>



</project>

尽管如此,当我执行mvn package时,buildProj的jar是空的

1 个答案:

答案 0 :(得分:1)

尝试在版本标记下添加

<packaging>jar</packaging>

稍后修改 如果您使用的是程序集插件,则还应该具有程序集XML

src / assembly / bin.xml

<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<formats>
    <format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <unpack>false</unpack>
        <includes>
            <include>${artifact}</include>
        </includes>
    </dependencySet>
    <dependencySet>
        <outputDirectory>/lib</outputDirectory>
        <unpack>false</unpack>
        <excludes>
            <exclude>${artifact}</exclude>
        </excludes>
    </dependencySet>
</dependencySets>

并打包

mvn clean package assembly:single

这也是您更新的pom

<build>
   <pluginManagement>
      <plugins>
         <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
               <archive>
                  <manifest>
                     <mainClass>com.bonanza.CabalImpl</mainClass>
                  </manifest>
               </archive>
               <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
               </descriptorRefs>
            </configuration>
            <executions>
               <execution>
                  <id>make-assembly</id>
                  <phase>package</phase>
                  <goals>
                     <goal>single</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </pluginManagement>
</build>