java – 如何在IntelliJ的文件模板中转义maven属性

我正在尝试在JetBrains IntelliJ IDEA 12中为我的Maven项目创建一个模板.

我的目标是在模板中转义预定义的maven属性.不幸的是,语法与模板中的IntelliJ参数相同.

根据online help,我可以使用前面的另一个$转义$符号,所以我的模板看起来像这样(底部的插件部分很重要):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    #if (${HAS_PARENT})
    <parent>
        <groupId>${PARENT_GROUP_ID}</groupId>
        <artifactId>${PARENT_ARTIFACT_ID}</artifactId>
        <version>${PARENT_VERSION}</version>
        #if (${HAS_RELATIVE_PATH})
        <relativePath>${PARENT_RELATIVE_PATH}</relativePath>
        #end
    </parent>
    #end

    <groupId>${GROUP_ID}</groupId>
    <artifactId>${ARTIFACT_ID}</artifactId>
    <version>${VERSION}</version>

    <!-- global properties -->
    <properties>
        <jdk.version>1.7</jdk.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <!-- set jdk version -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>$${jdk.version}</source>
                    <target>$${jdk.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    ${END}
</project>

但是使用此模板,输出仍然是:

<build>
    <plugins>
        <!-- set jdk version -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>$JDK.VERSION$</source>
                <target>$JDK.VERSION$</target>
            </configuration>
        </plugin>
    </plugins>
</build>

所以我的问题是:如何获得${jdk.version}而不是$JDK.VERSION $以及什么是逃避String的正确方法?

解决方法:

由于IntelliJ使用Apache Velocity作为模板引擎,因此您可以使用#set创建引用.

http://velocity.apache.org/engine/releases/velocity-1.6.4/user-guide.html

使用可以使用

#set( $jdk_version = "${jdk.version}" )

在模板中,只需引用${jdk_version}

你可以测试以下示例:

#set( $jdk_version = "${jdk.version}" )
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    #if (${HAS_PARENT})
    <parent>
        <groupId>${PARENT_GROUP_ID}</groupId>
        <artifactId>${PARENT_ARTIFACT_ID}</artifactId>
        <version>${PARENT_VERSION}</version>
        #if (${HAS_RELATIVE_PATH})
        <relativePath>${PARENT_RELATIVE_PATH}</relativePath>
        #end
    </parent>
    #end

    <groupId>${GROUP_ID}</groupId>
    <artifactId>${ARTIFACT_ID}</artifactId>
    <version>${VERSION}</version>

    <!-- global properties -->
    <properties>
        <jdk.version>1.7</jdk.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <!-- set jdk version -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>${jdk_version}</source>
                    <target>${jdk_version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    ${END}
</project>

那是产生以下输出:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>3</groupId>
        <artifactId>4</artifactId>
        <version>5</version>
        <relativePath>7</relativePath>
    </parent>

    <groupId>8</groupId>
    <artifactId>9</artifactId>
    <version>10</version>

    <!-- global properties -->
    <properties>
        <jdk.version>1.7</jdk.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <!-- set jdk version -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    11
</project>

我希望这对你有所帮助,也是你问题的答案……

上一篇:在Javascript中包含换行符的Unescape HTML实体?


下一篇:PHP:linux的转义文件名