【问题标题】:How can I use maven to include classes from a sibling module in a jar?如何使用 Maven 将兄弟模块中的类包含在 jar 中?
【发布时间】:2012-10-02 23:13:00
【问题描述】:

我有一个带有父模块和两个子模块的 Maven 项目:ServiceAPIService 模块是一个 WAR,包含项目的所有源文件,而 API 模块的存在纯粹是为了构建一个包含来自 Service 模块的类子集的 jar,并将该 jar 部署到本地 maven回购。

我尝试了 maven-dependency-pluginmaven-assembly-plugin 的组合来复制 Service 战争并将其包含在已部署的 API JAR 中,但我正在努力寻找一种方法来加入只是来自 Service 模块的一组特定类,而不是在整个胖 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 构建苗条JAR,但这似乎是一种不好的做法。有什么建议吗?

【问题讨论】:

    标签: java maven jar dependencies war


    【解决方案1】:

    我建议您的API 项目实际上包含您要部署的类,并且Service 模块依赖于它。如果你想模块化你的项目(你应该这样做),按照 Maven 的方式去做,而不是试图破解单个代码库。

    【讨论】:

    • 那是我最初的想法,但我只是不完全喜欢让构建系统决定代码库的结构,所以我正在寻找其他潜在的途径。我也同意我试图去做的方式相当可怕。谢谢!
    • @RustyBuckets - 不要将其视为定义项目结构的构建系统,而是将其视为能够重用并创建清晰的依赖关系的通用模块化。顺便说一句,我认为 maba 比我有更好的细分(和图表!)。
    • @parsifal - 感谢您的观点,我只需要通过一副不同的眼镜看它:)
    【解决方案2】:

    您应该从war 中取出实现细节并放入单独的模块中。

    . ├── pom.xml ├── 服务-api | ├── pom.xml | └── 源 | └── 主要 | └── java | └── com | └── 堆栈溢出 | └── SomeDao.java ├── 服务实现 | ├── pom.xml | └── 源 | └── 主要 | └── java | └── com | └── 堆栈溢出 | └── SomeDaoImpl.java └── 服务网 ├── pom.xml └── 源 └── 主要 └── 网页应用 └── WEB-INF └── web.xml

    service-impl 依赖于service-api。而service-web 将依赖于service-impl

    【讨论】:

    • 这听起来是最好的方法。非常感谢!
    猜你喜欢
    • 2011-08-27
    • 2017-09-25
    • 2020-03-20
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 2016-01-12
    • 2020-09-08
    • 2014-03-22
    相关资源
    最近更新 更多