我的 Maven 项目有一个父模块和两个子模块:Service
和 API
。 Service
模块是一个 WAR,包含项目的所有源文件,而 API
模块的存在纯粹是为了构建一个包含 Service 模块类子集的 jar ,并将该 jar 部署到本地 Maven 仓库。
我尝试了 maven-dependency-plugin
和 maven-assembly-plugin
的组合来复制 Service
war 并将其包含在已部署的 API
JAR 中,但我正在努力寻找一种方法来仅从服务模块中引入一组特定的类,而无需在整个胖 WAR 中套牢。
来自 API
的 pom.xml:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<excludeGroupIds>...</excludeGroupIds>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
我考虑过使用 Service
模块的 pom 中的 maven-jar-plugin
构建 slim JAR,但这似乎是一种不好的做法。有什么建议吗?
最佳答案
我建议您的 API
项目实际包含您要部署的类,并且 Service
模块依赖于它。如果您想模块化您的项目(您应该这样做),请按照 Maven 的方式进行,而不是试图分解单个代码库。
关于java - 我如何使用 Maven 将同级模块中的类包含在 jar 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12692537/