图1
图2
图3
D:\achetype\demo-ruleapp\target\generated-sources\archetype
图4
a).maven提供的基础命令,使用这个命令创建模板会默认查找最短的包名并使用${package} 。
b).假设这是我们模板文件中的一段代码如:图5。可以看出最小的包名是com.meyacom(因为第一个import有fw字段),所以使用create-from-project命令最后得出的文件应该是如:图6(请注意#set等关键字)。
图5
图6
c).当parent的pom.xml文件中的
的属性值和项目子模块文件名一致时如:图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
Key为我们在ruleapp-context.properties文件中使用的占位符
defaultValue属性为该占位符默认的值
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-url为archetype-metadata.xml文件中requiredProperties节点的 key属性(如下图),等于号后面的可以任意命名
a).create-from-project并不是一个很智能的命令,大多时候使用命令生成的模板都不能满足需求。如下图的分析
图11
图11为生成模板前的一个片段代码,注意红框的地方。接下来,使用命令生成模板我们来看下实际的效果。图12,13为parent模块的生成模板前后的代码对比
生成的模板和我们需要的模板是有一定的差距的。正确的模板应该是如图15所示
图12
图13
图14
图15
b).解决上述问题的两种方法:1).每个文件去检查每个占位符是否正确
2).手动的去替换每一个文件,自己先写好每个文件需要使用占位符的地方,然后覆盖使用命令生成的使用占位符的文件。
上述过程需要细心和耐心,心情不好的时候请勿尝试!!!!
c).当我们要生成的模板src/test/java和src/main/java里面没有java文件时,maven会自动把文件去掉。结果就是我们使用模板生成的项目里面会没有src/test/java和src/main/java这些目录(当然我们可以可以自己新建这些source folder)。虽然在生成项目后不会报错也不会影响项目运行。但是我们查看build path——>source可以看出有如下错误。如图16
图16
为了避免出现这种情况。建议在配置文件中加入如下的fileset
//当main目录下没有java文件
1):解决“Dynamic Web Module 3.0 requires Java 1.6 ornewer.”错误
(自己的demo,非本项目在此先做记录)
在项目的pom.xml的
Xml代码
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
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
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? |
Element |
Type |
Description |
requiredProperties/requiredProperty* |
List |
(Many) List of required properties to generate a project from this archetype. |
fileSets/fileSet* |
List |
(Many) File sets definition. |
modules/module* |
List |
(Many) Modules definition. |
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. |
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. |
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. |
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 |
(Many) Inclusion definition "à la" Ant. |
excludes/exclude* |
List |
(Many) Exclusion definition "à la" Ant. |
Attribute |
Type |
Description |
id |
String |
The module's artifactId. |
dir |
String |
The module's directory. |
name |
String |
The module's name. |
感谢阅读本文,欢迎各位提出建议和意见,如有问题大家一起探讨,如需转载请注明出处!