Is it possible within maven to set a system property that is attainable from within a java class.
I have seen that this is possible (here) within the surefire plugin as follows;
String param = System.getProperty("my_parameter1");  <configuration>     <systemPropertyVariables>         <my_property1>${my_property1}</my_property1>     </systemPropertyVariables> </configuration>   However I would like to get a handle on the environment I am working in, I am already passing prod or dev as a maven profile argument - is it possible somehow to get a handle in the code on this either from setting a variable in the profile i call and then calling system.getProperty or some other way?
Thanks
my pom file
<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>core</groupId>     <artifactId>core</artifactId>     <version>1.0</version>     <packaging>jar</packaging>     <build>         <sourceDirectory>src</sourceDirectory>         <testSourceDirectory>test</testSourceDirectory>         <plugins>             <plugin>                 <artifactId>maven-compiler-plugin</artifactId>                 <version>3.1</version>                 <configuration>                     <source>1.7</source>                     <target>1.7</target>                 </configuration>             </plugin>             <plugin>                 <artifactId>maven-jar-plugin</artifactId>                 <version>2.3</version>                 <configuration>                     <warSourceDirectory>WebContent</warSourceDirectory>                     <failOnMissingWebXml>false</failOnMissingWebXml>                 </configuration>             </plugin>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-surefire-plugin</artifactId>                 <version>2.17</version>                 <configuration>                     <parallel>methods</parallel>                     <threadCount>10</threadCount>                 </configuration>             </plugin>             <plugin>                 <groupId>org.codehaus.mojo</groupId>                 <artifactId>sonar-maven-plugin</artifactId>                 <version>2.1</version>             </plugin>         </plugins>         <resources>             <resource>                 <directory>src</directory>                 <excludes>                     <exclude>**/*.java</exclude>                 </excludes>             </resource>             <resource>                 <directory>resources</directory>                 <includes>                     <include>**/*.png</include>                 </includes>             </resource>         </resources>     </build>     <dependencies>         <dependency>             <groupId>commons-dbcp</groupId>             <artifactId>commons-dbcp</artifactId>             <version>1.4</version>         </dependency>         <dependency>             <groupId>commons-lang</groupId>             <artifactId>commons-lang</artifactId>             <version>2.3</version>         </dependency>         <dependency>             <groupId>commons-codec</groupId>             <artifactId>commons-codec</artifactId>             <version>1.9</version>         </dependency>         <dependency>             <groupId>com.fasterxml.jackson.core</groupId>             <artifactId>jackson-core</artifactId>             <version>2.3.0</version>         </dependency>         <dependency>             <groupId>com.fasterxml.jackson.core</groupId>             <artifactId>jackson-annotations</artifactId>             <version>2.3.0</version>         </dependency>         <dependency>             <groupId>com.fasterxml.jackson.core</groupId>             <artifactId>jackson-databind</artifactId>             <version>2.3.0</version>         </dependency>         <dependency>             <groupId>org.mariadb.jdbc</groupId>             <artifactId>mariadb-java-client</artifactId>             <version>1.1.7</version>         </dependency>         <dependency>             <groupId>junit</groupId>             <artifactId>junit</artifactId>             <version>4.11</version>         </dependency>         <dependency>             <groupId>org.mockito</groupId>             <artifactId>mockito-all</artifactId>             <version>1.9.5</version>             <scope>test</scope>         </dependency>         <dependency>             <groupId>org.facebook4j</groupId>             <artifactId>facebook4j-core</artifactId>             <version>[2.0,)</version>         </dependency>         <dependency>             <groupId>com.relayrides</groupId>             <artifactId>pushy</artifactId>             <version>0.3</version>         </dependency>         <dependency>             <groupId>log4j</groupId>             <artifactId>log4j</artifactId>             <version>1.2.16</version>         </dependency>         <dependency>             <groupId>com.sun.mail</groupId>             <artifactId>javax.mail</artifactId>             <version>1.5.2</version>         </dependency>         <dependency>             <groupId>org.hibernate</groupId>             <artifactId>hibernate-core</artifactId>             <version>4.3.6.Final</version>         </dependency>         <dependency>             <groupId>javax.mail</groupId>             <artifactId>javax.mail-api</artifactId>             <version>1.5.2</version>             <scope>runtime</scope>         </dependency>         <dependency>             <groupId>javax.activation</groupId>             <artifactId>activation</artifactId>             <version>1.1.1</version>         </dependency>         <dependency>             <groupId>com.threewks.thundr</groupId>             <artifactId>thundr-mailgun</artifactId>             <version>1.1.0</version>         </dependency>         <dependency>             <groupId>org.apache.commons</groupId>             <artifactId>commons-io</artifactId>             <version>1.3.2</version>         </dependency>         <dependency>             <groupId>com.google.code.gson</groupId>             <artifactId>gson</artifactId>             <version>2.3</version>         </dependency>     </dependencies>     <profiles>         <profile>             <id>DEV</id>             <properties>                 <swifte.url>jdbc:mariadb://ip:3306/swifte?autoReconnect=true</swifte.url>                 <swifte.username>user</swifte.username>                 <swifte.password>pass</swifte.password>             </properties>             <build>                 <resources>                     <resource>                         <directory>resources</directory>                         <includes>                             <include>JavaPNSDev.p12</include>                         </includes>                     </resource>                 </resources>             </build>         </profile>         <profile>             <id>PROD</id>             <properties>                 <swifte.url>jdbc:mariadb://ip:3306/swifte?autoReconnect=true</swifte.url>                 <swifte.username>username</swifte.username>                 <swifte.password>pass</swifte.password>             </properties>             <build>                 <resources>                     <resource>                         <directory>resources</directory>                         <includes>                             <include>JavaPNSProd.p12</include>                         </includes>                     </resource>                 </resources>             </build>         </profile>     </profiles> </project> 
                To provide System Properties to the tests from command line, you just need to configure maven surefire plugin and use -D{systemproperty}={propertyvalue} parameter in commandline. Run Single Test with Maven : $ mvn test -Dtest=MessageUtilTest#msg_add_test -Dmy_message="Hello, Developer!"
