maven使用当前工程生成自定义archetype

本文详细介绍了如何使用Maven自定义Web工程的目录结构,包括使用Maven-archetype-webapp新建工程、修改目录结构、配置archetype-descriptor等步骤,并通过命令行和Eclipse操作实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原料:maven3,eclipse 4.4.2

需求:领导希望使用自定义的目录结构创建maven工程.

maven默认web工程目录结构如下:


希望自定义为:


step1:使用 maven-archetype-webapp 默认原型新建一个web工程.

step2:修改目录结构为自定义的.

step3:打开命令行,切换到工程根目录,执行 mvn archetype:create-from-project会在生成一个目录为target\generated-sources\archetype将这个文件夹整个复制出来.比如到G盘.将 archetype\src\main\resources\archetype-resources 下的 .setting,.classpath,.project 文件/文件夹删除

step4:在 archetype\src\main\resources\META-INF 新建文件 

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>webapp3-archetype</id>

  <sources>
  	<!-- 指定源代码 -->
    <source>src/Web3Servlet.java</source>
  </sources>
  <!-- 指定测试源代码 -->
  <testSources>
	<source>test/TestApp.java</source>
  </testSources>
</archetype>

修改  archetype\src\main\resources\META-INF\archetype-metadata.xml  指定工程需要的目录

<?xml version="1.0" encoding="UTF-8"?>
<archetype-descriptor xsi:schemaLocation="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0 http://maven.apache.org/xsd/archetype-descriptor-1.0.0.xsd" name="webapp3"
    xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <fileSets>
    <fileSet filtered="true" encoding="UTF-8">
      <directory>src</directory>
      <includes>
        <include>**/*.java</include>
      </includes>
    </fileSet>
     <fileSet filtered="true" encoding="UTF-8">
      <directory>test</directory>
      <includes>
        <include>**/*.java</include>
      </includes>
    </fileSet>
    <fileSet filtered="true" encoding="UTF-8">
      <directory>WebRoot</directory>
      <includes>
        <include>**/*.jsp</include>
      </includes>
    </fileSet>
    <fileSet filtered="true" encoding="UTF-8">
      <directory>WebRoot/WEB-INF</directory>
      <includes>
        <include>**/*.xml</include>
      </includes>
    </fileSet>
  </fileSets>
</archetype-descriptor>

step5:在命令行将目录切换到修改完后的archetype目录.执行 mvn clean install 

step6:在eclipse中添加该原型:

file->new->maven project->next 点击Add Archetype填入坐标.就行了(可能需要勾选 Include snapshot archetypes才能显示出来).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值