Maven archetype - Velocity error because of a colon

蹲街弑〆低调 提交于 2019-12-01 18:19:42

问题


I am having trouble creating a Maven archetype because of the presence of a colon character (':') in one of the resources. I have a Spring XML that includes that symbol:

<property name="maxSize" value="${ws.client.pool.maxSize:5}"/>

When launching the archetype I get this error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.2:generate 
(default-cli) on project standalone-pom: 
org.apache.maven.archetype.exception.ArchetypeGenerationFailure: Error merging velocity templates: 
Encountered ":5}\"/>\n\t</bean>\n\t\n\t<bean id=\"fooServiceClient\" class=\"org.springframework.aop.framework.ProxyFactoryBean\">\n\t    <property name=\"targetSource\" ref=\"fooServiceClientPoolTargetSource\"/>\n\t</bean>\n\n</beans>\n" 
at line 15, column 69 of archetype-resources/src/main/resources/spring/library.ws-client.xml
[ERROR] Was expecting one of:
[ERROR] "}" ...
[ERROR] <DOT> ...
[ERROR] "(" ...
[ERROR] -> [Help 1]
[ERROR] 

I tried configuring a escape character in the archetype's pom:

    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-archetype-plugin</artifactId>
                <version>2.0</version>
            </plugin>
            <!-- Resources configuration -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <escapeString>\</escapeString>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>

However it still doesn't work. In this case:

<property name="maxSize" value="${ws.client.pool.maxSize\:5}"/>

the error is as follows:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2.2:generate 
(default-cli) on project standalone-pom: org.apache.maven.archetype.exception.ArchetypeGenerationFailure: 
Error merging velocity templates: 
Encountered "\\" at line 15, column 69 of archetype-resources/src/main/resources/spring/library.ws-client.xml
[ERROR] Was expecting one of:
[ERROR] "}" ...
[ERROR] <DOT> ...
[ERROR] "(" ...
[ERROR] -> [Help 1]
[ERROR] 

Any idea on how to escape that colon?


回答1:


Had the same error with the following Spring injection code for an optional property:

@Value("${hostname:}")
private String hostName;

The solution:

#set( $dollar = '$' )
@Value("${dollar}{hostname:}")
private String hostName;

The crucial step is to wrap the reference to your $ constant in {curly braces}




回答2:


I worked out a solution based on a Velocity variable ($maxSize):

#set( $maxSize = '${ws.client.pool.maxSize:5}' )

<bean id="fooServiceClientPoolTargetSource" class="org.springframework.aop.target.CommonsPoolTargetSource">
    <property name="targetBeanName" value="fooServiceClientTarget"/>
    <property name="maxSize" value="$maxSize"/>


来源:https://stackoverflow.com/questions/31201338/maven-archetype-velocity-error-because-of-a-colon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!