MAVEN ARCHETYPE搭建项目模板

MAVENARCHETYPE搭建项目模板

1):进入放入项目的文件夹

                                                              

1

2):使用命令mvn archetype:create-from-project创建需要的模板项目

                                                            

2

                                                           

3

3):创建成功信息BUILD SUCCESS。创建的模板目录在

D:\achetype\demo-ruleapp\target\generated-sources\archetype

                             

4

4):定制需要的模板

create-from-project 命令说明:

a).maven提供的基础命令,使用这个命令创建模板会默认查找最短的包名并使用${package} 。

b).假设这是我们模板文件中的一段代码如:图5。可以看出最小的包名是com.meyacom(因为第一个import有fw字段),所以使用create-from-project命令最后得出的文件应该是如:图6(请注意#set等关键字)。

                                                             

                                                           

5

                                                            

6

 

 

c).当parentpom.xml文件中的<artifactId>demo-ruleapp</artifactId>

的属性值和项目子模块文件名一致时如:图7,图8。Maven命令自动会使用占位符替换如:图9。当使用模板的时候会用artifactId将占位符替换

                                                       

7

                                                       

8

                                                   

9

变量替换的说明:

a).文件路径

xxx\target\generated-sources\archetype\src\main\resources\archetype-resources\__rootArtifactId__-web\src\main\resources

 

 

b).配置文件使用占位符:

打开ruleapp-context.properties文件会有如下类似的东东:

#set( $symbol_pound = '#' )

#set( $symbol_dollar = '$' )

#set( $symbol_escape = '\' )

log4j.location=src/main/resources/

oracle.jdbc.jdbcUrl=jdbc:oracle:thin:@localhost:1521:test

oracle.jdbc.user=chenxx

oracle.jdbc.password=root

oracle.jdbc.driver=oracle.jdbc.driver.OracleDriver

# CAS Configuration

cas.client.servlet=http://localhost:9881/ruleapp-ins-web/j_spring_cas_security_check

# SSO Enabled

cas.server.login=http://localhost:7777/login

cas.server.ticketvalidator=http://localhost:7777/

cas.server.logout=http://localhost:7777/logout

# SSO logout_to

cas.server.logout_to=http://localhost:9999/index.do

 

c).使用占位符替换所需要的数据库等信息(可以根据需要,不必要全部替换):

#set( $symbol_pound = '#' )

#set( $symbol_dollar = '$' )

#set( $symbol_escape = '\' )

log4j.location=src/main/resources/

 

oracle.jdbc.jdbcUrl=${oracle-jdbc-jdbcUrl}

oracle.jdbc.user=${oracle-jdbc-user}

oracle.jdbc.password=${oracle-jdbc-password}

oracle.jdbc.driver=${oracle-jdbc-driver}

cas.client.servlet=${cas-client-servlet}

cas.server.login=${cas-server-login}

cas.server.ticketvalidator=${cas-server-ticketvalidator}

cas.server.logout=${cas-server-logout}

cas.server.logout_to=${cas-server-logout_to}

 

配置文件说明:

a).配置文件路径如下:

xxxxx\target\generated-sources\archetype\src\main\resources\META-INF\maven\archetype-metadata.xml

 

b).部分配置文件代码(标签说明请查看附录):

                                                           

10

<requiredProperty key="oracle-jdbc-jdbcUrl">

               <defaultValue>jdbc:oracle:thin:@localhost:1521:test</defaultValue>

</requiredProperty>

Key为我们在ruleapp-context.properties文件中使用的占位符

defaultValue属性为该占位符默认的值

archetype.properties配置:

a).文件路径如下:Xxxx/target\generated-sources\archetype\src\test\resources\projects\basic/archetype.properties

 

b).文件内容:

#Tue Aug 1620:25:01 CST 2016

package=it.pkg

version=0.1-SNAPSHOT

groupId=archetype.it

artifactId=basic

 

c).增加需要使用占位符的变量(红色字体为增加的变量)

#Tue Aug 1620:25:01 CST 2016

package=it.pkg

version=0.1-SNAPSHOT

groupId=archetype.it

artifactId=basic

jdbc-oracle-url=url

………..

注意:jdbc-oracle-urlarchetype-metadata.xml文件中requiredProperties节点的 key属性(如下图),等于号后面的可以任意命名

最重要的说明:

  a).create-from-project并不是一个很智能的命令,大多时候使用命令生成的模板都不能满足需求。如下图的分析

                                                                        

11

图11为生成模板前的一个片段代码,注意红框的地方。接下来,使用命令生成模板我们来看下实际的效果。图12,13为parent模块的生成模板前后的代码对比

<version>1.1.0</version>其中的1.1.0默认使用了${version}占位符。这看似没有什么问题。当我们找到图11生成模板之后的代码片段如图14所示。可以看出

生成的模板和我们需要的模板是有一定的差距的。正确的模板应该是如图15所示

                                                                    

                                                                    

12

                                                                  

13

                                                                   

14

           

15

b).解决上述问题的两种方法:1).每个文件去检查每个占位符是否正确

                          2).手动的去替换每一个文件,自己先写好每个文件需要使用占位符的地方,然后覆盖使用命令生成的使用占位符的文件。

上述过程需要细心和耐心,心情不好的时候请勿尝试!!!!

c).当我们要生成的模板src/test/javasrc/main/java里面没有java文件时,maven会自动把文件去掉。结果就是我们使用模板生成的项目里面会没有src/test/javasrc/main/java这些目录(当然我们可以可以自己新建这些source folder)。虽然在生成项目后不会报错也不会影响项目运行。但是我们查看build path——>source可以看出有如下错误。如图16

                                                             

 

16

 

为了避免出现这种情况。建议在配置文件中加入如下的fileset

<fileSet filtered="true"encoding="UTF-8">

         <directory>src/test/java</directory>

         <includes>

           <include>**/*.java</include>

         </includes>

</fileSet>

//main目录下没有java文件

<fileSet filtered="true"encoding="UTF-8">

         <directory>src/main/java</directory>

         <includes>

           <include>**/*.java</include>

         </includes>

 </fileSet>

 

 

 

 

遇到的问题及解决思路

 

1):解决“Dynamic Web Module 3.0 requires Java 1.6 ornewer.”错误

(自己的demo,非本项目在此先做记录)

在项目的pom.xml<build></build>标签中加入: 

Xml代码  

 

1.  <plugins>  

2.      <plugin>  

3.          <groupId>org.apache.maven.plugins</groupId>  

4.          <artifactId>maven-compiler-plugin</artifactId>  

5.          <version>2.3.2</version>  

6.          <configuration>  

7.              <source>1.6</source>  

8.              <target>1.6</target>  

9.          </configuration>  

10.     </plugin>  

11. </plugins>  

 2):出现如下错误。

 

1.查看M2_HOME 的path路径是否正确。

2.如果正确。恭喜你,可以换系统了。

3):使用模板新建项目,出现Could not saveencoding settings.错误。

1.查看workspace编码格式是否是utf-8

2.jsp格式是否是utf-8

3.确保.xml和.Properties的编码格式为utf-8(.xml文件右键--àproperties查看编码格式)

4. 新建MAVEN_OPTS环境变量,属性值为 -Dfile.encoding=utf-8

 


附录:参考文档

参考文档:https://maven.apache.org/archetype/archetype-models/archetype-descriptor/archetype-descriptor.html

 

 

archetype-descriptor

 

Attribute

Type

Description

name

String

Name of the Archetype, that will be displayed to the user when choosing an archetype.

partial

boolean

Is this archetype representing a full Maven project or only parts?
Default value isfalse.

 

Element

Type

Description

requiredProperties/requiredProperty*

List<RequiredProperty>

(Many) List of required properties to generate a project from this archetype.

fileSets/fileSet*

List<FileSet>

(Many) File sets definition.

modules/module*

List<ModuleDescriptor>

(Many) Modules definition.

requiredProperty

Definition of a propertyrequired when generating a project from this archetype.

Attribute

Type

Description

key

String

Key value of the property.

 

Element

Type

Description

defaultValue

String

Default value of the property.

fileSet

A fileset defines the waythe project's files located in the jar file are used by the Archetype Plugin togenerate a project. If file or directory name contains__property__ pattern, it is replaced with corresponding property value.

Attribute

Type

Description

filtered

boolean

Filesets can be filtered, which means the selected files will be used as Velocity templates. They can be non-filtered, which means the selected files will be copied without modification. 
Default value isfalse.

packaged

boolean

Filesets can be packaged, which means the selected files will be generated/copied in a directory structure that is prepended by the package property. They can be non-packaged, which means that the selected files will be generated/copied without that prepend.
Default value isfalse.

encoding

String

Encoding to use when filtering content.

 

Element

Type

Description

directory

String

The directory where the files will be searched for, which is also the directory where the project's files will be generated.

includes/include*

List<String>

(Many) Inclusion definition "à la" Ant.

excludes/exclude*

List<String>

(Many) Exclusion definition "à la" Ant.

module

Attribute

Type

Description

id

String

The module's artifactId.

dir

String

The module's directory.

name

String

The module's name.

 


 感谢阅读本文,欢迎各位提出建议和意见,如有问题大家一起探讨,如需转载请注明出处!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

趣味花生牛奶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值