在Maven原型中更改包属性 [英] Change package property in Maven Archetype

查看:89
本文介绍了在Maven原型中更改包属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Maven原型.我的META-INF/maven/archetype-metadata.xml看起来像这样:

<archetype-descriptor xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  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">

  <fileSets>
    <fileSet filtered="true" packaged="true" encoding="UTF-8" >
      <directory>src/main/java</directory>
    </fileSet>
  </fileSets>

</archetype-descriptor>

这有效,因为它创建了Java源文件夹并将我的类放入groupIdartifactId所定义的包中.

但是,我想修改他的包裹名称.例如,如果我的groupId为com.example而我的artifactId为wvdz,则我的软件包应为:

com.example.wvdz.mypackage

问题:我该怎么做?

解决方案

要实现您的目标,并且由于您已经在true中使用了packaged属性(稍后说明),只需在以下路径中添加目录即可

保持相同的配置,并带有一个附加的include元素,如下所示:

<fileSets>
    <fileSet filtered="true" packaged="true" encoding="UTF-8" >
      <directory>src/main/java</directory>
        <includes>
            <include>**/*.java</include>
        </includes>       
    </fileSet>
</fileSets>

然后,您可以在src/main/java/mypackage您的Java源代码 templated 下放置package语句,如下所示:

package ${package}.mypackage

请注意,.mypackage恰好反映了直接在src/main/java下的mypackage文件夹.但是,在创建原型时,Maven将作为文件夹(例如包)放置在$ {package}属性值之间,该属性值默认为$ {groupId}.

您始终可以传递-Dpackage属性,并将其覆盖默认值(groupId),然后将根据上面的模板将其用作包的前缀.

之所以发生这种情况,是因为上面的fileSet部分中的packaged属性设置为true.在这种情况下,true表示:向其中添加${package}属性指定的文件夹层次结构.将其设置为false会导致${package}被忽略,如果您确实想对文件夹结构进行硬编码并将其明显地反映到Java代码的package语句中以保持一致性,则可以使用该. >


上述行为记录在官方方式中有关原型的元数据已存储?:

原型定义了一个文件集:

  • 文件集将采用archetype-resources/src/main/java中与**/*.java模式匹配的所有文件
  • 所选文件将使用Velocity引擎(filtered=true)生成
  • 文件将在与JAR文件相同的目录中的已生成项目的src/main/java目录中生成,但该目录前面带有package属性.

还有:

文件集可以为packaged,这意味着所选文件将在package属性之前的目录结构中生成/复制.它们可以是非打包的,这意味着所选文件将在没有此前缀的情况下生成/复制.

相同的详细信息(关于packaged属性)也可以在官方

请注意新的requiredProperties部分:在这里,我们为package属性设置默认值,不再需要在运行时提供它(尽管可以覆盖上面的值).

这样,src/main/java下的Java源模板(无需其他静态文件夹)将简单地是:

package ${package}

在创建(archetype:generate)期间,Maven将使用com.sample.something.mypackage作为包值(在Java源文件中),并用值com/sample/something/mypackage填充packageInPathFormat属性(相同的属性,但在路径中)格式)并创建所需的包层次结构,与Java源代码预期放置的包层次结构保持一致.

I created a Maven Archetype. My META-INF/maven/archetype-metadata.xml looks like this:

<archetype-descriptor xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  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">

  <fileSets>
    <fileSet filtered="true" packaged="true" encoding="UTF-8" >
      <directory>src/main/java</directory>
    </fileSet>
  </fileSets>

</archetype-descriptor>

This works, as in that it creates a Java source folder and puts my classes in the package as defined by the groupId and the artifactId.

However, I want to modify his package name. For example, if my groupId is com.example and my artifactId wvdz, then my package should be:

com.example.wvdz.mypackage

Question: How do I accomplish this?

解决方案

To accomplish your objectives and since you are already using the packaged attribute to true (explained later), you can simply add directories to your path below.

Keeping the same configuration, with an additional include element as following:

<fileSets>
    <fileSet filtered="true" packaged="true" encoding="UTF-8" >
      <directory>src/main/java</directory>
        <includes>
            <include>**/*.java</include>
        </includes>       
    </fileSet>
</fileSets>

You could then place under src/main/java/mypackage your Java sources templated where the package statement would be as following:

package ${package}.mypackage

Note the .mypackage reflects exactly the mypackage folder directly under src/main/java. However, when creating the archetype, Maven will then place as a folder (and as such as a package) in between the ${package} property value, which by default would be the ${groupId}.

You can always pass the -Dpackage property and override it the default value (the groupId), which will then be used as a prefix of the package, based on the template above.

This happens because of the packaged attribute set to true in the fileSet section above. In this case true means: add to it the folder hierarchy specified by the ${package} property. Setting it at false would result in ${package} ignored, which can be used if you really want to hard-code the folder structure and obviously reflect it in to the package statement of your Java code, for consistency.


The behavior above is documented in the official How is metadata about an archetype stored?:

the archetype defines a single fileset:

  • the fileset will take all the files in archetype-resources/src/main/java that match the **/*.java pattern
  • the selected files will be generated using the Velocity engine (filtered=true)
  • the files will be generated in the src/main/java directory of the generated project in the same directory as in the JAR file, but with that directory prepended by the package property.

And also:

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.

The same details (about the packaged property) can also be found in the official Archetype Descriptor Model.


Another possible solution is to use an additional property or define your package property value directly in the archetype-metadata.xml file as following:

<archetype-descriptor
    xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype-descriptor/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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">

    <fileSets>
        <fileSet filtered="true" packaged="true" encoding="UTF-8">
            <directory>src/main/java</directory>
        </fileSet>
    </fileSets>

    <requiredProperties>
        <requiredProperty key="package">
            <defaultValue>${groupId}.${artifactId}.mypackage</defaultValue>
        </requiredProperty>
    </requiredProperties>

</archetype-descriptor>

Note the new requiredProperties section: here we are setting the default value for the package property, no need to provide it at runtime anymore (yet possible to override the value above though).

As such, the Java source template under src/main/java (no need for further static folders) would simply be:

package ${package}

During the creation (archetype:generate) Maven will then use the com.sample.something.mypackage as package value (in the Java source file) and populate the packageInPathFormat property with the value com/sample/something/mypackage (the same property, but in path format) and create the desired package hierarchy, consistent with what the Java source code would expect to be placed in.

这篇关于在Maven原型中更改包属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