Mai*_*aik 97 ear maven-2 java-ee maven-ear-plugin
我刚刚从Ant切换到Maven,并试图找出建立基于EAR文件的企业项目的最佳实践?
假设我想创建一个非常标准的项目,其中包含EJB的jar文件,Web层的WAR文件和封装的EAR文件,以及相应的部署描述符.
我该怎么办呢?archetypeArtifactId=maven-archetype-webapp用war文件创建项目,并从那里扩展?什么是最好的项目结构(和POM文件示例)?你在哪里粘贴与ear文件相关的部署描述符等?
谢谢你的帮助.
Mik*_*ell 92
您创建一个新项目.新项目是您的EAR程序集项目,它包含EJB项目和WAR项目的两个依赖项.
所以你实际上有三个maven项目.一个EJB.一场战争 一个EAR将两个部分拉在一起并形成耳朵.
部署描述符可以由maven生成,也可以放在EAR项目结构的资源目录中.
maven-ear-plugin就是你用来配置它的,而且文档很好,但是如果你还在弄清楚maven一般是怎么工作的,那就不太清楚了.
举个例子,你可能会这样做:
<?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>com.mycompany</groupId>
<artifactId>myEar</artifactId>
<packaging>ear</packaging>
<name>My EAR</name>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<version>1.4</version>
<modules>
<webModule>
<groupId>com.mycompany</groupId>
<artifactId>myWar</artifactId>
<bundleFileName>myWarNameInTheEar.war</bundleFileName>
<contextRoot>/myWarConext</contextRoot>
</webModule>
<ejbModule>
<groupId>com.mycompany</groupId>
<artifactId>myEjb</artifactId>
<bundleFileName>myEjbNameInTheEar.jar</bundleFileName>
</ejbModule>
</modules>
<displayName>My Ear Name displayed in the App Server</displayName>
<!-- If I want maven to generate the application.xml, set this to true -->
<generateApplicationXml>true</generateApplicationXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
<finalName>myEarName</finalName>
</build>
<!-- Define the versions of your ear components here -->
<dependencies>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>myWar</artifactId>
<version>1.0-SNAPSHOT</version>
<type>war</type>
</dependency>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>myEjb</artifactId>
<version>1.0-SNAPSHOT</version>
<type>ejb</type>
</dependency>
</dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)
Pat*_*ner 46
帮助我很多的是运行Maven原型:生成目标并从其中一个原型中选择,其中一些似乎定期更新(特别是JBoss似乎得到很好的维护).
mvn archetype:generate
Run Code Online (Sandbox Code Playgroud)
数以百计的原型出现在编号列表中,可以从中选择(截至目前为519!).仍在运行的目标促使我通过输入数字或输入搜索字符串进行选择,例如:
513: remote -> org.xwiki.commons:xwiki-commons-component-archetype
514: remote -> org.xwiki.rendering:xwiki-rendering-archetype-macro
515: remote -> org.zkoss:zk-archetype-component
516: remote -> org.zkoss:zk-archetype-webapp
517: remote -> ru.circumflex:circumflex-archetype (-)
518: remote -> se.vgregion.javg.maven.archetypes:javg-minimal-archetype (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains):
Run Code Online (Sandbox Code Playgroud)
我输入了搜索字符串"ear",它将列表缩减为8个项目(截至今天):
Choose archetype:
1: remote -> org.codehaus.mojo.archetypes:ear-j2ee14 (-)
2: remote -> org.codehaus.mojo.archetypes:ear-javaee6 (-)
3: remote -> org.codehaus.mojo.archetypes:ear-jee5 (-)
4: remote -> org.hibernate:hibernate-search-quickstart (-)
5: remote -> org.jboss.spec.archetypes:jboss-javaee6-ear-webapp
6: remote -> org.jboss.spec.archetypes:jboss-javaee6-webapp-ear-archetype
7: remote -> org.jboss.spec.archetypes:jboss-javaee6-webapp-ear-archetype-blank
8: remote -> org.ow2.weblab.tools.maven:weblab-archetype-searcher
Run Code Online (Sandbox Code Playgroud)
我选择了"org.jboss.spec.archetypes:jboss-javaee6-ear-webapp"(通过在此示例中输入选择"5").
接下来,目标要求我输入groupId,artifactId,包名等,然后生成以下记录良好的示例应用程序:
[pgarner@localhost Foo]$ tree
.
|-- Foo-ear
| `-- pom.xml
|-- Foo-ejb
| |-- pom.xml
| `-- src
| |-- main
| | |-- java
| | | `-- com
| | | `-- foo
| | | |-- controller
| | | | `-- MemberRegistration.java
| | | |-- data
| | | | `-- MemberListProducer.java
| | | |-- model
| | | | `-- Member.java
| | | `-- util
| | | `-- Resources.java
| | `-- resources
| | |-- import.sql
| | `-- META-INF
| | |-- beans.xml
| | `-- persistence.xml
| `-- test
| |-- java
| | `-- com
| | `-- foo
| | `-- test
| | `-- MemberRegistrationTest.java
| `-- resources
|-- Foo-web
| |-- pom.xml
| `-- src
| `-- main
| |-- java
| | `-- com
| | `-- foo
| | `-- rest
| | |-- JaxRsActivator.java
| | `-- MemberResourceRESTService.java
| `-- webapp
| |-- index.html
| |-- index.xhtml
| |-- resources
| | |-- css
| | | `-- screen.css
| | `-- gfx
| | |-- banner.png
| | `-- logo.png
| `-- WEB-INF
| |-- beans.xml
| |-- faces-config.xml
| `-- templates
| `-- default.xhtml
|-- pom.xml
`-- README.md
32 directories, 23 files
Run Code Online (Sandbox Code Playgroud)
在阅读了四篇经过充分评论的POM文件后,我获得了所需的所有信息.
./pom.xml
./Foo-ear/pom.xml
./Foo-ejb/pom.xml
./Foo-web/pom.xml
Run Code Online (Sandbox Code Playgroud)
Ste*_*erg 23
我已经创建了一个github存储库来显示我认为是一个好的(或最佳实践)启动项目结构......
https://github.com/StefanHeimberg/stackoverflow-1134894
一些关键字:
Maven输出:
Reactor Summary:
MyProject - BOM .................................... SUCCESS [ 0.494 s]
MyProject - Parent ................................. SUCCESS [ 0.330 s]
MyProject - Common ................................. SUCCESS [ 3.498 s]
MyProject - Persistence ............................ SUCCESS [ 1.045 s]
MyProject - Business ............................... SUCCESS [ 1.233 s]
MyProject - Web .................................... SUCCESS [ 1.330 s]
MyProject - Application ............................ SUCCESS [ 0.679 s]
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 8.817 s
Finished at: 2015-01-27T00:51:59+01:00
Final Memory: 24M/207M
------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
NetBeans IDE自动定义与Patrick Garner建议的结构几乎相似的结构.对于NetBeans用户
文件 - > 新建项目 - >在左侧选择Maven,在右侧选择Maven Enterprise Application,然后按下Next - >询问war,ejb和settings的项目名称.
IDE将自动为您创建结构.
| 归档时间: |
|
| 查看次数: |
154398 次 |
| 最近记录: |