---
title: "maven2：从现有工程中创建archetype"
author: "lingshangwen"
date: "2009-05-14T21:40:40Z"
tags: []
slug: "maven2-archetype"
draft: false
lastmod: "2009-05-14T21:46:31Z"
id: 387088
url: "https://eagle516.iteye.com/blog/387088"
---

最近用spring2.5.6+hibernate3.2+struts2.0.14做项目，用这些框架做项目最鬼麻烦的就是搭建工程的基本原型，为了在以后不用在这么麻烦，于是就有了创建一个自己的ssh的archetype的念头，说干就干，开始动手。

 

1.创建一个maven项目(在eclipse 中通过m2eclipse创建或者用mvn archetype:genetate命令创建)。

2.添加spring，hibernate，struts2的依赖包，我的pom.xml如下:

```
<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>org.eagle</groupId>
	<artifactId>ssh-base</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.0.2</version>
				<configuration>
					<source>1.6</source>
					<target>1.6</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>maven-jetty-plugin</artifactId>
				<version>6.1.14</version>
			</plugin>
		</plugins>
	</build>

	<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<type>jar</type>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring</artifactId>
			<version>2.5.6</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.6.2</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.6.2</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.0.14</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-spring-plugin</artifactId>
			<version>2.0.14</version>
			<type>jar</type>
			<scope>compile</scope>
			<exclusions>
				<exclusion>
					<artifactId>spring-beans</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
				<exclusion>
					<artifactId>spring-core</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
				<exclusion>
					<artifactId>spring-context</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
				<exclusion>
					<artifactId>spring-web</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate</artifactId>
			<version>3.2.6.ga</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.6</version>
			<type>jar</type>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>c3p0</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.1.2</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.14</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib-nodep</artifactId>
			<version>2.1_3</version>
			<type>jar</type>
			<scope>compile</scope>
		</dependency>
	</dependencies>
</project>
```

 

3.添加基本的配置文件

src/main/resources/applicationContext.xml

```
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
				http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<context:property-placeholder location="WEB-INF/config/jdbc.properties" />

	<!-- 定义数据源Bean，使用C3P0数据源实现 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<!-- 指定连接数据库的驱动 -->
		<property name="driverClass" value="${jdbc.driverClassName}" />
		<!-- 指定连接数据库的URL -->
		<property name="jdbcUrl" value="${jdbc.url}" />
		<!-- 指定连接数据库的用户名 -->
		<property name="user" value="${jdbc.username}" />
		<!-- 指定连接数据库的密码 -->
		<property name="password" value="${jdbc.password}" />
		<!-- 指定连接数据库连接池的最大连接数 -->
		<property name="maxPoolSize" value="20" />
		<!-- 指定连接数据库连接池的最小连接数 -->
		<property name="minPoolSize" value="1" />
		<!-- 指定连接数据库连接池的初始化连接数 -->
		<property name="initialPoolSize" value="1" />
		<!-- 指定连接数据库连接池的连接的最大空闲时间 -->
		<property name="maxIdleTime" value="20" />
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mappingDirectoryLocations">
			<list>
				<value>classpath:org/eagle/orm</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="show_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.jdbc.batch_size">20</prop>
			</props>
		</property>
	</bean>

	<!-- 配置事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- 配置事务的传播性 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="modify*" propagation="REQUIRED" />
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<!-- 配置哪些类哪些方法使用事务 -->
	<aop:config>
		<!-- 配置切入点(定义哪些方法要进行事务处理) -->
		<aop:pointcut id="allManager"
			expression="execution(* org.eagle.services..*.*(..))" />
		<!-- 定义advice -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="allManager" />
	</aop:config>

</beans>
```

 

src/main/resources/struts.xml

```
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

</struts>
```

 

src/main/webapp/WEB-INF/config/jdbc.properties

```
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/shs2
jdbc.username=root
jdbc.password=ubuntu
```

 

src/main/webapp/WEB-INF/config/log4j.properties

```
log4j.rootLogger=INFO, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${webapp.root}/WEB-INF/logs/pcdiy.log
log4j.appender.logfile.MaxFileSize=512KB
# Keep three backup files.
log4j.appender.logfile.MaxBackupIndex=3
# Pattern to output: date priority [category] - message
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
```

 

4.web.xml

```
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>/WEB-INF/config/log4j.properties</param-value>
	</context-param>
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
</web-app>
```

 

 

5.基本上可以，现在来生成archetype的目录结构(假设工程在/home/lingshangwen/project/ssh-base)

进入到工程的根目录ssh-base下

$cd ~/project/ssh-base

$mvn archetype:create-from-project

运行完该命令后,将会有如下的目录~/project/ssh-base/target/generated-sources/archetype

 

6.创建archetype

$cd ~/project/ssh-base/target/generated-sources/archetype

$mvn install

 

到现在为止，我想要的ssh的archetype创建出来了，如何通过该archetype创建项目呢，很简单，看看

$cd ~/project

$mvn archetype:generate

