无法看到WAR模块中的JAR库

时间:2018-12-06 07:41:22

标签: maven jar war ear

我有带有WAR和EAR模块的Maven项目。 EAR模块将WAR作为依赖项,并作为战争模块包含在maven-ear-plugin中。 EAR模块中还添加了JAR库,WAR模块使用了它。

EAR模块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.example</groupId>
        <artifactId>MyApp</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>MyEar</artifactId>
    <packaging>ear</packaging>

    <dependencies>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>MyWar</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <type>war</type>
            </dependency>
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>MyJar</artifactId>
            </dependency>
        </dependencies>

        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-ear-plugin</artifactId>
                    <version>2.9</version>
                    <configuration>
                        <modules>
                            <jarModule>
                                <groupId>com.example</groupId>
                                <artifactId>MyJar</artifactId>
                            </jarModule>
                            <webModule>
                                <groupId>com.example</groupId>
                                <artifactId>MyWar</artifactId>
                                <contextRoot>/MyWebApp</contextRoot>
                            </webModule>
                        </modules>
                        <version>6</version>
                    </configuration>
                </plugin>
            </plugins>
        </build>

</project>

WAR模块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.example</groupId>
        <artifactId>MyApp</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>MyWar</artifactId>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>MyJar</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我正在尝试构建包含MyWar和MyJar根目录的EAR。问题是,如果我像上面那样使用EAR pom,MyWar不会“看到” MyJar库。

我在这里想念什么?

1 个答案:

答案 0 :(得分:1)

应该放在EAR归档文件根目录中的唯一模块是ejb-jars,wars和rars。

实用程序库jar应该放在EAR中的“ lib”目录中。

您只需要在EAR插件配置中添加一行即可完成此操作:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <modules>
                        <jarModule>
                            <groupId>com.example</groupId>
                            <artifactId>MyJar</artifactId>
                        </jarModule>
                        <webModule>
                            <groupId>com.example</groupId>
                            <artifactId>MyWar</artifactId>
                            <contextRoot>/MyWebApp</contextRoot>
                        </webModule>
                    </modules>
                    <version>6</version>
                    <defaultLibBundleDir>lib</defaultLibBundleDir>  <--- this!
                </configuration>
            </plugin>

如果要使用EAR文件,则应熟悉其中各个模块的类可见性规则。 My ear is not able to find ejb module classes中对此进行了概括。

通常,这些天不应该使用EAR文件。在大多数情况下,一个简单的WAR文件就足够了

相关问题