未看到 WAR 模块中的 JAR 库
JAR library in WAR module not seen
我有带有 WAR 和 EAR 模块的 Maven 项目。 EAR 模块具有 WAR 作为依赖项,并作为 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,MyJar 库不是 "seen" by MyWar.
我在这里错过了什么?
应该位于 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 文件,您应该熟悉其中各个模块的 class 可见性规则。 .
对此进行了粗略的总结
一般来说,这些天您不应该使用 EAR 文件。在大多数情况下,一个简单的 WAR 文件就足够了
我有带有 WAR 和 EAR 模块的 Maven 项目。 EAR 模块具有 WAR 作为依赖项,并作为 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,MyJar 库不是 "seen" by MyWar.
我在这里错过了什么?
应该位于 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 文件,您应该熟悉其中各个模块的 class 可见性规则。
一般来说,这些天您不应该使用 EAR 文件。在大多数情况下,一个简单的 WAR 文件就足够了