对具有特殊结构的多模块项目的Maven组件(Maven assembly on multi modu

2019-07-29 14:09发布

我是新来的Maven,我想我已经开始得到它是如何工作的想法。 但我不能够理解的maven组件插件。 我想实现这一目标是:

当所有的项目已经打包,各自的依赖,我想有他们所有的目标目录。 我不希望他们因为系统是基于模块打包成一个超级罐子。

让我解释一下,我有一个主要项目,服务器,在Maven项目“共同”,我有两个模块,“核心”和“机器人”。 在共同文件夹,还有,我想复制一个conf文件夹。 我想这样的结构:

  • 目标/ common.jar
  • 目标/ CONF /(配置文件)
  • 目标/模块/ core.jar添加
  • 目标/模块/的android.jar

我的项目结构是这样的:

  • pom.xml的(父项目)
  • 公共/(行家模块)
  • 芯/(行家模块)
  • 机器人/(行家模块)

谢谢您的帮助,或以正确的方式指针。 :)

编辑这里是ant构建文件,它的工作原理100%,也许我应该跟上吗?

<target name="init">
    <mkdir dir="dist" />
    <mkdir dir="dist/conf/" />
    <mkdir dir="dist/modules/" />
    <mkdir dir="dist/libs/" />

    <copy includeemptydirs="false" todir="dist/conf">
        <fileset dir="common/conf" />
    </copy>
</target>

<target name="copy-server">
    <copy todir="dist">
        <fileset file="common/target/server*.jar" />
    </copy>
</target>

<target name="copy-modules">
    <copy todir="dist/modules/">
        <fileset file="core/target/*.jar" />
        <fileset file="android/target/*.jar" />
    </copy>
</target>

<target name="copy-libs">
    <copy todir="dist/libs">
        <fileset dir="common/target/libs" />
        <fileset dir="core/target/libs" />
        <fileset dir="android/target/libs" />
    </copy>
    <delete>
        <fileset file="dist/libs/server*.jar" />
    </delete>
</target>

<target name="clean">
    <delete dir="dist" />
</target>

<target name="full-build" depends="clean, init, copy-server, copy-libs, copy-modules, increment">
    <echo message="Copying the fully built Maven project" />
</target>

<target name="increment">
    <propertyfile file="common/conf/version.properties">
        <entry key="build.number" type="int" operation="+" default="0" />
    </propertyfile>
    <property file="common/conf/version.properties" />
    <echo message="Build number is ${build.number}"/>
</target>

Answer 1:

首先,什么Assembly插件的作用:它使得它很容易使包含一个Maven项目的构件(S)的依赖,以及其他相关文件的tar或zip压缩文件。

这听起来像所有你需要做的是建立组装插件使用自定义描述在你感兴趣的罐子和配置文件来拉如果你有一个表示“分配”的东西一个单独的模块-即使模块依赖于其他模块-那么你可能只需要加一个装配描述符它包含了一些文件 , 文件集 ,和/或dependencySets 。

如果有多个模块项目要包括并没有什么依赖于所有的人,那么你会想看看moduleSets 。 我从来没有对那些使用自己,但他们正是出于这个问题。



Answer 2:

只要你想它这样的设置会正是解决这个问题。

directory layout

+- pom.xml
+- android
| +- pom.xml
| +- src
|   +- main
|     +- java
+- core
| +- pom.xml
| +- src
|   +- main
|     +- java
+- common
  +- pom.xml
  +- src
    +- main
     +- java
     +- resources
       +- conf

pom.xml

<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>com.example</groupId>
    <artifactId>my-project</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0-SNAPSHOT</version>

    <name>${project.artifactId}-${project.version}</name>

    <modules>
        <module>android</module>
        <module>common</module>
        <module>core</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>core</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>android</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

android/pom.xml

<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>com.example</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>android</artifactId>
    <packaging>jar</packaging>

    <name>${project.artifactId}-${project.version}</name>

</project>

core/pom.xml

<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>com.example</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>core</artifactId>
    <packaging>jar</packaging>

    <name>${project.artifactId}-${project.version}</name>

</project>

common/pom.xml

<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>com.example</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>common</artifactId>
    <packaging>jar</packaging>

    <name>${project.artifactId}-${project.version}</name>

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>android</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <targetPath>${project.build.directory}</targetPath>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/modules</outputDirectory>
                            <stripVersion>true</stripVersion>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

