【发布时间】:2014-05-29 15:15:16
【问题描述】:
虽然我已经使用 Java 几年了,但我还是 Glassfish 和 Java EE 的新手。我继承了一个停滞不前的项目,现在我需要重新开始开发它。我正在尝试按原样部署 Web 应用程序,以查看哪些有效,哪些需要注意。虽然实现各种功能的代码很多,但首先需要注意的是应用程序的部署。
我尝试部署的 Web 应用程序是 MyServer,并打包在 WAR 存档中。 MyServer 中的类引用了 MyDatabase 中的类,该类实现了来自 MyInterfaces 的接口 (InterfaceOne)。 MyServer 中的类也从MyInterfaces 直接引用了InterfaceOne。 MyServer、MyDatabase和MyInterfaces都是Eclipse中的项目,使用Maven打包。 MyServer POM 文件将 MyDatabase 列为依赖项,MyDatabase 的 POM 文件将 MyInterfaces 列为依赖项。 MyServer POM 文件没有将 MyInterfaces 列为直接依赖项,但我认为这不会导致问题。 Eclipse 没有在相关的MyServer 类中显示任何错误; Eclipse 似乎认为 Maven 依赖项足以找到所有必要的类。
当我通过管理 Web 控制台在 Glassfish v3.1.2 上部署 MyServer WAR,然后查看 ~glassfish3/glassfish/domains/MyDomain/logs/server.log 中的日志时,遇到以下错误:
[#|2014-05-29T10:06:02.371+0100|严重|glassfish3.1.2|global|_ThreadID=80;_ThreadName=Thread-2;|找不到类 [my/interfaces/InterfaceOne]。加载 [ class my.server.ControllerOne ]|#] 时出错 [#|2014-05-29T10:06:02.387+0100|SEVERE|glassfish3.1.2|global|_ThreadID=80;_ThreadName=Thread-2;|找不到类 [my/interfaces/InterfaceOne]。加载 [ class my.server.ControllerOne ]|#] 时出错
尝试从MyServer 加载类时,显示了两个与接口相关的错误。如果我从my.server.ControllerOne 类中删除对MyDatabase 的引用或从MyInterfaces 对InterfaceOne 的直接引用,那么上述错误之一就会消失(这是预期的,只是确认错误与引用有关(直接和间接)来自MyInterfaces 的InterfaceOne。
包括here、here 和here 在内的类似问题都提到确保所需的库(my-database.jar 和my-interfaces.jar)位于WEB-INT/lib 目录中MyServer 应用程序的WAR 存档。但是,如果我检查为MyServer 生成的WAR 存档,那么这些库确实位于WEB-INF/lib/ 目录中。如果我在部署后检查~glassfish3/glassfish/domains/MyDomain/applications/MyServer/WEB-INF/lib/ 目录,那么库也在那里。我假设这表明它们已成功从 WAR 存档中提取并放置在 Glassfish 服务器上。关键是图书馆肯定在那里!
我能想到的只是这些库没有添加到 JVM 的类路径中,试图加载 MyServer 的类,但如果 Glassfish 尝试加载应用程序的类,那么它也应该首先加载打包的库应用程序不应该吗?确实是 Oracle GlassFish Server 应用程序开发指南中的 this 页面
Release 3.1.2 表示将应用程序的WEB-INF/lib/ 目录添加到应用程序的类路径中。
我看到其他建议将 Web 应用程序所需的库放在 ~glassfish3/glassfish/domains/MyDomain/lib/ext/ 目录中。这可能很好用,但我的特定库仅由MyServer 应用程序需要,而不是整个MyDomain。据我所知,将它们放在我的应用程序的WEB-INF/lib/ 目录中应该是有效的。
有没有人知道为什么在部署过程中my-database.jar 和my-interfaces.jar 库似乎没有添加到类路径中?更具体地说,有谁知道为什么在部署期间加载 my.server.ControllerOne 类时 my.interfaces.InterfaceOne 类不可见?
MyServer的web.xml内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>MyServer</display-name>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
<welcome-file>default.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/balusc.taglib.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<context-param>
<param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
<param-value>false</param-value>
</context-param>
</web-app>
我的服务器 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>my.webapp</groupId>
<artifactId>my-server</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>My Server</name>
<parent>
<groupId>my.webapp</groupId>
<artifactId>my-parent-pom</artifactId>
<version>1.5</version>
</parent>
<organization>
<name>Example</name>
<url>http://www.example.com</url>
</organization>
<repositories>
<repository>
<id>eclipselink</id>
<url>http://www.eclipse.org/downloads/download.php?r=1&nf=1&file=/rt/eclipselink/maven.repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>eclipselink</groupId>
<artifactId>eclipselink</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>utilities</groupId>
<artifactId>utilities</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>my.webapp</groupId>
<artifactId>my-database</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<!-- <configuration> section added to pick up the WEB-INF/web.xml inside
WebContent -->
<configuration>
<webResources>
<resource>
<directory>WebContent</directory>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
<scm>
<connection>scm:svn:https://svn.server/location/in/repo/my_server</connection>
<developerConnection>scm:svn:https://svn.server/location/in/repo/my_server/trunk</developerConnection>
</scm>
</project>
我的数据库 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>
<artifactId>my-database</artifactId>
<version>0.1.0-SNAPSHOT</version>
<parent>
<groupId>my.webapp</groupId>
<artifactId>my-parent-pom</artifactId>
<version>1.5</version>
</parent>
<dependencies>
<dependency>
<groupId>eclipselink</groupId>
<artifactId>eclipselink</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mindrot</groupId>
<artifactId>bcrypt</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>my.webapp</groupId>
<artifactId>my-interfaces</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jpa_3.0_spec</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<scm>
<connection>scm:svn:https://svn.server/location/in/repo/my_database</connection>
<developerConnection>scm:svn:https://svn.server/location/in/repo/my_database/trunk/</developerConnection>
</scm>
<groupId>my.webapp</groupId>
</project>
MyInterfaces 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>my.webapp</groupId>
<artifactId>my-interfaces</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>my.webapp</groupId>
<artifactId>my-parent-pom</artifactId>
<version>1.5</version>
</parent>
<scm>
<connection>scm:svn:https://svn.server/location/in/repo/my_interfaces</connection>
<developerConnection>scm:svn:https://svn.server/location/in/repo/my_interfaces/trunk</developerConnection>
</scm>
<dependencies>
<dependency>
<groupId>my.webapp</groupId>
<artifactId>my-utilities</artifactId>
<version>0.1.2</version>
<classifier>me</classifier>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
MyInterfaces POM 列出了对MyUtilities 的依赖。我认为这个问题可以安全地忽略,因为它的类都没有出现在我描述的错误消息中。部署我的应用程序时,它也放在 Glassfish 服务器上的 WEB-INT/lib/ 目录中。
(Glassfish 问题似乎需要大量信息,例如 web.xml 文件和 pom.xml 等。感谢您抽出时间筛选所有这些。)
【问题讨论】:
-
请将您的
pom.xml添加到问题中。 -
您正在寻找的 POM 中是否有特定内容?由于库在部署期间放置在 Glassfish 服务器上的 WEB-INF/lib 目录中,因此我不知道 POM 中有什么可能有助于解决此问题。当然,这可能就是我首先遇到这个问题的原因......
-
没什么特别的,只是让包装更容易理解。 my-server-pom.xml 中的
utilities依赖项是否与 my-interfaces-pom.xml 中的my-utilities依赖项相同?可能导致webapp中打包my-utilities的版本不对。 -
不,它们不一样。他们只是有相似的名字让我保持警惕。
-
奇怪的东西,我已经设置了一个具有相同结构的基本项目并且它可以工作。我注意到错误消息一次提到
my/interface,一次提到my/interfaces。这是正确的还是某种错字?
标签: deployment classpath war glassfish-3