2013-08-23 108 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> 

我希望这可以帮助你,是你的问题的答案......