如何将不同模块的类捆绑到jar?

时间:2014-01-03 07:33:32

标签: java maven intellij-idea

我有一个主项目(比如A),它包含模块B和模块C.模块C是模块B的依赖。除此之外,我想将模块C的所有类添加到模块B的jar中。通过在maven pom文件中添加C作为依赖项工作正常。但是模块B jar独立运行并且在运行时需要模块C类(已编译)。

我怎么能用maven做到这一点。 我正在使用JIdea 12。

2 个答案:

答案 0 :(得分:2)

使用maven shade plugin,并将其配置为将模块C映射到模块B或直接进入主项目。

答案 1 :(得分:2)

首先将C添加为B

的依赖项
<dependency>
        <groupId>A</groupId>
        <artifactId>C</artifactId>
        <version>1.0</version>
        <scope>provided</scope>
    </dependency>

然后在模块B的pom文件中使用maven shade插件,如下所示

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <finalName>${artifactId}-${version}</finalName>
            </configuration>
        </plugin>