Maven Profile无法获取site.url:Java+TestNG项目启动报错
Got it, let's break down why you're hitting that NullPointerException and get your setup working properly.
Root Cause
There are two key issues here:
- Your code is trying to fetch
site.urldirectly viaSystem.getProperty("site.url"), but Maven's Profile property isn't being passed into the TestNG test's JVM system properties. - Even though you enabled resource filtering to replace
${site.url}inenvironment.properties, your code doesn't actually read this properties file — it skips straight to system properties, which are empty. - Bonus: Your POM has duplicate
maven-compiler-pluginentries, which is unnecessary and messy.
Step-by-Step Fixes
You have two valid paths to resolve this; pick the one that fits your workflow better.
Option 1: Read the filtered environment.properties file in your code
Since you already set up Maven to replace the ${site.url} placeholder in environment.properties, update your test code to load this file and pull the value from it:
public void SetUp() { Properties envProps = new Properties(); try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("environment.properties")) { envProps.load(inputStream); String siteUrl = envProps.getProperty("site.url"); // Initialize driver and navigate System.setProperty("webdriver.chrome.driver", ConfigFileReader.getDriverPath()); driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get(siteUrl); driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS); } catch (IOException e) { e.printStackTrace(); // Add proper error handling here (e.g., throw a test initialization exception) } }
Option 2: Inject the Profile property directly into test system properties
If you prefer to keep using System.getProperty("site.url"), modify your maven-surefire-plugin configuration to pass the Profile's site.url into the test JVM's system properties:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.21.0</version> <configuration> <suiteXmlFiles> <file>${project.basedir}/suites/smoke.xml</file> </suiteXmlFiles> <systemPropertyVariables> <environment.properties>/environment.properties</environment.properties> <!-- Add this line to pass the Profile property as a system property --> <site.url>${site.url}</site.url> </systemPropertyVariables> </configuration> </plugin>
Now when you run mvn clean install -Pdevhost, the Surefire plugin will inject site.url into the test's system properties, and your existing System.getProperty("site.url") call will work as expected.
Additional Cleanup
- Remove the duplicate
maven-compiler-pluginentry from your POM — you only need one. - Use forward slashes (
/) for resource directory paths (e.g.,src/test/resources) instead of backslashes (\) to avoid cross-platform issues.
Verification
Run your command again:
mvn clean install -Pdevhost
Your test should now successfully retrieve the site.url value and navigate to the desired site without the NullPointerException.
内容的提问来源于stack exchange,提问作者Y_Sh

