Avoiding maven properties in IntelliJ file templates

I am trying to create a template for my Maven projects in JetBrains IntelliJ IDEA 12.

My goal is to avoid the predefined maven property inside the template. Unfortunately, the syntax is the same as the IntelliJ options in the template.

According to online help, I can avoid the $ character with another $ in front of it, so my template looks like this (the important part of the plugin below):

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

But with this template, the output is all the same:

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

So my question is: how can I get ${jdk.version} instead of $JDK.VERSION$ and what is the correct way to avoid a string?

+4
source share
1 answer

Since IntelliJ uses Apache Velocity as a template engine, you can use #set to create links.

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

use can use

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

and inside your template just link to $ {jdk_version}

can you check the following example:

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

which generated the following output:

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

I hope this helps you and will be the answer to your question ...

+3
source

Source: https://habr.com/ru/post/1498575/


All Articles