posts - 2, comments - 27, trackbacks - 0, articles - 60
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

使用Maven创建项目模板

Posted on 2019-06-24 10:55 ZhouFeng 阅读(271) 评论(0)  编辑  收藏 所属分类: Web开发
在开发项目的时候,有许多项目结构都是差不多的,使用的框架也一样,spring配置文件也一样的,只是每个项目的名称,包名等不同,如果按照之前的方法,可以拷贝一个项目,然后再做一些修改就好了。这也不是不可以,只是我觉得,应该还有更帅的招,Maven可以使用archetype创建项目,那是否也可以创建自定义结构的项目了,所以就了解了一下通过Maven使用现有项目创建项目模板。尝试了一下,很不错,可以自动处理包名等转换,几个命令就可以生成雷同结构的项目,在此记录一下过程

1 准备好一个现有的项目

既然做为模板项目,里面的配置都是做好了的,是一个可以用的项目,加入了Eclipse支持,项目中包含一些Eclipse配置文件,本例中项目目录为
D:\workspace\myprj

2 创建模板工程

进入项目目录后,运行生成archetype命令
D:\workspace\myprj>mvn archetype:create-from-project
会在myprj目录下生成target目录,其下生成如下目录
classes
generated-sources
generated-test-sources
m2e-wtp
maven-status
surefire-reports
test-classes
模板代码主要位于generated-sources目录中

3 修改模板代码

项目中的所有代码都会自动替换为占位符,除了Eclipse配置文件之外,接下来修改模板,进入target\generated-sources\archetype\src\main\resources\archetype-resources\目录,将Eclipse配置文件也加入占位符,以方便新项目的生成,比如修改下面两个文件,加入占位符artifactId
文件1:.project
#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
        <name>${artifactId}</name>
        <comment></comment>
        <projects>
        </projects>
        <buildSpec>
                <buildCommand>
                        <name>org.eclipse.jdt.core.javabuilder</name>
                        <arguments>
                        </arguments>
                </buildCommand>
                <buildCommand>
                        <name>org.eclipse.wst.common.project.facet.core.builder</name>
                        <arguments>
                        </arguments>
                </buildCommand>
                <buildCommand>
                        <name>org.eclipse.wst.validation.validationbuilder</name>
                        <arguments>
                        </arguments>
                </buildCommand>
                <buildCommand>
                        <name>org.eclipse.m2e.core.maven2Builder</name>
                        <arguments>
                        </arguments>
                </buildCommand>
        </buildSpec>
        <natures>
                <nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
                <nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
                <nature>org.eclipse.jdt.core.javanature</nature>
                <nature>org.eclipse.m2e.core.maven2Nature</nature>
                <nature>org.eclipse.wst.common.project.facet.core.nature</nature>
                <nature>org.eclipse.wst.jsdt.core.jsNature</nature>
        </natures>
</projectDescription>
文件2:.settings\org.eclipse.wst.common.component
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
    <wb-module deploy-name="${artifactId}">
        <wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
        <wb-resource deploy-path="/" source-path="/src/main/webapp" tag="defaultRootSource"/>
        <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
        <wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
        <property name="context-root" value="${artifactId}"/>
        <property name="java-output-path" value="/${artifactId}/target/classes"/>
    </wb-module>
</project-modules>

4 修改项目文件列表

配置完成后,通过mvn生成新的项目时,上面的Eclipse配置文件也不会被放入新的工程里,需要在配置文件中加入这些文件的信息,以及标记哪些文件是需要使用占位符替换的,修改target\generated-sources\archetype\src\main\resources\META-INF\maven\archetype-metadata.xml文件,在现有文件中可以看到如下信息
<fileSet filtered="true" encoding="UTF-8">
    <directory>.settings</directory>
    <includes>
        <include>**/*.xml</include>
    </includes>
</fileSet>
也就是说,默认情况下,mvn只将.settings目录下的*.xml文件生成到项目中,其中filtered="true"表示需要进行内容过滤替换占位符,我们把需要添加的文件放入此配置,修改后结果如下
<fileSet filtered="true" encoding="UTF-8">
    <directory>.settings</directory>
    <includes>
        <include>**/*.xml</include>
        <include>**/*.container</include>
        <include>**/*.component</include>
        <include>**/*.name</include>
        <include>**/*.jsdtscope</include>
        <include>**/*.prefs</include>
    </includes>
</fileSet>
<fileSet filtered="true" encoding="UTF-8">
    <directory></directory>
    <includes>
        <include>.classpath</include>
        <include>.project</include>
    </includes>
</fileSet>
如果需要过滤掉一些不需要的文件,也可以在此配置。准备好后,就可以安装了

5 将模板项目安装到本地仓库

进入/target/generated-sources/archetype/目录,执行安装
D:\workspace\myprj\target\generated-sources\archetype>mvn install
此命令会在maven仓库中生成archetype-catalog.xml文件,仓库位置取决于maven配置文件settings.xml中的设置,内容如下,可以修改描述信息,方便在创建项目时进行提示
<?xml version="1.0" encoding="UTF-8"?>
<archetype-catalog xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0 http://maven.apache.org/xsd/archetype-catalog-1.0.0.xsd"
    xmlns
="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-catalog/1.0.0"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance">
  <archetypes>
    <archetype>
      <groupId>com.zhouf</groupId>
      <artifactId>myprj-archetype</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <description>simple project with springmvc and jetty</description>
    </archetype>
  </archetypes>
</archetype-catalog>

6 创建项目测试

执行生成命令
D:\workspace>mvn archetype:generate -DarchetypeCatalog=local
此命令可以在任何目录下执行,会在当前目录生成项目
Choose archetype:
1: local -> com.zhouf:myprj-archetype (simple project with springmvc and jetty)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1
Define value for property 'groupId': com.zhouf
Define value for property 'artifactId': demoprj1
Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'package' com.zhouf: :
Confirm properties configuration:
groupId: com.zhouf
artifactId: demoprj1
version: 1.0-SNAPSHOT
package: com.zhouf
 Y: :
生成项目demoprj1

7 导入Eclipse运行

将生成的项目导入到Eclipse环境中,直接部署到服务器上跑一个,测试通过,不需要修改配置,帅
如果项目是通过mvn jetty:run运行的,也可以省略中间加入Eclipse配置文件的操作

转自我的简书:https://www.jianshu.com/p/fb5c0e39692f

只有注册用户登录后才能发表评论。


网站导航: