如何使用maven将一个兄弟模块中的类包含在jar中? [英] How can I use maven to include classes from a sibling module in a jar?

查看:159
本文介绍了如何使用maven将一个兄弟模块中的类包含在jar中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Maven项目,一个父模块和两个孩子:服务 API 服务模块是一个WAR,包含项目的所有源文件,而 API 模块纯粹存在于从Service模块构建一个包含类的子集的jar,并将该jar部署到本地的maven repo。



我尝试过一个组合的 maven-dependency-plugin maven -assembly-plugin 复制服务 war,并将其包含在部署的 API JAR,但是我很难找到一种方法来从服务模块中获取一组特定的类,而不需要在整个胖的WAR中进行操作。



API 的pom.xml:

  ;插件> 
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-dependency-plugin< / artifactId>
<执行>
< 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>
<执行>
< execution>
< goals>
< goal> single< / goal>
< / goals>
< phase> package< / phase>
< / execution>
< / executions>
< configuration>
< descriptors>
< descriptor> assembly.xml< / descriptor>
< / descriptors>
< / configuration>
< / plugin>
< / plugins>

我已经考虑过使用 maven-jar-插件服务模块的pom,但这似乎是不好的做法。建议?

解决方案

您应该从战争中取出实施细节并放在一个单独的模块中。

 

├──pom.xml
├──service-api
| ├──pom.xml
| └──src
| └──main
| └──java
| └──com
| └──stackoverflow
| └──SomeDao.java
├──service-impl
| ├──pom.xml
| └──src
| └──main
| └──java
| └──com
| └──stackoverflow
| └──SomeDaoImpl.java
└──service-web
├──pom.xml
└──src
└──main
└──webapp
└──WEB-INF
└──web.xml

服务-impl 取决于 service-api 。而 service-web 将取决于 service-impl


I have Maven project with a parent module and two children: Service and API. The Service module is a WAR and contains all of the source files for the project, while the API module exists purely to build a jar containing a subset of classes from the Service module, and to deploy that jar to a local maven repo.

I've tried a combo of the maven-dependency-plugin and the maven-assembly-plugin to copy over the Service war and include it in the deployed API JAR, but I'm struggling to find a way to pull in just a specific set of classes from the Service module without roping in the entire fat WAR.

From API's 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>

I've thought about building the slim JAR using the maven-jar-plugin from the Service module's pom, but this seems like bad practice. Suggestions?

解决方案

You should rather take out the implementation details from the war and put in a separate module.

.
├── pom.xml
├── service-api
|   ├── pom.xml
|   └── src
|       └── main
|           └── java
|               └── com
|                   └── stackoverflow
|                       └── SomeDao.java
├── service-impl
|   ├── pom.xml
|   └── src
|       └── main
|           └── java
|               └── com
|                   └── stackoverflow
|                       └── SomeDaoImpl.java
└── service-web
    ├── pom.xml
    └── src
        └── main
            └── webapp
                └── WEB-INF
                    └── web.xml

Let service-impl depend on service-api. And service-web will depend on the service-impl.

这篇关于如何使用maven将一个兄弟模块中的类包含在jar中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