如何从打包为 war 的项目的一部分创建 jar [英] How to create a jar from part of my project that is packaged as a war

查看:14
本文介绍了如何从打包为 war 的项目的一部分创建 jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Maven 新手,所以如果这是微不足道的,我深表歉意.基本上,我正在开发一个 webapp,我正在使用 Maven 来管理项目.我的 pom.xml 中有 <packaging>war</packaging> 这样当我运行 mvn package 时,它会吐出一个 war 文件以部署在网络服务器.

现在,作为此应用程序的一部分,我们使用了作为战争交付给我们的第 3 方库,它单独部署在 Web 服务器上.这场战争包括我们编写的一些自定义集成功能.对于持久性逻辑,我最初只是在此集成代码中直接编写了一个存储库.当我发现我需要更多的持久性逻辑(比基本的 SELECT 多)时,我发现我想要使用在我们的应用程序代码中找到的存储库和域对象.因此,理想情况下,我希望能够打包我们的核心包,然后将该 jar 包含在这个 3rd 方战争中,这样我就可以在那里使用相同的功能.

我只是不知道如何设置 pom.xml 来告诉它我想要在这个 jar 中包含哪些包(如果需要,甚至只需要 1 个包),以及如何创建 jar 本身.有没有办法从项目中的特定包生成 jar,该项目设置为将整个项目打包为战争?

我找到了有关 Maven jar 插件的信息,但是它说,如果您的项目的打包设置为 'jar',则只要它通过打包"阶段,就会执行此插件."我的项目中的包装未设置为 jar.还有什么办法可以使用这个插件吗?

解决方案

您可以创建一个多模块项目,其中一个模块包含您的域类,另一个模块是您的 Web 应用程序.通过这样做,您的外部 3rd 方 war 可以通过包含该 jar 来使用您的域类.

这是目录结构的简单概述:

<前>.├── pom.xml├── 域名|├── pom.xml|└── src|└── 主要|└── 爪哇|└── com|└── stackoverflow|└── 领域|├── SomeDao.java|└── AnotherDao.java└── 网络├── pom.xml└── src└── 主要├── java|└── com|└── stackoverflow|└── 网络|└── SomeBackingBean.java└── 网页应用└── 网络信息└── web.xml

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.stackoverflow</groupId><artifactId>Q12576767</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><name>${project.artifactId}-${project.version}</name><模块><module>域</module><module>web</module></模块><依赖管理><依赖项><!-- 模块间依赖--><依赖><groupId>com.stackoverflow</groupId><artifactId>Q12576767-domain</artifactId><version>${project.version}</version></依赖></依赖项></dependencyManagement></项目>

域/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><父母><groupId>com.stackoverflow</groupId><artifactId>Q12576767</artifactId><version>1.0-SNAPSHOT</version></父母><artifactId>Q12576767-domain</artifactId><name>${project.artifactId}-${project.version}</name></项目>

web/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><父母><groupId>com.stackoverflow</groupId><artifactId>Q12576767</artifactId><version>1.0-SNAPSHOT</version></父母><artifactId>Q12576767-web</artifactId><包装>战争</包装><name>${project.artifactId}-${project.version}</name><依赖项><依赖><groupId>com.stackoverflow</groupId><artifactId>Q12576767-domain</artifactId></依赖></依赖项></项目>

通过这种方式,您可以解耦一些依赖项,并且可以重用 domain 模块中的 jar 文件.

<小时>

最后你还可以看看使用 Overlays通过将第 3 方 war 与您自己的 war 叠加来创建一个 war.我不知道在您的设置中是否可行,但值得一看.我已经成功使用了.

I'm a Maven newbie, so I apologize if this is something trivial. Basically, I am developing a webapp, and I am using Maven to manage the project. I have <packaging>war</packaging> in my pom.xml so that that when I run mvn package, it will spit out a war file to deploy on the web server.

Now, as part of this application, we are using a 3rd party library that is delivered to us as a war and it is deployed separately on the web server. This war includes some custom integration functionality that we code up. For the persistence logic, I originally just wrote up a repository straight in this integration code. As I find I'm needing more persistence logic (more than basic SELECTs), I'm finding I'm wanting to use repositories and domain objects found in our application code. So, ideally, I would like to be able to jar up our core packages, and then include that jar in this 3rd party war so I have the same functionality available to me there.

I just don't know how I could set up the pom.xml to tell it what packages I would want in this jar (or even just 1 package if necessary), and how to create the jar itself. Is there a way to generate a jar from specific packages in a project that's setup to package the whole project as a war?

I found information about the Maven jar plugin, but it says, "If the packaging of your project is set to 'jar', this plugin is executed whenever it passes the "package" phase." The packaging in my project is not set to jar. Is there still some way to use this plugin?

解决方案

You can create a multi module project where one module contains your domain classes and the other module is your web application. By doing so your external 3rd party war can use your domain classes by just include that jar.

This is a simple overview of the directory structure:

.
├── pom.xml
├── domain
|   ├── pom.xml
|   └── src
|       └── main
|           └── java
|               └── com
|                   └── stackoverflow
|                       └── domain
|                           ├── SomeDao.java
|                           └── AnotherDao.java
└── web
    ├── pom.xml
    └── src
        └── main
            ├── java
            |   └── com
            |       └── stackoverflow
            |           └── web
            |               └── SomeBackingBean.java
            └── webapp
                └── WEB-INF
                    └── web.xml

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.stackoverflow</groupId>
    <artifactId>Q12576767</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>${project.artifactId}-${project.version}</name>

    <modules>
        <module>domain</module>
        <module>web</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <!-- Inter-Module dependencies -->
            <dependency>
                <groupId>com.stackoverflow</groupId>
                <artifactId>Q12576767-domain</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

domain/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.stackoverflow</groupId>
        <artifactId>Q12576767</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>Q12576767-domain</artifactId>

    <name>${project.artifactId}-${project.version}</name>
</project>

web/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.stackoverflow</groupId>
        <artifactId>Q12576767</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>Q12576767-web</artifactId>
    <packaging>war</packaging>

    <name>${project.artifactId}-${project.version}</name>

    <dependencies>
        <dependency>
            <groupId>com.stackoverflow</groupId>
            <artifactId>Q12576767-domain</artifactId>
        </dependency>
    </dependencies>
</project>

By doing it this way you decouple some dependencies and can reuse the jar file from the domain module.


Also at the end you can look at using Overlays to just create one war by overlaying the 3rd-party war with your own war. I don't know if it is feasible in your setup but it is worth looking at. I have sucessfully used it.

这篇关于如何从打包为 war 的项目的一部分创建 jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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