zoukankan      html  css  js  c++  java
  • 创建基于maven的项目模版

    我们在实际工作中 ,有些项目的架构是相似的,例如基于 restful的接口项目,如果每次都重新搭建一套架构或者通过拷贝建立一个项目难免有些得不偿失,这里我们可以用maven的archtype建立项目模版来解决 。

    创建maven archetype的步骤:

    • archetype组成:
    1. archetype.xml  (src/main/resources/META-INF/maven/)
    2. 框架本身的源码和资源文件模版  src/main/resources/archetype-resources/
    3. 框架本身的pom文件模版 src/main/resources/archetype-resources/
    4. 生成框架的这个archetype的pom

    archetype的pom,这里面的groupId,artifactId,version会在执行maven 的archetype generate 时显示

    其中pom中的 id应该和archetype.xml里面的artifactId一致

                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/xsd/maven-4.0.0.xsd">
    
      <modelVersion>4.0.0</modelVersion>
    
     
    
      <groupId>my.groupId</groupId>
    
      <artifactId>quickstart</artifactId>
    
      <version>1.0-SNAPSHOT</version>
    
      <packaging>jar</packaging>
    
    </project>

    archetype.xml  

     

    <archetype xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0 http://maven.apache.org/xsd/archetype-1.0.0.xsd">
    
      <id>quickstart</id>
    
      <sources>
    
        <source>src/main/java/App.java</source>
    
      </sources>
    
      <testSources>
    
        <source>src/test/java/AppTest.java</source>
    
      </testSources>
    
    </archetype>

     

    • 标签说明:

     <allowPartial>true</allowPartial>  该标签说明在一个已经存在的项目中依然可以运行 mvm archetype

     <sources>, <resources>, <testSources>, <testResources> , <siteResources>代表项目的不同部分

      • <sources> = src/main/java
      • <resources> = src/main/resources
      • <testSources> = src/test/java
      • <testResources> = src/test/resources
      • <siteResources> = src/site

     

    <sources><testSources>包含<source>,这个表示里面的一个源码文件

    <testResources> a <siteResources>包含<resource>,这个表示里面是一个资源文件

     

    • 创建一个架构的pom模版
    <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/xsd/maven-4.0.0.xsd">
    
      <modelVersion>4.0.0</modelVersion>
    
     
    
      <groupId>${groupId}</groupId>
    
      <artifactId>${artifactId}</artifactId>
    
      <version>${version}</version>
    
      <packaging>jar</packaging>
    
      <name>A custom project</name> 
    
      <dependencies>
    
        <dependency>
    
          <groupId>junit</groupId>
    
          <artifactId>junit</artifactId>
    
          <version>3.8.1</version>
    
          <scope>test</scope>
    
        </dependency>
    
      </dependencies>
    
    </project>
    
     
    
    ${artifactId}和${groupId} 将在执行maven archetype 里面提示

     

    配置完后,执行

    mvn install完成安装

     

     

    安装后,执行

     

    mvn archetype:generate                                 

      -DarchetypeGroupId=<archetype-groupId>               

      -DarchetypeArtifactId=<archetype-artifactId>         

      -DarchetypeVersion=<archetype-version>               

      -DgroupId=<my.groupid>                               

      -DartifactId=<my-artifactId>

     

    可以完成该架构的安装

     

     

     

    eg:

    mvn archetype:generate                                 

      -DarchetypeGroupId=com.taobao.data.arch              

      -DarchetypeArtifactId=jersey-quickstart         

      -DarchetypeVersion=1.0-SNAPSHOT              

      -DgroupId=com.taobao.data.zl                               

      -DartifactId=testjersey

     

  • 相关阅读:
    jQuery过滤器 过滤器
    查询总结
    ado.net
    禅修程序员十诫
    Asp.net中Bind()和Eval()的区别
    什么是好代码?
    12 个有效的提高编程技能的方法
    Visual Studio各种版本之间的转换工具
    程序员的培养
    session和cookie的区别和联系!
  • 原文地址:https://www.cnblogs.com/wully/p/3437709.html
Copyright © 2011-2022 走看看