28

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>
Community
  • 1
  • 1
Biscuit128
  • 5,218
  • 22
  • 89
  • 149

1 Answers1

32

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:exec executes programs and Java programs in a separate process.

exec:java executes 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>
XP1
  • 6,910
  • 8
  • 54
  • 61
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • so i would want to use the exec maven plugin opposed to what i currently use being maven-compiler-plugin? – Biscuit128 Sep 01 '14 at 12:32
  • No they can go together. The exec plugin is just responsible for launching the program. The compiler plugin doesn't do that. – Paul Samsotha Sep 01 '14 at 12:34
  • one final question if thats ok? 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. I will post my pom now – Biscuit128 Sep 01 '14 at 12:35
  • is it necessary to define the main class and arguments parameters? – Biscuit128 Sep 01 '14 at 15:19
  • Arguments, no. But if you don't specify the class, then it won't know the launching point of the program, and the goal will fail. Remember the point of the plugin is to execute a program – Paul Samsotha Sep 01 '14 at 15:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60400/discussion-between-biscuit128-and-peeskillet). – Biscuit128 Sep 01 '14 at 15:29
  • So it looks like the jboss-as-maven-plugin has a [deploy](https://docs.jboss.org/jbossas/7/plugins/maven/latest/deploy-mojo.html) goal you can configure with a [beforeDeployment](https://docs.jboss.org/jbossas/7/plugins/maven/latest/deploy-mojo.html#beforeDeployment) where you can pass command line commands – Paul Samsotha Sep 01 '14 at 16:09
  • I think I found the one you need. Check my latest update. Tested on a webapp and it works as advertised – Paul Samsotha Sep 01 '14 at 17:14
  • and this would be called in the same way i assume? – Biscuit128 Sep 01 '14 at 17:16
  • Not sure what you mean by "called" – Paul Samsotha Sep 01 '14 at 17:17
  • well we set the property in the goal, in the profile we would reference it in the same way and in my java class i would just do system.getproperty(value)? – Biscuit128 Sep 01 '14 at 17:17
  • Yea same way. All the properties get set in the `phase` you specify. As long as it's before the phase your app deploys, it should be fine. Try it out. – Paul Samsotha Sep 01 '14 at 17:18
  • are you still around in chat? – Biscuit128 Sep 01 '14 at 17:33
  • @PaulSamsotha, your answer is good and appreciable. I am also doing the same in my application but when I trying to get the property its giving me null. I have posted my issue in this question https://stackoverflow.com/questions/49934111/unable-to-read-system-property-from-maven-in-java , Can you please help me out? – Devendra Singh Apr 20 '18 at 06:40