从多模块maven项目构建单个胖罐 - 包括测试类

kal*_*ada 6 testing selenium maven

我正处于需要从多模块maven项目中构建胖罐的情况.该项目基于Selenium/cucumber API构建.

我的项目结构如下

   Parent -- pom
   |
   |__core_module src/main/java --> helper classes for selenium
   |
   |__acme_module src/main/test --> Test Classes for acme project
Run Code Online (Sandbox Code Playgroud)

我尝试了不同的方法来构建一个"acme_test.jar",其中包括core_module + acme_module.But它们都没有帮助我.

非常感谢解决这个问题的任何线索.

谢谢

Yia*_*kis 4

正如 @MariuszS 提到的,首先重构您的项目,以便将测试(=驱动/验证导航)Acme 的实际类的任何单元测试或集成测试分开。

   Parent -- pom
   |
   |__core_module src/main/java --> helper classes for selenium
   |
   |__acme_module src/main/java --> Classes specific for navigating acme 
   |
   |__acme_module src/test/java--> (Unit etc) Test Classes for acme project
Run Code Online (Sandbox Code Playgroud)

然后,您需要 acme_module 包含 core_module 作为依赖项。

最后,在 acme_module 中,将其放入构建插件部分:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.3</version>
    <executions>
        <execution>
            <id>create-fat-jar</id>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                <!-- add Main-Class to manifest file -->
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>your.main.class</mainClass>
                    </transformer>
                </transformers>
                    <finalName>YourJarName</finalName>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

您还可以通过在插件配置部分添加包含过滤器来显式包含 core_module

  • 或者,如果您不希望将模块解压到阴影 jar 中,则可以使用 [onejar-maven-plugin](https://code.google.com/archive/p/onejar-maven-plugin/) 以便创建嵌套 JAR 的 uber JAR,即有一个特殊的类加载器直接从 uber JAR 加载未更改的 JAR。也可通过 [this fork](https://github.com/jolira/onejar-maven-plugin) 在 Maven Central 上获取。如果你(kallada)想了解更多,并且我有机会让你接受它作为答案,请告诉我,我会准备一个。但 Maven Shade 也能完成这项工作,这个答案是正确的。 (2认同)