Maven is a blockchain-based marketplace for the sharing economy. It’s a decentralized platform that allows users to monetize their assets and services in a peer-to-peer fashion.
The maven repository is a central location for people to share and distribute software. It uses the Apache Maven software project management tool.
This is a short tutorial on how to use Maven to set System Properties in tests and when running the Java Main class.
Maven allows us to set System Properties in a variety of methods, including using the maven plugin, the command line, or a file.
1. Using the command line
To give System Properties to the tests from the command line, just setup the Maven Plugin that works like a charm and use the commandline option -Dsystemproperty=propertyvalue.
3.0.0-M5 maven-surefire-plugin
Case Study:
@Test void msg add test() public class MessageUtilTest String msg = System.getProperty(“my message”); assertEquals(msg, “Hello, Developer!”);
Using Maven to run a single test:
mvn test -Dtest=MessageUtilTest#msg add test -Dmy message=$ mvn test -Dtest=MessageUtilTest#msg add test -Dmy message=$ mvn test – “Greetings, Developer!” [INFO] ———————————————————————————- [INFO] T E S T S [INFORMATION] ———————————————————————————- [INFO] com.javabydeveloper.util is being used. MessageUtilTest [INFO] Tests run: 1, failures: 0, errors: 0, errors: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, 0.437 seconds elapsed – in com.javabydeveloper.util. MessageUtilTest [INFO] [INFO] Results: [INFO] [INFO] Failures: 0; Errors: 0; Skipped: 0; Tests run: 1; Failures: 0; Errors: 0; Skipped: 0; [INFO] [INFO] ———————————————————————————————————— [INFO] BUILD SUCCESSFULNESS [INFO] ———————————————————————————————————— [INFO] Time taken: 24.123 seconds [INFO] 2021-01-18T11:41:12+08:00 Finished at: 2021-01-18T11:41:12+08:00 [INFO] ————————————————————————————————————
2. Use the Surefire Plugin to configure system settings.
2.1. We may set System properties in surefire plugin configuration using either systemPropertyVariables or systemProperties (systemProperties deprecated in current plugin version) configuration parameters.
3.0.0-M5 maven-surefire-plugin Good day, Developer!
2.2. The -D option may be used to override the System properties specified in the Surefire plugin from the command line.
mvn test -Dtest=MessageUtilTest#msg add test -Dmy message=$ mvn test -Dtest=MessageUtilTest#msg add test -Dmy message=$ mvn test – “Hello, Java Developer!” says the narrator.
2.3. If you have a large number of properties to configure, you may use the systemPropertiesFile option to define System Properties in a properties file, but you must give the file’s location.
maven-surefire-plugin src/test/resources/system props.properties $maven-surefire-plugin.version
The system props.properties file contains the following information:
Hello, my message2=Dear Developer, my message3=Developer!
Case Study:
@Test void msg add test2 public class MessageUtilTest () msg1 = System.getProperty(“my message1”); msg2 = System.getProperty(“my message2”); msg3 = System.getProperty(“my message3″); msg = msg1+” “+msg2+” “+msg3; assertEquals(msg, “Hello, Dear Developer!”);
Results:
mvn test $ -Dtest=MessageUtilTest#msg add test2 [INFO] ———————————————————————————- [INFO] S E R V I C E [INFO] ———————————————————————————- [INFO] Running commuting .javabydeveloper.util. MessageUtilTest [INFO] Tests run: 1, failures: 0, errors: 0, errors: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, omitted: 0, 0.342 seconds elapsed – in com .javabydeveloper.util.MessageUtilTest [INFO] [INFO] Results: [INFO] [INFO] Failures: 0; Errors: 0; Skipped: 0; Tests run: 1; Failures: 0; Errors: 0; Skipped: 0; [INFO] [INFO] ———————————————————————————————————— [INFO] BUILD SUCCESSFULNESS [INFO] ———————————————————————————————————— [INFO] 20.983 seconds total
2.4 The -D option may be used to override the System Properties specified in the file from the command line. Let’s look at an example.
mvn test -Dtest=MessageUtilTest#msg add test2 -Dmy message3=Customer! $ mvn test -Dtest=MessageUtilTest#msg add test2 -Dmy message3=Customer!
3. Select Tests of Integration from the System Properties menu.
To run integration tests, we utilize the Maven Failsafe plugin. Failsafe plugin, like surefire plugin, has the same configuration options for setting System Properties. All of the techniques above may be used with the failsafe plugin as well.
org.apache.maven.plugins integration-test verify maven-failsafe-plugin 3.0.0-M5 Good day, Developer! src/test/resources/system props.properties
Performing the Integration Test:
@Test void msg add test2 public class ITMessageUtilTest () msg1 = System.getProperty(“my message1”); msg2 = System.getProperty(“my message2”); msg3 = System.getProperty(“my message3″); msg = msg1+” “+msg2+” “+msg3; assertEquals(msg, “Hello, Dear Developer!”); mvn verify -Dit mvn verify -Dit mvn verify -Dit m test=ITMessageUtilTest#msg add test2 [INFO] ———————————————————————————- [INFO] T E S T S [INFORMATION] ———————————————————————————- [INFO] com.javabydeveloper.util.ITMessageUtilTest is being run. [INFO] Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.357 s – in com.javabydeveloper.util.ITMessageUtilTest [INFO] [INFO] Results: [INFO] [INFO] Failures: 0; Errors: 0; Skipped: 0; Tests run: 1; Failures: 0; Errors: 0; Skipped: 0; [INFO] [INFO] @ Junit5-maven-example maven-failsafe-plugin:3.0.0-M5:verify (default) —- [INFO] ———————————————————————————————————— [INFO] BUILD SUCCESSFULNESS [INFO] ———————————————————————————————————— [INFO] Time taken: 32.124 seconds
4. Making use of the Exec plugin
4.1. Maven will be used to execute the Java Main class using the Exec plugin. Let’s look at how to set System Properties from the command line and in plugin settings.
MainClass1:
System.out.println(“—— Main Class1 —–“); System.out.println(“my prop: “+System.getProperty(“my prop”)); System.out.println(“—— Main Class1 —–“); System.out.println(“—— Main Class1 —–“); System.out.println(“—— Main Class1 —–“); System.out.println(“—— Main
Configuration of the plugin:
exec-maven-plugin 3.0.0 by org.codehaus.mojo
Results:
mvn exec:java -Dexec.mainClass=”com.javabydeveloper.util.MainClass1″ -Dmy prop=”Hello, Developer!” $ mvn exec:java -Dexec.mainClass=”com.javabydeveloper.util.MainClass1″ -Dmy prop=”Hello, Developer!” [INFO] [INFO] @ Junit5-maven-example exec-maven-plugin:3.0.0:java (default-cli) ————— 1st Main Class Hello, Developer! ——- my prop: [INFO] ———————————————————————————————————— [INFO] BUILD SUCCESSFULNESS [INFO] ———————————————————————————————————— [INFO] 5.385 seconds total
4.2. System Properties may be configured at the plugin level, and particular phases can be specified.
test java com.javabydeveloper.util.MainClass1 my prop org.codehaus.mojo exec-maven-plugin 3.0.0 org.codehaus.mojo exec-maven-plugin 3.0.0 org.codehaus.mojo exec-maven-plugin 3.0.0 org.codehaus Good day, Developer!
Results:
mvn test $ [INFO] [INFO] @ Junit5-maven-example exec-maven-plugin:3.0.0:java (default) ————— ———————————————————————————————————— Good day, Developer! [INFO] ———————————————————————————————————— [INFO] BUILD SUCCESSFULNESS [INFO] ———————————————————————————————————— [INFO] 19.563 seconds total
5. Final thoughts
In this lesson, we looked at how to use Maven to specify System Properties in tests and how to run the Java Main class using Maven.
You may also be interested in these Maven guides:
- Maven should be installed on Windows.
- Setup Maven on a Mac
- On Ubuntu, install Maven.
- Eclipse may be used to import a Maven project.
- Maven runs a single test
- Set the Java version using Maven.
- Maven doesn’t run tests.
- Local Maven repository
6. Bibliography
- Integration Tests
- Surefire plugin
this advertisement should be reported
Maven is a Java build tool that allows developers to create reusable software components. The maven download is available for free on the website.
{“@context”:”https://schema.org”,”@type”:”FAQPage”,”mainEntity”:[{“@type”:”Question”,”name”:”What is Maven and why we use it?”,”acceptedAnswer”:{“@type”:”Answer”,”text”:”
Maven is a software framework for building, testing, and deploying Java applications. It provides a set of common tools that developers can use to build their applications with less effort.”}},{“@type”:”Question”,”name”:”Why is Maven so bad?”,”acceptedAnswer”:{“@type”:”Answer”,”text”:”
The Maven is not bad, it is just a new way of doing things. It takes time to get used to the changes in the game and learn how to use them effectively.”}},{“@type”:”Question”,”name”:”What is Maven in simple words?”,”acceptedAnswer”:{“@type”:”Answer”,”text”:”
Maven is the name of a person who is an expert in any field.”}}]}
Frequently Asked Questions
What is Maven and why we use it?
Maven is a software framework for building, testing, and deploying Java applications. It provides a set of common tools that developers can use to build their applications with less effort.
Why is Maven so bad?
The Maven is not bad, it is just a new way of doing things. It takes time to get used to the changes in the game and learn how to use them effectively.
What is Maven in simple words?
Maven is the name of a person who is an expert in any field.
Related Tags
- maven tutorial
- maven dependency
- maven search
- maven central
- apache maven