如何整合Maven中子项目的构建工件?

时间:2012-05-06 16:58:00

标签: java maven

道歉,如果在其他地方有所涉及,我恐怕无法找到这个特别琐碎问题的答案。

我有一个maven项目,它引用并构建了4个子项目(作为bundle)。我已经正确地设置了子项目之间的依赖关系,最终我得到了4个不同的.jar文件构建:

  • parentproj / SUB1 /目标/ sub1.jar
  • parentproj / SUB2 /目标/ sub2.jar
  • parentproj / SUB3 /目标/ sub3.jar
  • parentproj / SUB4 /目标/ sub4.jar

我的问题是,我如何设置mu父版本,以便当我从主.POM文件构建时,所有4个jar放在“/ parentproj / target /...”?

另一个问题是,如何构建xxxx-ALL.jar(合并这4个包的内容)?

2 个答案:

答案 0 :(得分:3)

只需使用带有“dir”格式的Maven Assembly plugin将它们全部放在一个文件夹中,然后使用“jar”格式将它们分组到一个jar中。

更多细节:

  1. 您从maven项目开始(让我们称之为子项目)
  2. 创建一个新的maven项目(pom包装),使用<聚合所有子项目。模块>标签
  3. 将汇编插件放入pom.xml文件(见下文)
  4. 创建正确的assembly.xml文件(见下文)
  5. pom.xml文件:

    <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2.1</version>
        <executions>
            <execution>
                    <id>distro-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                        <finalName>somefolder</finalName>
                        <attach>false</attach>
                        <descriptors>
                        <descriptor>assembly.xml</descriptor>
                        </descriptors>
                    <phase>package</phase>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    assembly.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">
    <id>SomeName</id>
    <formats>
        <format>dir</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <baseDirectory></baseDirectory>
    
    <fileSets>
        <fileSet>
            <directory>appfiles</directory>
            <includes>
                <include>**</include>
            </includes>
            <outputDirectory>.</outputDirectory>
        </fileSet>
    </fileSets>
    
    <moduleSets>
        <moduleSet>
            <!-- useAllReactorProjects must be false if the assembly's run on the project with the module list -->
            <useAllReactorProjects>false</useAllReactorProjects>
            <includeSubModules>false</includeSubModules>
    
            <binaries>
                <includeDependencies>true</includeDependencies>
                <outputDirectory>bundle</outputDirectory>
                <unpack>false</unpack>
            </binaries>
        </moduleSet>
    </moduleSets>
    

答案 1 :(得分:1)

  

另一个问题是,如何构建xxxx-ALL.jar(合并这4个包的内容)?

您可以使用Maven shade plugin创建一个uber-jar(包含项目所有依赖项的jar)。只需创建另一个项目作为依赖项,并生成这个超级jar。