大家好,
在观看Justin撰写的有关Spring和GWT的非常有趣的文章时,我认为展示如何构建自己的自定义Maven原型非常有用。我们将展示的原型基于Justin的上一个项目,并包括各种技术,例如Spring , GWT , AspectJ , HornetQ和Infinispan 。
聊够了,现在让我们动手吧。
准备原型。
首先,您必须有一个用于构建原型的模板项目。在我们的演示文稿中,您将在此处找到Justin项目。
将目录更改为项目的根目录,然后运行以下命令。
mvn archetype:create-from-project
命令完成后,您将在以下位置找到一个新项目,该项目在$ {project_home} / target / generated-sources / archetype位置生成。它是一个完整的Maven原型项目,您可以根据自己的需要进行自定义。
- 在文件夹src / main / resources / META-INF / maven中,您可以找到archetype-metadata.xml,它是原型的描述符。
- 在文件夹src / main / resources / archetype-resources中是将要生成的模板项目。
定制原型。
Maven原型使用apache速度来生成其代码。 您可以通过在文件顶部设置以下常量来访问文件中的速度变量:
#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
然后,您可以使用$ {artifactId}语法访问Maven属性(例如artifactId ) 。还可以使用以下语法__artifactId__来访问参数值。
每个原型都具有以下基本参数:
- groupId
- artifactId
- 版
- 包
您还可以使用以下语法在archetype-metadata.xml中设置任意数量的变量:
default_value
请注意,每次添加变量时,都应编辑src / test / resources / projects / basic / archetype.properties并添加参数。
version=0.1-SNAPSHOT
groupId=archetype.it
artifactId=basic
custom_variable=default_value
在我们的示例中,我们执行了以下更改:
在src / main / resources / META-INF / maven / archetype-metadata.xml中
<requiredProperties>
<requiredProperty key="db">
<defaultValue>derby</defaultValue>
</requiredProperty>
<requiredProperty key="dburl">
<defaultValue>javacodegeeks</defaultValue>
</requiredProperty>
<requiredProperty key="dbusername">
<defaultValue>***</defaultValue>
</requiredProperty>
<requiredProperty key="dbpassword">
<defaultValue>***</defaultValue>
</requiredProperty>
<requiredProperty key="cache">
<defaultValue>y</defaultValue>
</requiredProperty>
</requiredProperties>
在src / test / resources / projects / basic / archetype.properties
db=derby
dburl=javacodegeeks
dbusername=test
dbpassword=test
cache=n
在src / main / resources / archetype-resources / pom.xml中
#if( $db == "derby" )
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.6.1.0</version>
</dependency>
#elseif( $db == "mysql" )
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.13</version>
</dependency>
#else
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.6.1.0</version>
</dependency>
和
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/webapp/${package}.Application</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources/${artifactId}</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
在src / main / resources / archetype-resources / src / main / webapp / WEB-INF / applicationContext.xml中
#if($db == "mysql")
<bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="javacodegeeks" />
<property name="xaDataSourceClassName"
value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
<property name="xaProperties">
<props>
<prop key="URL">${dburl}</prop>
<prop key="user">${dbusername}</prop>
<prop key="password">${dbpassword}</prop>
</props>
</property>
<property name="maxPoolSize" value="50" />
<property name="minPoolSize" value="20" />
</bean>
#else
<bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="javacodegeeks" />
<property name="xaDataSourceClassName" value="org.apache.derby.jdbc.EmbeddedXADataSource" />
<property name="xaProperties">
<props>
<prop key="databaseName">${dburl}</prop>
<prop key="createDatabase">create</prop>
</props>
</property>
<property name="maxPoolSize" value="50" />
<property name="minPoolSize" value="20" />
</bean>
#end
在src / main / resources / archetype-resources / src / main / resources / META-INF / persistence.xml中
#if($cache == "y")
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.infinispan.InfinispanRegionFactory"/>
<!--
<property name="hibernate.cache.infinispan.entity.eviction.strategy" value= "LRU"/>
<property name="hibernate.cache.infinispan.entity.eviction.wake_up_interval" value= "2000"/>
<property name="hibernate.cache.infinispan.entity.eviction.max_entries" value= "5000"/>
<property name="hibernate.cache.infinispan.entity.expiration.lifespan" value= "60000"/>
<property name="hibernate.cache.infinispan.entity.expiration.max_idle" value= "30000"/>
-->
#else
<property name="hibernate.cache.use_second_level_cache" value="false"/>^M
<property name="hibernate.cache.use_query_cache" value="false"/>^M
#end
将com / javacodegeeks / gwtspring / public文件夹重命名为__artifactId__。
安装和运行原型
要安装原型,请转到原型的根文件夹,然后键入。
mvn install
这将创建〜/ .m2 / archetype-catalog.xml文件,如果您是Eclipse的m2eclipse用户,则可以将其导入到eclipse。
要运行原型,请运行以下命令。
mvn archetype:generate -DarchetypeCatalog=local -DarchetypeGroupId=com.javacodegeeks -DarchetypeArtifactId=gwtspring-archetype
或在eclispe中创建一个新的maven项目,然后从您从〜/ .m2 / archetype-catalog.xml导入的本地目录中选择原型。
***请注意,原型可能是快照,您应选中“包括快照原型” 。
原型的源代码在此处 。
希望您喜欢这篇文章,
最好的祝福,
拍
翻译自: https://www.javacodegeeks.com/2010/07/building-your-own-gwt-spring-manen.html