Set the environment variables using system properties. Open command terminal and set environment variables. Open command terminal and set environment variables.
To get a specific system property you can use System. getProperty(String key) or System. getProperty(String key, String def) . Environment variables are set in the OS, e.g. in Linux export HOME=/Users/myusername or on Windows SET WINDIR=C:\Windows etc, and, unlike properties, may not be set at runtime.
Open Maven settings. m2 directory where %USER_HOME% represents the user home directory. If settings. xml file is not there, then create a new one. Add test profile as an active profile using active Profiles node as shown below in example.
You should check out the exec-maven-plugin.
With the following configuration (notice the <systemProperties>)...
<plugin>     <groupId>org.codehaus.mojo</groupId>     <artifactId>exec-maven-plugin</artifactId>     <version>1.3.2</version>     <executions>         <execution>             <phase>package</phase>             <goals>                 <goal>java</goal>             </goals>         </execution>     </executions>     <configuration>         <mainClass>com.example.Main</mainClass>         <arguments>             <argument>argument1</argument>         </arguments>         <systemProperties>             <systemProperty>                 <key>hello.world</key>                 <value>Hello Stack Overflow!</value>             </systemProperty>         </systemProperties>     </configuration> </plugin>   ...and the following class...
package com.example; public class Main {     public static void main(String[] args) {         String prop = System.getProperty("hello.world");         System.out.println(prop);     } }   ...and running a package (notice the phase in the configuration - you can change if you want, maybe to install), it prints out the value Hello Stack Overflow! from the key hello.world. So basically, the plugin executes your program when you build.
See also the exec:exec goal. In the example, I used the exec:java goal, but the two are different in how they function.
exec:execexecutes programs and Java programs in a separate process.
exec:javaexecutes Java programs in the same VM.
UPDATE
Currently I am setting some values in properties based on the profile in my maven pom file. Is it possible to set this system property in the profile ? because really, i only have one pom file for dev and prod and its within the profile i would need to set it.
Yes, just use the ${property.name} in the <value> element of the system property element. For example:
<profiles>     <profile>         <activation>             <activeByDefault>true</activeByDefault>         </activation>         <id>world</id>         <properties>             <hello.world>Hello World!</hello.world>         </properties>     </profile>     <profile>         <activation>             <activeByDefault>false</activeByDefault>         </activation>         <id>stack</id>         <properties>             <hello.world>Hello Stack Overflow</hello.world>         </properties>     </profile> </profiles>   And the plugin <systemProperties>:
<systemProperties>     <systemProperty>         <key>hello.world</key>         <value>${hello.world}</value>     </systemProperty> </systemProperties>   Just by changing the profile, to either stack or world, the message will print Hello Stack Overflow or Hello World, respectively.
UPDATE 2
Another plugin is the properties-maven-plugin. Nothing's been done on it in a while, but from a few tests, the necessary functionality is there.
It has a set-system-properties goal along with some other useful goals to help ease properties management
<plugin>     <groupId>org.codehaus.mojo</groupId>     <artifactId>properties-maven-plugin</artifactId>     <version>1.0-alpha-2</version>     <executions>         <execution>             <!-- any phase before your app deploys -->             <phase>prepare-package</phase>             <goals>                 <goal>set-system-properties</goal>             </goals>             <configuration>                 <properties>                     <property>                         <name>hello.world.two</name>                         <value>Hello World!</value>                     </property>                 </properties>             </configuration>         </execution>     </executions> </plugin> 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With