Specify system property to Maven project
Asked Answered
V

4

72

Is there a way ( I mean how do I ) set a system property in a maven project?

I want to access a property from my test and my webapp ( running locally ) and I know I can use a java system property.

Should I put that in ./settings.xml or something like that?

Context

I took an open source project and managed to change the db configuration to use JavaDB

Now, in the jdbc url for JavaDB, the location of the database could be specified as the full path ( see: this other question )

Or a system property: derby.system.home

I have the code working already, but currently it is all hardcoded to:

 jdbc:derby:/Users/oscarreyes/javadbs/mydb

And I want to remove the full path, to leave it like:

 jdbc:derby:mydb

To do that I need to specify the system property ( derby.system.home ) to maven, but I don't know how.

The test are performed using junit ( I don't see any plugin in pom.xml ) and the web app runs with the jetty plugin.

Specifying the system property in the command line seems to work for jetty, but I'm not sure if that's something practical ( granted some other users may run it from eclipse/idea/ whatever )

Vala answered 12/7, 2010 at 19:57 Comment(1)
I had exactly the same problem, when I moved my tests to integration tests (just renamed them and added maven-failsafe-plugin. This system property stopped working. I also had to move systemPropertyVariables from maven-surefire-plugin into this maven-failsafe-plugin. System property, configured in maven-surefire-plugin were ignored with integration tests.Ximenes
F
89

Is there a way ( I mean how do I ) set a system property in a maven project? I want to access a property from my test [...]

You can set system properties in the Maven Surefire Plugin configuration (this makes sense since tests are forked by default). From Using System Properties:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.5</version>
        <configuration>
          <systemPropertyVariables>
            <propertyName>propertyValue</propertyName>
            <buildDirectory>${project.build.directory}</buildDirectory>
            [...]
          </systemPropertyVariables>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

and my webapp ( running locally )

Not sure what you mean here but I'll assume the webapp container is started by Maven. You can pass system properties on the command line using:

mvn -DargLine="-DpropertyName=propertyValue"

Update: Ok, got it now. For Jetty, you should also be able to set system properties in the Maven Jetty Plugin configuration. From Setting System Properties:

<project>
  ...
  <plugins>
    ...
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
        <configuration>
         ...
         <systemProperties>
            <systemProperty>
              <name>propertyName</name>
              <value>propertyValue</value>
            </systemProperty>
            ...
         </systemProperties>
        </configuration>
      </plugin>
  </plugins>
</project>
Fong answered 12/7, 2010 at 20:26 Comment(4)
I was able to specify that on the command line but I don't really know if that's the best way. I'll update my original question. In the mean time +1Vala
@Oscar I see now. As I wrote, JUnit tests run in a forked VM so you really have to set system properties in the surefire configuration. And the Maven Jetty Plugin provides similar configuration facilities. I suggest to configure both, this is a much better option than the command line indeed.Fong
I thought everyone could read minds as I do... ( btw stop thinking that!!! ) ... That worked perfectly. Thank you !!Vala
2019 note: Depreciated. Use systemPropertyVariables instead of systemProperties maven.apache.org/plugins-archives/maven-surefire-plugin-2.12.4/…Dissimulation
N
79

properties-maven-plugin plugin may help:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0.0</version>
    <executions>
        <execution>
            <goals>
                <goal>set-system-properties</goal>
            </goals>
            <configuration>
                <properties>
                    <property>
                        <name>my.property.name</name>
                        <value>my property value</value>
                    </property>
                </properties>
            </configuration>
        </execution>
    </executions>
</plugin>
Nationwide answered 31/10, 2012 at 13:24 Comment(3)
I don't understand why maven-surefire-plugin is mentioned for things that are not test related. It seems like it's a completely separate thing from normal development/deployment, and I don't understand the conflation.Corselet
Most of the people think that maven has testing purpose only.Armillary
version 1.2.1 now - mvnrepository.com/artifact/org.codehaus.mojo/…Indigence
H
11

I have learned it is also possible to do this with the exec-maven-plugin if you're doing a "standalone" java app.

            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>${maven.exec.plugin.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>${exec.main-class}</mainClass>
                <systemProperties>
                    <systemProperty>
                        <key>myproperty</key>
                        <value>myvalue</value>
                    </systemProperty>
                </systemProperties>
            </configuration>
        </plugin>
Hellebore answered 28/12, 2013 at 1:25 Comment(0)
W
2

If your test and webapp are in the same Maven project, you can use a property in the project POM. Then you can filter certain files which will allow Maven to set the property in those files. There are different ways to filter, but the most common is during the resources phase - http://books.sonatype.com/mvnref-book/reference/resource-filtering-sect-description.html

If the test and webapp are in different Maven projects, you can put the property in settings.xml, which is in your maven repository folder (C:\Documents and Settings\username.m2) on Windows. You will still need to use filtering or some other method to read the property into your test and webapp.

Worldlywise answered 12/7, 2010 at 20:5 Comment(1)
I actually need it as a System property so I can perform System.getProperty("myproperty");Vala

© 2022 - 2024 — McMap. All rights reserved.