这最后一个POM是在那里你可以根据自己的需要改变自己的行为。

如果你想收拾这一切在一些尤伯杯罐子,那么你可以这样做。


编辑

好了,看完您的意见和看着你的Ant构建脚本后,我想出了以下设计/安装,这将使你或多或少你想要什么。

directory layout

+- pom.xml
+- android
| +- pom.xml
| +- src
|   +- main
|     +- java
+- core
| +- pom.xml
| +- src
|   +- main
|     +- java
+- common
| +- pom.xml
| +- conf.xml
| +- src
|   +- main
|    +- java
|    +- resources
|      +- conf
+- dist
  +- pom.xml

pom.xml

<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>com.example</groupId>
    <artifactId>my-project</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0-SNAPSHOT</version>

    <name>${project.artifactId}-${project.version}</name>

    <modules>
        <module>android</module>
        <module>common</module>
        <module>core</module>
        <module>dist</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>common</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>common</artifactId>
                <version>${project.version}</version>
                <classifier>conf</classifier>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>core</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>android</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

android/pom.xml

<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>com.example</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>android</artifactId>
    <packaging>jar</packaging>

    <name>${project.artifactId}-${project.version}</name>

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common</artifactId>
        </dependency>
    </dependencies>
</project>

core/pom.xml

<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>com.example</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>core</artifactId>
    <packaging>jar</packaging>

    <name>${project.artifactId}-${project.version}</name>

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common</artifactId>
        </dependency>
    </dependencies>
</project>

common/pom.xml

<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>com.example</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>common</artifactId>
    <packaging>jar</packaging>

    <name>${project.artifactId}-${project.version}</name>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <id>assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>conf.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

common/conf.xml

该组件将描述符打包conf文件在一个单独的罐子,任何项目将能够在其上具有相关性。

<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>conf</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${basedir}/src/main/resources/conf</directory>
            <outputDirectory>conf</outputDirectory>
            <includes>
                <include>*.properties</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

dist/pom.xml

该DIST模块将解压的conf的依赖和其他依赖复制到目标目录。

<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>com.example</groupId>
        <artifactId>my-project</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>dist</artifactId>
    <packaging>pom</packaging>

    <name>${project.artifactId}-${project.version}</name>

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common</artifactId>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>common</artifactId>
            <classifier>conf</classifier>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>android</artifactId>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>core</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>modules</id>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.example</groupId>
                                    <artifactId>android</artifactId>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>com.example</groupId>
                                    <artifactId>core</artifactId>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>com.example</groupId>
                                    <artifactId>common</artifactId>
                                    <outputDirectory>${project.build.directory}</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                            <outputDirectory>${project.build.directory}/modules</outputDirectory>
                            <stripVersion>true</stripVersion>
                        </configuration>
                    </execution>
                    <execution>
                        <id>unpack</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.example</groupId>
                                    <artifactId>common</artifactId>
                                    <classifier>conf</classifier>
                                </artifactItem>
                            </artifactItems>
                            <excludes>**/MANIFEST.MF</excludes>
                            <outputDirectory>${project.build.directory}</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

我倾向于喜欢与工作maven-dependency-plugin ,我觉得很厉害。

该DIST模块将拥有所有必要的依赖于其他模块和下载并解压缩,如你所愿。 然后,如果您想拥有它全部装在一个zip或tar.gz的或其他格式,那么你可以使用maven-assembly-plugin为。



Answer 3:

建设项目时Maven会默认为以下结构:

  • 公共/目标/共的.jar
  • 芯/目标/芯的.jar
  • 机器人/目标/机器人-的.jar

然而,一切都没有失去! 与Maven中大多数的选项,这可以很容易地使用适当的配置标签覆盖。 在你的情况,你要设置你的标签下的输出目录标签(每个模块POMS),以正确的目录。

例如,您的$ {BASEDIR} /common/pom.xml应编辑添加此行:

...
<build>
    <outputDirectory>../target</outputDirectory>
...


你./android/pom.xml将类似编辑:

...
<build>
    <outputDirectory>../target/modules</outputDirectory>
...

我不知道在配置文件复制的最佳方式。 你可能想尝试寻找(或写,因为它是一个相当简单的操作)一个插件,简单地在复制文件,并给它一个范围,如“编译”,而在正确的目录指向它。



文章来源: Maven assembly on multi module project with special structure