2013-08-23 38 views
4

我想在JetBrains的IntelliJ IDEA的我的Maven項目創建一個模板12HOWTO逃生行家性能

我的目標是爲了躲避模板中預定義的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$,什麼是逃串的正確方法?

+0

我不認爲這是一個偉大的想法。 Maven提供了一個稱爲原型的功能:https://maven.apache.org/guides/introduction/introduction-to-archetypes.html –

+0

我知道Archetypes可用並提供更多的靈活性,但我也希望能夠擁有Intellij生成更好的pom。 –

回答

3

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

那個wa的發電輸出如下:

<?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> 

我希望這可以幫助你,是你的問題的答案......