【问题标题】:Howto escape maven properties in IntelliJ's file templates如何在 IntelliJ 文件模板中转义 Maven 属性
【发布时间】:2013-08-23 11:00:24
【问题描述】:

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

【问题讨论】:

  • 我认为这根本不是一个好主意。 Maven 为此提供了一个称为原型的功能:maven.apache.org/guides/introduction/…
  • 我了解 Archetypes 可用并提供更大的灵活性,但我也希望能够让 Intellij 生成更好的 pom。

标签: java templates maven intellij-idea escaping


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 2012-01-31
    • 1970-01-01
    • 2012-05-28
    • 2011-01-25
    • 1970-01-01
    • 2021-09-29
    相关资源
    最近更新 更多