java - 如何转义 IntelliJ 文件模板中的 Maven 属性

标签 java templates maven intellij-idea escaping

我正在尝试在 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$ 以及转义字符串的正确方法是什么?

最佳答案

由于 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>

我希望这对您有所帮助,并且是您问题的答案...

关于java - 如何转义 IntelliJ 文件模板中的 Maven 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18401215/

相关文章:

java - 在创建可执行 jar 时,如何引用本地文件并将其包含在我的 Maven 程序集中?

c++ - 模板类给出链接器错误

templates - Mandrill 模板中的条件内容

maven - Spring web-flow 与 JSF ClassNotFoundException 问题

maven - nexus-staging-maven-plugin 仅上传明确设置为不上传的 Artifact

c++ - 具有类类型非类型模板参数的类模板成员的类外定义

java - 无法运行简单的JavaFX程序

java - 如何使用 JSqlParser 添加新条件?

java - 用于检查 xml 是否包含键和值的 XPath 表达式

java - 更新JComboBox后,如何刷新box的长度