在J2EE开发中,当我们的某个ejb的jar包不但可以使用于jboss中,还希望运行在websphere或weblogic中。一个ejb包要发布在不同的j2ee中间件中时有些配置信息往往不一样,比如:这个ejb包中有JPA的配置文件persistence.xml文件。那么,这个文件在jboss中的设置如下:
<properties>
<!-- Auto detect annotation model classes -->
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.cache.use_second_level_cache"
value="true" />
</properties>
而websphere的设置如下:
<properties>
<!-- Auto detect annotation model classes -->
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.region_prefix" value="mes"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup"/>
</properties>
如果这个ejb的jar包是一个平台的基本模块,当某一个项目使用该平台发布在jboss环境下,另一个项目也使用该平台发布在websphere环境下,这个时候要想用maven管理该怎么办呢?这个时候classifier就发挥作用了。
首先,我们使用maven-jar-plugin或maven-ejb-plugin的classifier属性,将这个模块分别发布为jboss类型的和websphere类型的,下面以maven-ejb-plugin为例配置如下:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<classifier>${appserver.type}</classifier>
<ejbVersion>3.0</ejbVersion>
<archive>
<manifest>
<addClasspath>false</addClasspath>
</manifest>
</archive>
<generateClient>true</generateClient>
<clientExcludes>
<clientExclude>**/*Bean.class</clientExclude>
<clientExclude>**/package.html</clientExclude>
</clientExcludes>
</configuration>
</plugin>
特别需要注意的是:<classifier>${appserver.type}</classifier>
appserver.type是变量:
当发布jboss版本的模块时使用jboss作为变量值.
当发面websphere版本的模块时使用websphere作为变量值.
把分别不同情况生成的包发布到私服中,可以在仓库中看到xxx-jboss.jar和xxx-websphere.jar,如图所示:
当系统要发布在jboss或websphere中间件时,也可以通过在依赖中使用classifier来指定是使用平台模块的jboss版本还是websphere版本,配置如下:
<dependency>
<groupId>cn.com.pzhsteel.mes</groupId>
<artifactId>mes-ejb</artifactId>
<version>${mes.platform.version}</version>
<classifier>websphere</classifier>
<type>ejb</type>
</dependency>
这样就可以指定是使用哪个版本的平台模块了。