简体   繁体   English

如何在Maven原型XML中调用Java方法

[英]How to call Java methods in Maven archetype XML

I am trying to install a custom archetype in my local repo. 我正在尝试在本地存储库中安装自定义原型。 When user generate a project based on this archetype, they are required to provide the artifactId. 当用户基于此原型生成项目时,要求他们提供artifactId。 Most of the time it is in all lower case. 在大多数情况下,它都是小写的。 However, the main Class name (also the java filename) is dependent on this artifactId with the first letter capitalized. 但是,主要的类名(也就是Java文件名)取决于此artifactId,且首字母大写。 Instead of asking user to input another variable, I would like to call some String method to convert the artifactId to the correct format for class name. 我不要求用户输入另一个变量,而是要调用一些String方法来将artifactId转换为类名的正确格式。

In Maven archetype: Modify artifactId , looks like you can embed Java method in archetype-metadata.xml as below: Maven原型中:修改artifactId ,看起来您可以将Java方法嵌入archetype-metadata.xml中,如下所示:

<requiredProperty key="artifactIdWithUnderscore" >
  <defaultValue>${artifactId.replaceAll("-", "_")}</defaultValue>
</requiredProperty>

So I did something similar in my archetype-metadata.xml to capitalize first letter. 因此,我在archetype-metadata.xml中做了类似的操作以大写第一个字母。

<requiredProperty key="artifactId" />
<requiredProperty key="serviceName">
   <defaultValue>${artifactId.toLowerCase().substring(0,1).toUpperCase()+actifactId.toLowerCase().substring(1)}</defaultValue>
</requiredProperty>

However I got the following parse error: 但是我得到以下解析错误:

SEVERE: Parser Exception: serviceName
org.apache.velocity.runtime.parser.ParseException: Encountered "+artifactId.toLowerCase().substring(1)}" at line 1, column 55.
Was expecting one of:
    "[" ...
    "}" ...

    at org.apache.velocity.runtime.parser.Parser.generateParseException(Parser.java:3679)

What is correct way to insert Java String method in this archetype xml? 在此原型xml中插入Java String方法的正确方法是什么?

This will capitalize the first letter. 这将大写一个字母。

  <requiredProperty key="artifactId" />
    <requiredProperty key="serviceName">
       <defaultValue>${StringUtils.capitalize("artifactId")}</defaultValue>
    </requiredProperty>

Make sure you have Apache commons dependency included. 确保您包括Apache Commons依赖项。

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.5</version>
</dependency>

The plus character can't be used as string concatenation operator here, it's also not needed. 加号字符在此处不能用作字符串连接运算符,也不需要。 Just concatenate two replacements (without any operator between): 只需串联两个替换项(之间没有任何运算符):

<defaultValue>${artifactId.toLowerCase().substring(0,1).toUpperCase()}${actifactId.toLowerCase().substring(1)}</defaultValue>
<defaultValue>
    ${artifactId.substring(0,1).toUpperCase()}${artifactId.substring(1).toLowerCase()}
</defaultValue>

works for me, but artifactId is coming from the archetype project and not from my argument artifactId as expected... 为我工作,但artifactId来自原型项目,而不是预期的来自我的参数artifactId ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM