0

I setup a basic maven Java project in Netbeans. I made a new property in the POM.xml file and tried to read it in my code. But, I keep getting a null instead of the value. What am I doing wrong?

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>
    <parent>
        <groupId>com.test</groupId>
        <artifactId>Test</artifactId>
        <version>1.0</version>
    </parent>
    <artifactId>Test</artifactId>
    <packaging>jar</packaging>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <system>ABC</system>
    </properties>
</project>

Code :

package com.test.Test;
public class Test {
    public static void main(String[] args) {
        System.out.println("Creating connection to " + System.getProperty("system") + "...");
    }
}
JackSlayer94
  • 805
  • 3
  • 16
  • 38
  • 2
    Because they are maven properties, not system properties. They are valid only during a maven build, not at runtime in your program. – Seelenvirtuose May 27 '17 at 06:49
  • 1
    Why do you need to read them from Maven properties? Should they be used during the test or during the run time of your application? – khmarbaise May 27 '17 at 10:06

2 Answers2

2

It's unrelated to Maven.

You should set it as JVM arguments. Then you will be able to read them as System.getProperty().

In Eclipse, for example, you'll find it in the menu here:

Run --> Run Configurations... --> Arguments --> VM arguments --> there you add your properties (don't forget to apply your changes):

-Dsystem=ABC

will cause System.getProperty("system") to return ABC.

SHG
  • 2,516
  • 1
  • 14
  • 20
0

I think that the only way how you can do that is to such properties during project start using the following command:

-Dsystem=...
Alex
  • 1,940
  • 2
  • 18
  • 36
  • Is there a way this can be done from reading maven properties? – JackSlayer94 May 27 '17 at 08:46
  • You know, I'm not really aware of such opportunities and I would ask another question - why do you need to do that in this exact way? So if there are any properties which you need to use in your application then it would make sense to move them in some property file to have direct access. Maven properties are only used for some build time information. If you provide some real use case for that probably we will be able to find better way how to handle that. – Alex May 27 '17 at 09:03
  • Yes I want to use a build property in my runtime as well. Is there a possibility of this? – JackSlayer94 Jun 02 '17 at 14:01
  • I would suggest you check another approach - move your properties to property file and then use property file to load properties in maven using this approach - https://stackoverflow.com/questions/12619446/can-i-use-property-file-in-maven-pom-xml-for-flyway-configuration. – Alex Jun 02 '17 at 17:54