Maven archetype - Velocity error because of a colo

2019-02-24 06:36发布

问题:

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"/>