2

我正在学习 Maven 并尝试建立一个简单的项目。我想要这样的结构:

-myproject
  -myproject-ear
  -myproject-service
    -webservice
  -myproject-ejb

编辑 1
已解决。下面编辑 2 中的新问题。

编辑 2
当我不支持 JavaEE 时,我可以编译和部署我的 EAR 文件。但是当我添加对 JavaEE 的支持时,我得到了一些错误。根 pom.xml (myproject) 如下:

 <?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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>myproject</groupId>
<artifactId>myproject</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>myproject</name>
<url>http://maven.apache.org</url>

<modules>
    <module>myproject-ejb</module>
    <module>myproject-service</module>
    <module>myproject-ear</module>
</modules>

<repositories>
    <repository>
        <id>jboss</id>
        <url>http://repository.jboss.org/maven2/</url>
    </repository>
</repositories>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jboss.spec</groupId>
            <artifactId>jboss-javaee-6.0</artifactId>
            <version>3.0.2.Final</version>
            <scope>provided</scope>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.2.0.Final</version>
            <scope>provided</scope>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.2.0.Final</version>
            <scope>provided</scope>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ejb-plugin</artifactId>
            <version>2.3</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.8</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.4.3</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <ejbVersion>3.0</ejbVersion>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ear-plugin</artifactId>
                <version>2.8</version>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                    </archive>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.0</version>
                <configuration>
                    <remoteTagging>true</remoteTagging>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <remoteTagging>true</remoteTagging>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <remoteTagging>true</remoteTagging>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

pom.xmlmyproject-ejb看起来像这样:

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>myproject</groupId>
        <artifactId>myproject</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>myproject</groupId>
    <artifactId>myproject-ejb</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>myproject-ejb</name>
    <packaging>ejb</packaging>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.2.0.Final</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

如果我创建一个界面,比如

package org.myproject.ejb.customer;

import javax.ejb.Remote;

@Remote
public interface TestBean {
    public void createSomethingOnDatabase();
}

然后javax.ejb.*找不到包并mvn clean install失败。我是否错过了 pom.xml 中的某些内容myproject-ejb

编辑 3
如果我在 myproject-ejb 中添加 jboss-javaee-6.0 的依赖项(包括版本),这就是我得到的:

[ERROR] Failed to execute goal on project myproject-ejb: Could not resolve dependencies for project myproject:myproject-ejb:ejb:1.0-SNAPSHOT: The following artifacts could not be resolved: org.jboss.spec:jboss-javaee-6.0:jar:3.0.2.Final, xalan:xalan:jar:2.7.1.jbossorg-2: Could not find artifact org.jboss.spec:jboss-javaee-6.0:jar:3.0.2.Final in central (http://repo.maven.apache.org/maven2) -> [Help 1]
4

5 回答 5

3

groupId使用、artifactId和. 将依赖项type与依赖项管理中的依赖项进行匹配classifiertypeisjarclassifierit's 的默认值null

因此,依赖项myproject-ejb应该是:

<dependency>
  <groupId>org.jboss.spec</groupId>
  <artifactId>jboss-javaee-6.0</artifactId>
  <type>pom</type>
</dependency>

(关于EDIT 3

于 2013-04-30T15:06:33.120 回答
2

根据我在输出中看到的内容,您没有在 ear 模块中定义对 web 服务的依赖,因为 web 服务必须在 ear 模块之前构建。

于 2013-04-25T06:33:10.180 回答
1

您可以在myproject-ejb.

    <dependency>
        <groupId>org.jboss.spec</groupId>
        <artifactId>jboss-javaee-6.0</artifactId>
        <version>3.0.2.Final</version>
    </dependency>

关于您对该问题的第三次更新:

这是我用于的依赖项javax.ejb.Remote

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
    <scope>provided</scope>
</dependency>

您可以使用它来代替org.jboss.spec.

于 2013-04-30T13:56:13.613 回答
1

看起来您正在尝试依赖,<artifactId>myproject-webservice</artifactId>但您的模块是myproject-service

还有,应该myproject-service打包成jar而不是war?通常,如果一个项目被另一个项目用作依赖项,它将被打包为一个 jar。有关一些注释,请参阅此问题

于 2013-04-27T02:58:54.020 回答
0

对于您的 EDIT 3 问题:您需要将存储库包含到您的父 pom 或 pom 中:

<repositories>
    <repository>
        <id>jboss-repo</id>
        <name>jboss-repo</name>
        <url>https://repository.jboss.org/nexus/content/repositories/thirdparty-releases</url>
    </repository>
</repositories>

然后强制更新您的 Maven 依赖项。

mvn clean install -U
于 2014-01-14T04:38:34.450 回答