Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify system property to Maven project

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 )

like image 415
OscarRyz Avatar asked Jul 12 '10 19:07

OscarRyz


People also ask

How do you define system properties?

The System class maintains a Properties object that describes the configuration of the current working environment. System properties include information about the current user, the current version of the Java runtime, and the character used to separate components of a file path name.

How do I set system properties in Junit?

If your test relies on system properties you could set them and unset them in 'before' and 'after' lifecycle methods. In Junit5, setting system properties for all tests in a test case might look like this: @BeforeAll public static void setSystemProperties() { // set the system properties // ... }

How do I set system properties in Linux?

Go to the Registry Editor (Start > regedit.exe). To change existing properties double-click the appropriate value. To change additional properties, double-click options. Refer to the list of parameters in Recognized System Properties.

How do I set custom system properties in Java?

Programmatically, a system property can be set using the setProperty method of the System object, and also via the setProperty method of the Properties object that can be obtained from System via getProperties.


2 Answers

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> 
like image 154
Pascal Thivent Avatar answered Sep 23 '22 15:09

Pascal Thivent


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> 
like image 42
yegor256 Avatar answered Sep 23 '22 15:09

yegor256