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

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

问题描述

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

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.

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

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.

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

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?

我找到了关于 Maven jar插件,但它说,如果项目的包装设置为'jar',这个插件只要通过包阶段就会被执行。我项目中的包装没有设置为jar。还有一些方法可以使用这个插件吗?

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?

推荐答案

您可以创建一个多模块项目,其中一个模块包含您的域类和另一个模块是您的Web应用程序。通过这样做,您的外部第三方 war 可以使用您的域类,只需包含 jar

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





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



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



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>

通过这种方式,您可以解耦一些依赖项,并可以重用 jar 来自模块的文件。

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

最后,您可以使用叠加层来查看通过使用您自己的 war war 来创建一个 war $ C>。我不知道你的设置是否可行,但值得一看。我成功地使用了它。

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.

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

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