\[INFO] Scanning for projects...  
\[INFO] Searching repository for plugin with prefix: 'archetype'.  
\[INFO] ------------------------------------------------------------------------  
\[INFO] Building Maven Default Project  
\[INFO]    task-segment: \[archetype:generate] (aggregator-style)  
\[INFO] ------------------------------------------------------------------------  
\[INFO] Preparing archetype:generate  
\[INFO] No goals needed for project - skipping  
\[INFO] Setting property: classpath.resource.loader.class =&gt; 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.  
\[INFO] Setting property: velocimacro.messages.on =&gt; 'false'.  
\[INFO] Setting property: resource.loader =&gt; 'classpath'.  
\[INFO] Setting property: resource.manager.logwhenfound =&gt; 'false'.  
\[INFO] \[archetype:generate]  
\[INFO] Generating project in Interactive mode  
\[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)  
Choose archetype:  
1: local -&gt; ssh (null)  
2: local -&gt; ssh-base-archetype (ssh-base-archetype)  
3: internal -&gt; appfuse-basic-jsf (AppFuse archetype for creating a web application with Hibernate, Spring and JSF)  
4: internal -&gt; appfuse-basic-spring (AppFuse archetype for creating a web application with Hibernate, Spring and Spring MVC)  
5: internal -&gt; appfuse-basic-struts (AppFuse archetype for creating a web application with Hibernate, Spring and Struts 2)  
6: internal -&gt; appfuse-basic-tapestry (AppFuse archetype for creating a web application with Hibernate, Spring and Tapestry 4)  
7: internal -&gt; appfuse-core (AppFuse archetype for creating a jar application with Hibernate and Spring and XFire)  
8: internal -&gt; appfuse-modular-jsf (AppFuse archetype for creating a modular application with Hibernate, Spring and JSF)  
9: internal -&gt; appfuse-modular-spring (AppFuse archetype for creating a modular application with Hibernate, Spring and Spring MVC)  
10: internal -&gt; appfuse-modular-struts (AppFuse archetype for creating a modular application with Hibernate, Spring and Struts 2)  
11: internal -&gt; appfuse-modular-tapestry (AppFuse archetype for creating a modular application with Hibernate, Spring and Tapestry 4)  
12: internal -&gt; maven-archetype-j2ee-simple (A simple J2EE Java application)  
13: internal -&gt; maven-archetype-marmalade-mojo (A Maven plugin development project using marmalade)  
14: internal -&gt; maven-archetype-mojo (A Maven Java plugin development project)  
15: internal -&gt; maven-archetype-portlet (A simple portlet application)  
16: internal -&gt; maven-archetype-profiles ()  
17: internal -&gt; maven-archetype-quickstart ()  
18: internal -&gt; maven-archetype-site-simple (A simple site generation project)  
19: internal -&gt; maven-archetype-site (A more complex site project)  
20: internal -&gt; maven-archetype-webapp (A simple Java web application)  
21: internal -&gt; jini-service-archetype (Archetype for Jini service project creation)  
22: internal -&gt; softeu-archetype-seam (JSF+Facelets+Seam Archetype)  
23: internal -&gt; softeu-archetype-seam-simple (JSF+Facelets+Seam (no persistence) Archetype)  
24: internal -&gt; softeu-archetype-jsf (JSF+Facelets Archetype)  
25: internal -&gt; jpa-maven-archetype (JPA application)  
26: internal -&gt; spring-osgi-bundle-archetype (Spring-OSGi archetype)  
27: internal -&gt; confluence-plugin-archetype (Atlassian Confluence plugin archetype)  
28: internal -&gt; jira-plugin-archetype (Atlassian JIRA plugin archetype)  
29: internal -&gt; maven-archetype-har (Hibernate Archive)  
30: internal -&gt; maven-archetype-sar (JBoss Service Archive)  
31: internal -&gt; wicket-archetype-quickstart (A simple Apache Wicket project)  
32: internal -&gt; scala-archetype-simple (A simple scala project)  
33: internal -&gt; lift-archetype-blank (A blank/empty liftweb project)  
34: internal -&gt; lift-archetype-basic (The basic (liftweb) project)  
35: internal -&gt; cocoon-22-archetype-block-plain (\[http://cocoon.apache.org/2.2/maven-plugins/])  
36: internal -&gt; cocoon-22-archetype-block (\[http://cocoon.apache.org/2.2/maven-plugins/])  
37: internal -&gt; cocoon-22-archetype-webapp (\[http://cocoon.apache.org/2.2/maven-plugins/])  
38: internal -&gt; myfaces-archetype-helloworld (A simple archetype using MyFaces)  
39: internal -&gt; myfaces-archetype-helloworld-facelets (A simple archetype using MyFaces and facelets)  
40: internal -&gt; myfaces-archetype-trinidad (A simple archetype using Myfaces and Trinidad)  
41: internal -&gt; myfaces-archetype-jsfcomponents (A simple archetype for create custom JSF components using MyFaces)  
42: internal -&gt; gmaven-archetype-basic (Groovy basic archetype)  
43: internal -&gt; gmaven-archetype-mojo (Groovy mojo archetype)  
Choose a number:  (1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41/42/43) 17: :

 

ssh-base的archetype为2，故选择2

 

~~有个这个archetype以后创建ssh项目就快多了