如何使我的原型从多模块项目中使用基于artifactId的目录?

时间:2011-07-16 02:13:07

标签: build-process maven-3 archetypes

我使用archetype创建了一个原型:从多模块项目中创建项目。

archetype-metadata.xml如下所示,我想要的是当我运行mvn archetype时可以修改“dir”:使用我提供的archetypeId而不是使用固定的dir生成。可以这样做吗?

 <?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="service-parent"
     xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <modules>
     <module id="service-def" dir="service-def" name="service-def">
       <fileSets>
         <fileSet filtered="true" packaged="true" encoding="UTF-8">
           <directory>src/main/java</directory>
           <includes>
             <include>**/*.java</include>
           </includes>
         </fileSet>
       </fileSets>
     </module>
     <module id="service" dir="service" name="service">
       <fileSets>
         <fileSet filtered="true" packaged="true" encoding="UTF-8">
           <directory>src/main/java</directory>
           <includes>
             <include>**/*.java</include>
           </includes>
         </fileSet>
         <fileSet filtered="true" packaged="true" encoding="UTF-8">
           <directory>src/test/java</directory>
           <includes>
             <include>**/*.java</include>
           </includes>
         </fileSet>
         <fileSet filtered="true" encoding="UTF-8">
           <directory>src/test/resources</directory>
           <includes>
             <include>**/*.xml</include>
             <include>**/*.properties</include>
           </includes>
         </fileSet>
         <fileSet encoding="UTF-8">
           <directory>src/test/resources</directory>
           <includes>
             <include>**/*.sql</include>
             <include>**/*.dtd</include>
           </includes>
         </fileSet>
       </fileSets>
     </module>
     <module id="service-web" dir="service-web" name="service-web">
       <fileSets>
         <fileSet filtered="true" encoding="UTF-8">
           <directory>src/main/webapp</directory>
           <includes>
             <include>**/*.xml</include>
           </includes>
         </fileSet>
         <fileSet filtered="true" encoding="UTF-8">
           <directory>src/main/resources</directory>
           <includes>
             <include>**/*.xml</include>
           </includes>
         </fileSet>
       </fileSets>
     </module>
   </modules>
 </archetype-descriptor>

这是原型的结构:

 ├── pom.xml
 └── src
     ├── main
     │   └── resources
     │       ├── archetype-resources
     │       │   ├── pom.xml
     │       │   ├── service
     │       │   │   ├── pom.xml
     │       │   │   └── src
     │       │   │       ├── main
     │       │   │       │   └── java
     │       │   │       └── test
     │       │   │           ├── java
     │       │   │           └── resources
     │       │   ├── service-def
     │       │   │   ├── pom.xml
     │       │   │   └── src
     │       │   │       └── main
     │       │   │           └── java
     │       │   └── service-web
     │       │       ├── pom.xml
     │       │       └── src
     │       │           └── main
     │       │               ├── resources
     │       │               │   ├── dao-context.xml
     │       │               │   ├── hibernate.cfg.xml
     │       │               │   └── single-context.xml
     │       │               └── webapp
     │       │                   └── WEB-INF
     │       │                       ├── jboss-web.xml
     │       │                       ├── remoting-servlet.xml
     │       │                       └── web.xml
     │       └── META-INF
     │           └── maven
     │               └── archetype-metadata.xml
     └── test
         └── resources
             └── projects
                 └── basic
                     ├── archetype.properties
                     └── goal.txt

1 个答案:

答案 0 :(得分:11)

您需要使用 rootArtifactId 占位符,例如:

 <?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="service-parent"
 xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <modules>
   <module id="${rootArtifactId}-def" dir="__rootArtifactId__-def" name="${rootArtifactId}-def">
     <fileSets>
       <fileSet filtered="true" packaged="true" encoding="UTF-8">
         <directory>src/main/java</directory>
         <includes>
           <include>**/*.java</include>
         </includes>
       </fileSet>
     </fileSets>
   </module>
   <module id="${rootArtifactId}" dir="__rootArtifactId__" name="${rootArtifactId}">
     <fileSets>
       <fileSet filtered="true" packaged="true" encoding="UTF-8">
       <directory>src/main/java</directory>
       <includes>
         <include>**/*.java</include>
       </includes>
     </fileSet>
     <fileSet filtered="true" packaged="true" encoding="UTF-8">
       <directory>src/test/java</directory>
       <includes>
         <include>**/*.java</include>
       </includes>
     </fileSet>
     <fileSet filtered="true" encoding="UTF-8">
       <directory>src/test/resources</directory>
       <includes>
         <include>**/*.xml</include>
         <include>**/*.properties</include>
       </includes>
     </fileSet>
     <fileSet encoding="UTF-8">
         <directory>src/test/resources</directory>
         <includes>
           <include>**/*.sql</include>
           <include>**/*.dtd</include>
         </includes>
       </fileSet>
     </fileSets>
   </module>
   <module id="${rootArtifactId}-web" dir="__rootArtifactId__-web" name="${rootArtifactId}-web">
     <fileSets>
       <fileSet filtered="true" encoding="UTF-8">
         <directory>src/main/webapp</directory>
         <includes>
           <include>**/*.xml</include>
         </includes>
       </fileSet>
       <fileSet filtered="true" encoding="UTF-8">
         <directory>src/main/resources</directory>
          <includes>
           <include>**/*.xml</include>
         </includes>
       </fileSet>
     </fileSets>
   </module>
 </modules>

使用__rootArtifactId__前缀重命名模块文件夹。

您可以在http://code.google.com/p/open-archetypes/source/browse/multi-javaee5-archetype/src/main/resources/META-INF/maven/archetype-metadata.xml

中查看此类原型的示例