How to refer environment variable in POM.xml?
Check out the Maven Properties Guide...
As Seshagiri pointed out in the comments, ${env.VARIABLE_NAME}
will do what you want.
I will add a word of warning and say that a pom.xml
should completely describe your project so please use environment variables judiciously. If you make your builds dependent on your environment, they are harder to reproduce
Maven read environment variable in properties file
You should probably first filter the properties file with the maven resources plugin. Afterwards myBatis plugin should work as intended.
See following gist for a short example to the resources plugin.
development.properties
# place in src/main/resources
username=${env.username}
password=${env.password}
pom.xml
<?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>
<groupId>com.stackoverflow</groupId>
<artifactId>question-48693420</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
Command line:
username=user1 password=pass1 mvn resources:resources && cat target/classes/development.properties
Console log:
(...)
[INFO] --- maven-resources-plugin:2.3:resources (default-cli) @ question-48693420 ---
(...)
[INFO] Copying 1 resource
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
(...)
username=user1
password=pass1
Intellij not recognising environment variable in pom.xml
Environment Variables can be referred in pom.xml using 'env.' prefix.
Please refer to Maven properties
For example,
env.PATH
Contains the current PATH in which Maven is running. The PATH contains a list of directories used to locate executable scripts and programs.
pom.xml environment variable with default fallback
You could use profiles to achieve this:
<profiles>
<profile>
<id>buildnumber-defined</id>
<activation>
<property>
<name>env.BUILD_NUMBER</name>
</property>
</activation>
<properties>
<buildnumber>${env.BUILD_NUMBER}</buildnumber>
</properties>
</profile>
<profile>
<id>buildnumber-undefined</id>
<activation>
<property>
<name>!env.BUILD_NUMBER</name>
</property>
</activation>
<properties>
<buildnumber>0</buildnumber>
</properties>
</profile>
</profiles>
A bit more verbose than bash...
How to conditionally select a specific directory based on environment pom.xml
<profile>
<id>prod</id>
<activation>
<property>
<name>env</name>
<value>prod</value>
</property>
</activation>
</profile>
This actually works if you set your env variable either via Explicitly using command console input or Through maven settings.
So this profile will be activated depending upon the env variable specified. We can also use !Prod, to select all non-prod environments.
Pom file not reading environment variables
Try adding this profile to your pom.xml
(in <profiles/>
):
<profile>
<id>tools.jar</id>
<activation>
<file><exists>${java.home}/../lib/tools.jar</exists></file>
</activation>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>jdk</groupId>
<artifactId>tools</artifactId>
<version>${java.specification.version}</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<dependencies>
<dependency>
<groupId>jdk</groupId>
<artifactId>tools</artifactId>
<version>${java.specification.version}</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<docletPath>${tools.jar}</docletPath>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
I've included the corresponding change to the javadoc plug-in which may or
may not be useful to you. You can remove it if you don't need it.
Related Topics
How to Configure Encoding in Maven
What Is the Convention for Word Separator in Java Package Names
When to Close Connection, Statement, Preparedstatement and Resultset in Jdbc
How to Compile a Java File with a Different Name Than the Class
Java Webservice Client (Best Way)
Difference Between Break and Continue Statement
How to Get Information About the Local Variables Using Java Reflection
Jfreechart Line Chart with Text at Each Point
Does Java Have Built in Libraries for Audio _Synthesis_
Efficient Swapping of Elements of an Array in Java
Is Java.Util.Date Using Timezone
What Is Difference Between Crudrepository and JPArepository Interfaces in Spring Data JPA
What Are the Differences Between Arraylist and Vector
How to Get Data Between Quotes in Java
Creating Java Date Object from Year,Month,Day