How to read Maven properties from JUnit test?
Asked Answered
J

3

12

I'm using Maven 3.0.3 with JUnit 4.8.1. In my JUnit test, how do I read the project.artifactId defined in my Maven pom.xml file? In my pom, I have

<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.myco.pplus2</groupId>
<artifactId>pplus2</artifactId>

But this isn't working within my JUnit test to gete the artifact id ...

@Before
public void setUp() { 
    ...        
    System.out.println( "artifactId:" + System.getProperty("project.build.sourceEncoding") ); 
}   // setUp

The above outputs "artifactId:null". Anyway, appreciate any help, - Dave

Junejuneau answered 26/10, 2011 at 13:53 Comment(1)
Possible duplicate of How to retrieve maven properties inside a JUnit test?Herrah
G
8

Look at the systemPropertyVariables (and friends) for surefire. It does what you want. AFAIK there is no way to just pass all the maven properties without listing them.

Granada answered 26/10, 2011 at 19:53 Comment(0)
L
12

Maven project properties aren't automatically added to Java System properties. To achieve that there are quite a few options. For this specific need you could define a System property for maven-surefire-plugin (the one running tests) and then use the System.getProperty method.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.10</version>
    <configuration>
        <systemProperties>
            <property>
                <name>projectArtifactId</name>
                <value>${project.artifactId}</value>
            </property>
        </systemProperties>
    </configuration>
</plugin>

Other way to achieve getting Maven properties to JUnit tests would probably be resources filtering for test source files.

PS. Reading Maven configurations at runtime, even in tests is pretty dirty IMHO. :)

Lavalley answered 26/10, 2011 at 20:30 Comment(1)
use systemPropertyVariables instead of systemProperties (deprecated)Neoptolemus
G
8

Look at the systemPropertyVariables (and friends) for surefire. It does what you want. AFAIK there is no way to just pass all the maven properties without listing them.

Granada answered 26/10, 2011 at 19:53 Comment(0)
B
0

Sometimes, Eclipse is configured to use the Java Builder for Project->Automatically Build (Right Click->Project->Properties->Builders)

If such is the case, sometimes the resource filtering doesn't work. You have several options:

  1. Provide the property in the pom.xml file as above.
  2. Provide a properties file and perform Maven resource filtering
  3. Use the Maven Invoker

2 and 3 are described in http://scottizu.wordpress.com/2013/10/16/reading-the-project-version-from-the-maven-pom-file/

Berserk answered 16/10, 2013 at 18:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.