1

我有一个像这样的多模块项目

foobar
 |
 +-- pom.xml
 |
 +-- common-lib/
 |       |
 |       +-- pom.xml
 |       +-- src/
 |
 +-- foo-app/
 |       |
 |       +-- pom.xml
 |       +-- src/
 |
 +-- bar-app/
 |       |
 |       +-- pom.xml
 |       +-- src/
 |
-+-

两者都foo-app依赖bar-app于代码,common-lib也依赖于它们自己的 POM 中的依赖关系。

使用mvn packageI 可以构建三个轻量级 JAR。

我想要的是两个可执行的 JAR,每个都包含依赖项,用于:

  • 富应用
  • 酒吧应用

我如何用 Maven 做到这一点?


万一有人提出建议,由于依赖项之间的冲突foo-appbar-app我无法将它们合并到一个单独的 foobar-app 中。

4

1 回答 1

1

Add maven assembly plugin to the pom.xml's which you want to create a executable jar with dependencies. Execute mvn package on aggregator pom.xml. This command will execute mvn package command on all the sub modules. The ones that have maven assembly plugin will generate executable jars with dependencies.

In your case add this to foo-app and bar-app projects pom.xml. And configure your <mainClass>your.package.mainclass</mainClass> according to each projects main class.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>your.package.mainclass</mainClass>
                    </manifest>
                </archive>

            </configuration>
            <executions>
                <execution>
                    <id>assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
于 2018-03-07T18:25:00.547 回答