maven - 更改 Maven Archetype 中的包属性

标签 maven maven-3 maven-archetype

我创建了一个 Maven 原型(prototype)。我的 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

问题:我该如何实现这一目标?

最佳答案

为了实现您的目标,并且由于您已经在使用 packaged属性为true (稍后解释),您只需将目录添加到下面的路径中即可。

保持相同的配置,并添加一个 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 源模板化,其中包语句如下:

package ${package}.mypackage

注意 .mypackage准确反射(reflect)了mypackage直接位于 src/main/java 下的文件夹。但是,在创建原型(prototype)时,Maven 将作为文件夹(以及包)放置在 ${package} 属性值之间,默认情况下为 ${groupId}。

您随时可以通过-Dpackage属性并覆盖它的默认值( groupId ),然后根据上面的模板将其用作包的前缀。

发生这种情况是因为 packaged属性设置为 truefileSet上面的部分。在这种情况下true意思是:向其中添加${package}指定的文件夹层次结构。属性(property)。将其设置为 false将导致 ${package}忽略,如果您确实想对文件夹结构进行硬编码并明显地将其反射(reflect)到 package 中,则可以使用它。您的 Java 代码的声明,以保持一致性。

<小时/>

以上行为记录在官方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.

还有:

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.

相同的详细信息(关于 packaged 属性)也可以在官方 Archetype Descriptor Model 中找到.

<小时/>

另一种可能的解决方案是使用附加属性或定义您的 package属性值直接在 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>

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

</archetype-descriptor>

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

因此,src/main/java 下的 Java 源模板(不需要更多的静态文件夹)就是:

package ${package}

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

关于maven - 更改 Maven Archetype 中的包属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39080569/

相关文章:

Git for Windows(64 位)中的 Maven classworlds.launcher.Launcher 错误

java - Maven Archetype 上的 charsetName 引起的 MojoFailureException

java - 使用 servlet 和 Jetty 时丢失图像

java - 使用 Apache Tomcat 将上传的静态文件存储在何处

java - 请求处理失败;嵌套异常是 org.springframework.dao.DataIntegrityViolationException : could not execute statement;

java - Maven 原型(prototype) : org. apache.maven.plugins :maven-surefire-plugin:2. 20.1 :test failed. : NullPointerException

java - 创建一个 Maven 原型(prototype)以从一个类模板生成多个类

Maven 存储库继承和覆盖

java - jetty-maven-plugin 不适用于 Maven 3

maven-3 - "default-test"在 maven-surefire 插件中代表什么