0

When using Maven to run test for MariaDB Connector J, you can specify dbUrl using dbUrl option like follows:

mvn -DdbUrl="jdbc:mysql://myhost:3306/testj?user=myuser&password=mypassword" package

My question is how to pass the dbUrl value dynamically when running from shell, i.e. something like this:

export myUrl="abcd"
mvn -DdbUrl=$myUrl package

But this does not work. May I ask is there a way to pass shell variables when using mvn command?

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
Sky
  • 11
  • 5
  • See also [How to retrieve maven properties inside a JUnit test?](https://stackoverflow.com/q/247346/1744774) and [How to read Maven properties from JUnit test?](https://stackoverflow.com/q/7903621/1744774). – Gerold Broser Apr 09 '19 at 13:23

2 Answers2

0

If you define a property on the Maven command line with -Dname=value it is meant to be used inside your project's POM. (BTW, you can also access environment variables directly there by using ${env.<variable name>}).

To pass system properties to your test code you can use the Maven Surefire Plugin test goal's parameter <systemPropertyVariables>:

<systemPropertyVariables> List of System properties to pass to the JUnit tests.

according to the examples on Using System Properties:

  ...  
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M3</version>
    <configuration>
      <systemPropertyVariables>
        <dbUrl>${dbUrl}</dbUrl>  <!-- if defined on Maven cmd line -->
        <!-- <dbUrl>${env.myUrl}</dbUrl>  if defined as env variable -->
      </systemPropertyVariables>
    </configuration>
  </plugin>
  ...

Use System.getProperty("dbUrl") in your code then.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • Hi Gerold, thanks a lot for the detailed explanation for the system properties usage. However, my case is kind of special, I cannot modify pom file or the test code. I have found a way to workaround this by make up the whole mvn command string first, then run it with sh. May I ask is this the only way if I cannot modify the pom file or test code? Thanks a lot. – Sky Apr 10 '19 at 07:08
0

Thanks a lot for Gerold Broser for the detailed explanation for the system properties usage. However, my case is kind of special, I cannot modify pom file or the test code. I have found a way to workaround this by make up the whole mvn command string first by something like string concat, then run it with sh.

Sky
  • 11
  • 5