有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从另一个模块maven导入类

我无法将类从ejb模块导入web模块。ejb类在模块ejb中的package:jsf中,我想将其导入web模块package:jsf中

导入jsf。DaneOsobowe;-多森特作品

我错了什么

pom大师:

    <?xml version="1.0" encoding="UTF-8"?>
<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>jsf</groupId>
  <artifactId>PAW</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>PAW</name>

  <modules>
    <module>PAW-ear</module>
    <module>PAW-web</module>
    <module>PAW-ejb</module>
  </modules>

  <dependencyManagement>
    <dependencies>

      <dependency>
        <groupId>jsf</groupId>
        <artifactId>PAW-ejb</artifactId>
        <version>1.0</version>
        <type>ejb</type>
        <scope>compile</scope>
      </dependency>

      <dependency>
        <groupId>jsf</groupId>
        <artifactId>PAW-web</artifactId>
        <version>1.0</version>
        <type>jar</type>
        <scope>compile</scope>

      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

ejb pom:

    <?xml version="1.0" encoding="UTF-8"?>
<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>
    <artifactId>PAW</artifactId>
    <groupId>jsf</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <groupId>jsf</groupId>
  <artifactId>PAW-ejb</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>ejb</packaging>

  <name>PAW-ejb</name>

  <properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>4.3.1.Final</version>
    </dependency>
    <dependency>
      <groupId>unknown.binary</groupId>
      <artifactId>hibernate-jpamodelgen-4.3.1.Final</artifactId>
      <version>SNAPSHOT</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>7.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>jsf</groupId>
      <artifactId>PAW-ejb</artifactId>
      <type>ejb</type>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <compilerArguments>
            <endorseddirs>${endorsed.dir}</endorseddirs>
          </compilerArguments>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-ejb-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <ejbVersion>3.1</ejbVersion>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <phase>validate</phase>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <outputDirectory>${endorsed.dir}</outputDirectory>
              <silent>true</silent>
              <artifactItems>
                <artifactItem>
                  <groupId>javax</groupId>
                  <artifactId>javaee-endorsed-api</artifactId>
                  <version>7.0</version>
                  <type>jar</type>
                </artifactItem>
              </artifactItems>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <repositories>
    <repository>
      <id>unknown-jars-temp-repo</id>
      <name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
      <url>file:${project.basedir}/lib</url>
    </repository>
  </repositories>
</project>

网络pom:

    <?xml version="1.0" encoding="UTF-8"?>
<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>
    <artifactId>PAW</artifactId>
    <groupId>jsf</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <groupId>jsf</groupId>
  <artifactId>PAW-web</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>PAW-web</name>

  <properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.primefaces</groupId>
      <artifactId>primefaces</artifactId>
      <version>5.0</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>5.2.2.Final</version>
      <type>jar</type>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>4.3.1.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate.javax.persistence</groupId>
      <artifactId>hibernate-jpa-2.1-api</artifactId>
      <version>1.0.0.Final</version>
    </dependency>
    <dependency>
      <groupId>unknown.binary</groupId>
      <artifactId>mysql-connector-java-5.1.23-bin</artifactId>
      <version>SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>javax.validation</groupId>
      <artifactId>validation-api</artifactId>
      <version>1.1.0.Final</version>
      <type>jar</type>
    </dependency>
    <dependency>
      <groupId>org.primefaces.extensions</groupId>
      <artifactId>primefaces-extensions</artifactId>
      <version>4.0.0</version>
    </dependency>
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-web-api</artifactId>
      <version>7.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>jsf</groupId>
      <artifactId>PAW-ejb</artifactId>
      <type>ejb</type>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <compilerArguments>
            <endorseddirs>${endorsed.dir}</endorseddirs>
          </compilerArguments>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.3</version>
        <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <phase>validate</phase>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <outputDirectory>${endorsed.dir}</outputDirectory>
              <silent>true</silent>
              <artifactItems>
                <artifactItem>
                  <groupId>javax</groupId>
                  <artifactId>javaee-endorsed-api</artifactId>
                  <version>7.0</version>
                  <type>jar</type>
                </artifactItem>
              </artifactItems>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <repositories>
    <repository>
      <url>http://repository.primefaces.org/</url>
      <id>PrimeFaces-maven-lib</id>
      <layout>default</layout>
      <name>Repository for library PrimeFaces-maven-lib</name>
    </repository>
  </repositories>
</project>

共 (1) 个答案

  1. # 1 楼答案

    抱歉,我不太熟悉EAR+EJB的东西,因为在大多数情况下,我遇到的都是严重的并发症。然而,如果这是你需要使用的

    看看an answer explaining them in some detail

    基本上,EJB应该包含其他地方定义的一些接口的实现。WAR不知道在运行时会得到什么实现,它只知道接口,所以它应该依赖于接口。让我们调用这些接口API,您将得到以下项目结构:

    parent
    |- api
    |- ejb
    |- war
    \- ear
    

    其中:

    1. API只包含接口(不指定打包,默认为jar)
    2. EJB依赖API来实现一些/所有接口
    3. WAR依赖于API来使用它们(例如,通过接口注入点)
    4. EAR依靠WAR和EJB将两者捆绑到最终归档中

    如果这对于您的用例来说太复杂,您可以从包含所有的WAR开始,然后重构。你可以部署。war适用于任何应用服务器,甚至只适用于没有完整应用服务器功能的容器